/install/phing/classes/phing/types/selectors/ContainsSelector.php

https://github.com/chregu/fluxcms · PHP · 151 lines · 78 code · 16 blank · 57 comment · 11 complexity · 97bb933f4ca73895e4dff8244efe0b69 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/types/selectors/BaseExtendSelector.php';
  22. /**
  23. * Selector that filters files based on whether they contain a
  24. * particular string.
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  27. * @author Bruce Atherton <bruce@callenish.com> (Ant)
  28. * @package phing.types.selectors
  29. */
  30. class ContainsSelector extends BaseExtendSelector {
  31. private $contains = null;
  32. private $casesensitive = true;
  33. const CONTAINS_KEY = "text";
  34. const CASE_KEY = "casesensitive";
  35. public function toString() {
  36. $buf = "{containsselector text: ";
  37. $buf .= $this->contains;
  38. $buf .= " casesensitive: ";
  39. if ($this->casesensitive) {
  40. $buf .= "true";
  41. } else {
  42. $buf .= "false";
  43. }
  44. $buf .= "}";
  45. return $buf;
  46. }
  47. /**
  48. * The string to search for within a file.
  49. *
  50. * @param string $contains the string that a file must contain to be selected.
  51. */
  52. public function setText($contains) {
  53. $this->contains = $contains;
  54. }
  55. /**
  56. * Whether to ignore case in the string being searched.
  57. *
  58. * @param boolean $casesensitive whether to pay attention to case sensitivity
  59. */
  60. public function setCasesensitive($casesensitive) {
  61. $this->casesensitive = $casesensitive;
  62. }
  63. /**
  64. * When using this as a custom selector, this method will be called.
  65. * It translates each parameter into the appropriate setXXX() call.
  66. *
  67. * @param array $parameters the complete set of parameters for this selector
  68. */
  69. public function setParameters($parameters) {
  70. parent::setParameters($parameters);
  71. if ($parameters !== null) {
  72. for ($i=0,$size=count($parameters); $i < $size; $i++) {
  73. $paramname = $parameters[$i]->getName();
  74. switch(strtolower($paramname)) {
  75. case self::CONTAINS_KEY:
  76. $this->setText($parameters[$i]->getValue());
  77. break;
  78. case self::CASE_KEY:
  79. $this->setCasesensitive($parameters[$i]->getValue());
  80. break;
  81. default:
  82. $this->setError("Invalid parameter " . $paramname);
  83. }
  84. } // for each param
  85. } // if params
  86. }
  87. /**
  88. * Checks to make sure all settings are kosher. In this case, it
  89. * means that the pattern attribute has been set.
  90. *
  91. */
  92. public function verifySettings() {
  93. if ($this->contains === null) {
  94. $this->setError("The text attribute is required");
  95. }
  96. }
  97. /**
  98. * The heart of the matter. This is where the selector gets to decide
  99. * on the inclusion of a file in a particular fileset.
  100. *
  101. * @param basedir the base directory the scan is being done from
  102. * @param filename is the name of the file to check
  103. * @param file is a java.io.File object the selector can use
  104. * @return whether the file should be selected or not
  105. */
  106. public function isSelected(File $basedir, $filename, File $file) {
  107. $this->validate();
  108. if ($file->isDirectory()) {
  109. return true;
  110. }
  111. $userstr = $this->contains;
  112. if (!$this->casesensitive) {
  113. $userstr = strtolower($this->contains);
  114. }
  115. $in = null;
  116. try {
  117. $in = new BufferedReader(new FileReader($file));
  118. $teststr = $in->readLine();
  119. while ($teststr !== null) {
  120. if (!$this->casesensitive) {
  121. $teststr = strtolower($teststr);
  122. }
  123. if (strpos($teststr, $userstr) !== false) {
  124. return true;
  125. }
  126. $teststr = $in->readLine();
  127. }
  128. return false;
  129. } catch (IOException $ioe) {
  130. if ($in) $in->close();
  131. throw new BuildException("Could not read file " . $filename);
  132. }
  133. $in->close();
  134. }
  135. }