Changeset 4
- Timestamp:
- 08/25/06 22:12:25 (2 years ago)
- Files:
-
- trunk/XJConf/DefinitionParser.php (modified) (10 diffs)
- trunk/XJConf/XmlParser.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/XJConf/DefinitionParser.php
r2 r4 13 13 /** 14 14 * Parse tag definitions files. 15 * 15 * 16 16 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 17 17 */ … … 19 19 { 20 20 const TAG_NAMESPACE = 'namespace'; 21 21 22 22 /** 23 23 * Stack for currently open definitions 24 * 24 * 25 25 * @var array<Definition> 26 26 */ 27 27 private $defStack = array(); 28 28 29 29 private $defHandlerStack = array(); 30 30 31 31 /** 32 32 * Constant for the default namespace 33 33 */ 34 34 const DEFAULT_NAMESPACE = '__default'; 35 35 36 36 /** 37 37 * The current namespace 38 * 38 * 39 39 * @var string 40 40 */ 41 private $currentNamespace = self::DEFAULT_NAMESPACE;42 41 private $currentNamespace; 42 43 43 /** 44 44 * All extracted namespace definitions 45 * 45 * 46 46 * @var NamespaceDefinitions 47 47 */ 48 48 private $defs; 49 49 50 50 private $reader; 51 51 52 private $nodeTypes = array(); 53 54 52 55 /** 53 56 * constructor … … 56 59 { 57 60 $this->defs = new NamespaceDefinitions(); 61 $this->currentNamespace = self::DEFAULT_NAMESPACE; 62 63 if (!defined('XMLREADER_ELEMENT')) { 64 $this->nodeTypes = array('startTag' => XMLReader::ELEMENT, 65 'text' => XMLReader::TEXT, 66 'endTag' => XMLReader::END_ELEMENT 67 ); 68 } else { 69 $this->nodeTypes = array('startTag' => XMLREADER_ELEMENT, 70 'text' => XMLREADER_TEXT, 71 'endTag' => XMLREADER_END_ELEMENT 72 ); 73 } 58 74 } 59 75 60 76 public function getCurrentNamespace() 61 77 { 62 78 return $this->currentNamespace; 63 79 } 64 80 65 81 public function getNamespaceDefinitions() 66 82 { 67 83 return $this->defs; 68 84 } 69 85 70 86 public function &getDefStack() 71 87 { 72 88 return $this->defStack; 73 89 } 74 90 75 91 private function initParser() 76 92 { … … 83 99 * parse a tag definitions file and return 84 100 * an instance of NamespaceDefinition 85 * 101 * 86 102 * @param string $filename filename of the defintions file 87 103 * @return NamespaceDefinition … … 94 110 while ($this->reader->read()) { 95 111 switch ($this->reader->nodeType) { 96 case XMLReader::ELEMENT:112 case $this->nodeTypes['startTag']: 97 113 $nameSpaceURI = $this->reader->namespaceURI; 98 114 $elementName = $this->reader->localName; … … 107 123 } 108 124 } 109 125 110 126 $this->startElement($nameSpaceURI, $elementName, $attributes); 111 127 break; 112 113 case XMLReader::TEXT:128 129 case $this->nodeTypes['text']: 114 130 $this->characters($this->reader->value); 115 131 break; 116 117 case XMLReader::END_ELEMENT:132 133 case $this->nodeTypes['endTag']: 118 134 $this->endElement($this->reader->namespaceURI, $this->reader->localName); 119 135 break; 120 136 } 121 137 } 122 138 123 139 $this->reader->close($filename); 124 140 125 141 return $this->defs; 126 142 } 127 143 128 144 /** 129 145 * Start Element handler 130 * 146 * 131 147 * Creates the TagDefinition object and places it on 132 148 * the stack. 133 * 149 * 134 150 * @param string $namespaceURI namespace of start tag 135 151 * @param string $sName name of start tag … … 143 159 throw new InvalidNamespaceDefinitionException('The <' . self::TAG_NAMESPACE . '> tag is missing the uri attribute.'); 144 160 } 145 161 146 162 $this->currentNamespace = $atts['uri']; 147 163 return; 148 164 } 149 165 150 166 $defHandler = DefinitionHandlerFactory::create($sName, $this); 151 167 $def = $defHandler->startElement($namespaceURI, $sName, $atts); … … 153 169 array_push($this->defStack, $def); 154 170 } 155 171 156 172 if ($defHandler->needsEnd() == true) { 157 173 array_push($this->defHandlerStack, $defHandler); … … 161 177 /** 162 178 * End Element handler 163 * 179 * 164 180 * Fetches the TagDefinition from the stack and 165 181 * adds it to the NamespaceDefinition object. … … 172 188 return; 173 189 } 174 190 175 191 $defHandler = array_pop($this->defHandlerStack); 176 192 $defHandler->endElement($namespaceURI, $sName); trunk/XJConf/XmlParser.php
r2 r4 10 10 { 11 11 private $tagStack = array(); 12 12 13 13 private $config = array(); 14 14 15 15 private $tagDefs; 16 16 17 17 private $depth = 0; 18 18 19 19 private $extensions = array(); 20 20 21 21 private $myNamespace = 'http://www.schst.net/XJConf'; 22 22 23 23 private $openFiles = array(); 24 24 25 25 private $reader; 26 26 27 27 private $nodeTypes = array(); 28 28 29 29 public function __construct() 30 30 { 31 if ( version_compare(phpversion(), '5.1') >= 0) {31 if (!defined('XMLREADER_ELEMENT')) { 32 32 $this->nodeTypes = array('startTag' => XMLReader::ELEMENT, 33 33 'text' => XMLReader::TEXT, … … 41 41 } 42 42 } 43 43 44 44 public function setTagDefinitions(NamespaceDefinitions $tagDefs) 45 45 { 46 46 $this->tagDefs = $tagDefs; 47 47 } 48 48 49 49 public function addTagDefinitions(NamespaceDefinitions $tagDefs) 50 50 { … … 53 53 return; 54 54 } 55 55 56 56 $this->tagDefs->appendNamespaceDefinitions($tagDefs); 57 57 } 58 58 59 59 public function addExtension($namespace, Extension $ext) 60 60 { 61 61 $this->extensions[$namespace] = $ext; 62 62 } 63 63 64 64 public function parse($filename) 65 65 { … … 82 82 } 83 83 } 84 84 85 85 $this->startElement($nameSpaceURI, $elementName, $attributes); 86 86 break; 87 87 88 88 case $this->nodeTypes['text']: 89 89 $this->characters($this->reader->value); 90 90 break; 91 91 92 92 case $this->nodeTypes['endTag']: 93 93 $this->endElement($this->reader->namespaceURI, $this->reader->localName); … … 95 95 } 96 96 } 97 97 98 98 $this->reader->close($filename); 99 99 array_pop($this->openFiles); 100 101 } 102 100 101 } 102 103 103 public function getConfigValue($name) 104 104 { 105 105 return $this->config[$name]; 106 106 } 107 107 108 108 public function getCurrentFile() 109 109 { 110 110 return end($this->openFiles); 111 111 } 112 112 113 113 private function initParser() 114 114 { … … 117 117 } 118 118 } 119 119 120 120 /** 121 121 * Start element 122 * 122 * 123 123 * Creates a new Tag object and pushes it 124 124 * onto the stack. 125 * 125 * 126 126 * @param string $namespaceURI namespace of start tag 127 127 * @param string $sName name of start tag … … 130 130 private function startElement($namespaceURI, $sName, $atts) 131 131 { 132 132 133 133 if ($this->myNamespace == $namespaceURI && 0 < $this->depth) { 134 134 return; 135 135 } 136 136 $this->depth++; 137 137 138 138 // no namespace defined, use the default namespace 139 139 if (strlen($namespaceURI) == 0) { … … 144 144 if (1 == $this->depth) { 145 145 return; 146 } 146 } 147 147 148 148 // This tag needs to be handled by an extension … … 155 155 throw new UnknownNamespaceException('Unknown namespace ' . $namespaceURI . ' in file ' . end($this->openFiles)); 156 156 } 157 157 158 158 if ($this->tagDefs->isTagDefined($namespaceURI, $sName) == false) { 159 159 throw new UnknownTagException('Unknown tag ' . $sName . ' in namespace ' . $namespaceURI); 160 160 } 161 161 162 162 $tag = new DefinedTag($sName, $atts); 163 163 // fetch the defintion for this tag … … 165 165 $tag->setDefinition($tagDef); 166 166 } 167 167 168 168 array_push($this->tagStack, $tag); 169 169 } 170 170 171 171 /** 172 172 * End element. 173 * 173 * 174 174 * Fetches the current element from the stack and 175 175 * converts it to the correct type. 176 * 176 * 177 177 * @param string $namespaceURI namespace of end tag 178 178 * @param string $sName name of end tag … … 197 197 // get the last tag from the stack 198 198 $tag = array_pop($this->tagStack); 199 199 200 200 if (isset($this->extensions[$namespaceURI]) == true) { 201 201 $result = $this->extensions[$namespaceURI]->endElement($this, $tag); … … 221 221 } 222 222 } 223 223 224 224 /** 225 225 * Character data handler 226 * 226 * 227 227 * Fetches the current tag from the stack and 228 228 * appends the data. … … 233 233 return; 234 234 } 235 235 236 236 $tag = end($this->tagStack); 237 237 $tag->addData($buf);
