1 19 package org.netbeans.modules.xslt.model.impl; 20 21 import org.netbeans.modules.xslt.model.Attribute; 22 import org.netbeans.modules.xslt.model.Choose; 23 import org.netbeans.modules.xslt.model.Document; 24 import org.netbeans.modules.xslt.model.ForEach; 25 import org.netbeans.modules.xslt.model.If; 26 import org.netbeans.modules.xslt.model.Key; 27 import org.netbeans.modules.xslt.model.LiteralResultElement; 28 import org.netbeans.modules.xslt.model.Namespace; 29 import org.netbeans.modules.xslt.model.Otherwise; 30 import org.netbeans.modules.xslt.model.Param; 31 import org.netbeans.modules.xslt.model.Sequence; 32 import org.netbeans.modules.xslt.model.SequenceConstructor; 33 import org.netbeans.modules.xslt.model.Sort; 34 import org.netbeans.modules.xslt.model.Stylesheet; 35 import org.netbeans.modules.xslt.model.Template; 36 import org.netbeans.modules.xslt.model.ValueOf; 37 import org.netbeans.modules.xslt.model.Variable; 38 import org.netbeans.modules.xslt.model.When; 39 import org.netbeans.modules.xslt.model.XslComponent; 40 import org.netbeans.modules.xslt.model.XslVisitorAdapter; 41 import org.w3c.dom.Element ; 42 43 44 48 class XslComponentsBuildVisitor extends XslVisitorAdapter { 49 50 XslComponentsBuildVisitor( XslModelImpl model ){ 51 assert model != null; 52 myModel = model; 53 } 54 55 XslComponent createSubComponent( XslComponent parent , Element element ) 56 { 57 myElement = element; 58 String namespace = element.getNamespaceURI(); 59 if ( namespace == null && parent instanceof XslComponentImpl ) { 60 namespace = ((XslComponentImpl)parent). 61 lookupNamespaceURI(element.getPrefix()); 62 } 63 64 65 if (XslComponent.XSL_NAMESPACE.equals(namespace)) { 66 if (parent == null) { 67 if (XslElements.STYLESHEET.getName().equals( 68 getElement().getLocalName()) 69 || XslElements.TRANSFORM.getName().equals( 70 getElement().getLocalName())) 71 { 72 setResult(new StylesheetImpl(getModel(), element)); 73 } 74 } 75 else { 76 parent.accept(this); 77 } 78 } 79 else { 80 createNonXslComponentChild( parent ); 81 } 82 return myResult; 83 } 84 85 88 @Override 89 public void visit( Stylesheet stylesheet ) 90 { 91 if ( isAcceptable( XslElements.IMPORT) ) { 92 setResult( new ImportImpl( getModel() , getElement()) ); 93 } 94 else if ( isAcceptable( XslElements.VARIABLE )) { 95 setResult( new VariableImpl( getModel() , getElement()) ); 96 } 97 else if ( isAcceptable( XslElements.PARAM )) { 98 setResult( new ParamImpl( getModel() , getElement()) ); 99 } 100 else { 101 createDeclaration(stylesheet ); 102 } 103 104 } 105 106 @Override 107 public void visit( Variable variable ) { 108 visitSequenceConstructor( variable ); 109 } 110 111 @Override 112 public void visit( Document document ) { 113 visitSequenceConstructor( document ); 114 } 115 116 @Override 117 public void visit( org.netbeans.modules.xslt.model.Element element ) { 118 visitSequenceConstructor( element ); 119 } 120 121 @Override 122 public void visit( Attribute attribute ) { 123 visitSequenceConstructor( attribute ); 124 } 125 126 @Override 127 public void visit( ForEach forEach ) { 128 if ( visitSequenceConstructor( forEach ) ) { 129 return; 130 } 131 if ( isAcceptable( XslElements.SORT )) { 132 setResult( new SortImpl( getModel() , getElement()) ); 133 } 134 } 135 136 @Override 137 public void visit( If iff ) { 138 visitSequenceConstructor( iff ); 139 } 140 141 @Override 142 public void visit( Key key ) { 143 visitSequenceConstructor( key ); 144 } 145 146 @Override 147 public void visit( Namespace namespace ) { 148 visitSequenceConstructor( namespace ); 149 } 150 151 @Override 152 public void visit( Otherwise otherwise ) { 153 visitSequenceConstructor( otherwise ); 154 } 155 156 @Override 157 public void visit( Param param ) { 158 visitSequenceConstructor( param ); 159 } 160 161 @Override 162 public void visit( Sequence sequence ) { 163 visitSequenceConstructor( sequence ); 164 } 165 166 @Override 167 public void visit( Sort sort ) { 168 visitSequenceConstructor( sort ); 169 } 170 171 @Override 172 public void visit( Template template ) { 173 if ( visitSequenceConstructor( template ) ) { 174 return; 175 } 176 if ( isAcceptable( XslElements.PARAM )) { 177 setResult( new ParamImpl( getModel() , getElement()) ); 178 } 179 } 180 181 @Override 182 public void visit( ValueOf valueOf ) { 183 visitSequenceConstructor( valueOf ); 184 } 185 186 @Override 187 public void visit( When when ) { 188 visitSequenceConstructor( when ); 189 } 190 191 public void visit( Choose choose ) { 192 if ( isAcceptable( XslElements.WHEN )) { 193 setResult( new WhenImpl( getModel() , getElement() ) ); 194 } 195 else if ( isAcceptable( XslElements.OTHERWISE )) { 196 setResult( new OtherwiseImpl( getModel() , getElement() ) ); 197 } 198 } 199 200 203 @Override 204 public void visit( LiteralResultElement element ) 205 { 206 visitSequenceConstructor( element ); 207 } 208 209 private boolean visitSequenceConstructor( SequenceConstructor constructor ) { 210 if ( isAcceptable( XslElements.APPLY_TEMPLATES)) { 211 setResult( new ApplyTemplatesImpl( getModel() , getElement() ) ); 212 } 213 else if ( isAcceptable( XslElements.ATTRIBUTE) ) { 214 setResult( new AttributeImpl( getModel() , getElement()) ); 215 } 216 else if ( isAcceptable( XslElements.CALL_TEMPLATE) ) { 217 setResult( new CallTemplateImpl( getModel() , getElement()) ); 218 } 219 else if ( isAcceptable( XslElements.CHOOSE) ) { 220 setResult( new ChooseImpl( getModel() , getElement()) ); 221 } 222 else if ( isAcceptable( XslElements.COPY) ) { 223 setResult( new CopyImpl( getModel() , getElement()) ); 224 } 225 else if ( isAcceptable( XslElements.COPY_OF) ) { 226 setResult( new CopyOfImpl( getModel() , getElement()) ); 227 } 228 else if ( isAcceptable( XslElements.DOCUMENT) ) { 229 setResult( new DocumentImpl( getModel() , getElement()) ); 230 } 231 else if ( isAcceptable( XslElements.ELEMENT ) ) { 232 setResult( new ElementImpl( getModel() , getElement()) ); 233 } 234 else if ( isAcceptable( XslElements.FOR_EACH) ) { 235 setResult( new ForEachImpl( getModel() , getElement()) ); 236 } 237 else if ( isAcceptable( XslElements.IF ) ) { 238 setResult( new IfImpl( getModel() , getElement()) ); 239 } 240 else if ( isAcceptable( XslElements.NAMESPACE ) ) { 241 setResult( new NamespaceImpl( getModel() , getElement()) ); 242 } 243 else if ( isAcceptable( XslElements.NUMBER ) ) { 244 setResult( new NumberImpl( getModel() , getElement()) ); 245 } 246 else if ( isAcceptable( XslElements.SEQUENCE ) ) { 247 setResult( new SequenceImpl( getModel() , getElement()) ); 248 } 249 else if ( isAcceptable( XslElements.TEXT ) ) { 250 setResult( new TextImpl( getModel() , getElement()) ); 251 } 252 else if ( isAcceptable( XslElements.VALUE_OF ) ) { 253 setResult( new ValueOfImpl( getModel() , getElement()) ); 254 } 255 return myResult!= null; 256 } 257 258 private void createDeclaration( Stylesheet stylesheet ) { 259 if ( isAcceptable( XslElements.ATTRIBUTE_SET ) ) { 260 setResult( new AttributeSetImpl( getModel() , getElement()) ); 261 } 262 else if ( isAcceptable( XslElements.INCLUDE ) ) { 263 setResult( new IncludeImpl( getModel() , getElement() ) ); 264 } 265 else if ( isAcceptable( XslElements.KEY ) ) { 266 setResult( new KeyImpl( getModel() , getElement() ) ); 267 } 268 else if ( isAcceptable( XslElements.TEMPLATE ) ) { 269 setResult( new TemplateImpl( getModel() , getElement() ) ); 270 } 271 else if ( isAcceptable( XslElements.OUTPUT ) ) { 272 setResult( new OutputImpl( getModel() , getElement() ) ); 273 } 274 } 275 276 277 private void createNonXslComponentChild( XslComponent parent ) { 278 if ( parent instanceof SequenceConstructor ) { 279 setResult( new LiteralResultElementImpl( getModel() , getElement() ) ); 280 } 281 } 282 283 private void setResult( XslComponent component ) { 284 myResult = component; 285 } 286 287 private boolean isAcceptable( XslElements element ) { 288 return element.getName().equals( getLocalName() ); 289 } 290 291 private String getLocalName() { 292 return getElement().getLocalName(); 293 } 294 295 void init(){ 296 myResult = null; 297 myElement = null; 298 } 299 300 private XslModelImpl getModel() { 301 return myModel; 302 } 303 304 private Element getElement() { 305 return myElement; 306 } 307 308 private Element myElement; 309 310 private XslModelImpl myModel; 311 312 private XslComponent myResult; 313 } 314 | Popular Tags |