1 18 19 20 package org.apache.struts.tiles.xmlDefinition; 21 22 import java.io.BufferedInputStream ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 28 import org.apache.commons.digester.Digester; 29 import org.xml.sax.SAXException ; 30 31 34 public class XmlParser 35 { 36 37 38 protected Digester digester; 39 43 protected boolean validating = false; 44 49 protected String registrations[] = { 50 "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN", 51 "/org/apache/struts/resources/tiles-config_1_1.dtd", 52 }; 53 54 58 public XmlParser() 59 { 60 digester = new Digester(); 61 digester.setValidating(validating); 62 digester.setNamespaceAware(true); 63 digester.setUseContextClassLoader(true); 64 for (int i = 0; i < registrations.length; i += 2) { 66 URL url = this.getClass().getResource(registrations[i+1]); 67 if (url != null) 68 { 69 digester.register(registrations[i], url.toString()); 70 } 71 } 72 initDigester( digester ); 74 } 75 76 79 public void setValidating( boolean validating ) 80 { 81 digester.setValidating( validating); 82 } 83 84 85 90 private void initDigesterForComponentsDefinitionsSyntax( Digester digester ) 91 { 92 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition"; 94 String DEFINITION_TAG = "component-definitions/definition"; 95 String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition"; 96 97 String PUT_TAG = DEFINITION_TAG + "/put"; 98 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute"; 99 100 String LIST_TAG = DEFINITION_TAG + "/putList"; 101 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute"; 102 103 String ADD_LIST_ELE_TAG = LIST_TAG + "/add"; 104 105 digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass ); 107 digester.addSetProperties( DEFINITION_TAG); 108 digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass); 109 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass); 111 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass); 112 digester.addSetProperties( PUT_TAG); 113 digester.addCallMethod( PUT_TAG, "setBody", 0); 114 digester.addObjectCreate( LIST_TAG, listHandlerClass); 116 digester.addSetProperties( LIST_TAG); 117 digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass); 118 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass); 122 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass); 123 digester.addSetProperties( ADD_LIST_ELE_TAG); 124 digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0); 125 } 126 127 132 private void initDigesterForTilesDefinitionsSyntax( Digester digester ) 133 { 134 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition"; 136 String DEFINITION_TAG = "tiles-definitions/definition"; 137 String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition"; 138 139 String PUT_TAG = DEFINITION_TAG + "/put"; 140 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute"; 141 142 String LIST_TAG = "putList"; 145 String DEF_LIST_TAG = DEFINITION_TAG + "/" + LIST_TAG; 146 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute"; 147 String ADD_LIST_ELE_TAG = "*/" + LIST_TAG + "/add"; 149 150 digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass ); 152 digester.addSetProperties( DEFINITION_TAG); 153 digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass); 154 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass); 160 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass); 161 digester.addSetProperties( PUT_TAG); 162 digester.addCallMethod( PUT_TAG, "setBody", 0); 163 digester.addObjectCreate( DEF_LIST_TAG, listHandlerClass); 166 digester.addSetProperties( DEF_LIST_TAG); 167 digester.addSetNext( DEF_LIST_TAG, "addAttribute", putAttributeHandlerClass); 168 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass); 172 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass); 173 digester.addSetProperties( ADD_LIST_ELE_TAG); 174 digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0); 175 176 String NESTED_LIST = "*/" + LIST_TAG + "/" + LIST_TAG; 179 digester.addObjectCreate( NESTED_LIST, listHandlerClass); 180 digester.addSetProperties( NESTED_LIST); 181 digester.addSetNext( NESTED_LIST, "add", putAttributeHandlerClass); 182 183 String ADD_WILDCARD = "*/item"; 189 String menuItemDefaultClass = "org.apache.struts.tiles.beans.SimpleMenuItem"; 190 digester.addObjectCreate( ADD_WILDCARD, menuItemDefaultClass, "classtype"); 191 digester.addSetNext( ADD_WILDCARD, "add", "java.lang.Object"); 192 digester.addSetProperties( ADD_WILDCARD); 193 194 String BEAN_TAG = "*/bean"; 196 String beanDefaultClass = "org.apache.struts.tiles.beans.SimpleMenuItem"; 197 digester.addObjectCreate( BEAN_TAG, beanDefaultClass, "classtype"); 198 digester.addSetNext( BEAN_TAG, "add", "java.lang.Object"); 199 digester.addSetProperties( BEAN_TAG); 200 201 digester.addSetProperty(BEAN_TAG+ "/set-property", "property", "value"); 203 } 204 205 211 private void initDigesterForInstancesSyntax( Digester digester ) 212 { 213 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition"; 215 String INSTANCE_TAG = "component-instances/instance"; 216 String instanceHandlerClass = PACKAGE_NAME + ".XmlDefinition"; 217 218 String PUT_TAG = INSTANCE_TAG + "/put"; 219 String PUTATTRIBUTE_TAG = INSTANCE_TAG + "/putAttribute"; 220 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute"; 221 222 String LIST_TAG = INSTANCE_TAG + "/putList"; 223 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute"; 224 225 String ADD_LIST_ELE_TAG = LIST_TAG + "/add"; 226 227 digester.addObjectCreate( INSTANCE_TAG, instanceHandlerClass ); 229 digester.addSetProperties( INSTANCE_TAG); 230 digester.addSetNext( INSTANCE_TAG, "putDefinition", instanceHandlerClass); 231 digester.addObjectCreate( PUTATTRIBUTE_TAG, putAttributeHandlerClass); 233 digester.addSetProperties( PUTATTRIBUTE_TAG); 234 digester.addSetNext( PUTATTRIBUTE_TAG, "addAttribute", putAttributeHandlerClass); 235 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass); 237 digester.addSetProperties( PUT_TAG); 238 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass); 239 digester.addObjectCreate( LIST_TAG, listHandlerClass); 241 digester.addSetProperties( LIST_TAG); 242 digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass); 243 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass); 247 digester.addSetProperties( ADD_LIST_ELE_TAG); 248 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass); 249 } 250 251 255 protected void initDigester( Digester digester ) 256 { 257 initDigesterForTilesDefinitionsSyntax( digester ); 258 initDigesterForComponentsDefinitionsSyntax( digester ); 259 initDigesterForInstancesSyntax( digester ); 260 } 261 262 269 public void parse( InputStream in, XmlDefinitionsSet definitions ) throws IOException , SAXException 270 { 271 try 272 { 273 digester.push(definitions); 276 digester.parse(in); 278 in.close(); 279 } 280 catch (SAXException e) 281 { 282 throw e; 284 } 285 286 } 287 288 291 public static void main(String [] args) 292 { 293 String filename = "E:/programs/jakarta-tomcat-4.0.3/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml"; 295 299 300 if( args.length > 1 ) 301 { 302 filename = args[1]; 303 } 305 System.out.println( "Read file '" + filename +"'" ); 306 307 InputStream input = null; 308 try 311 { 312 input = new BufferedInputStream ( 313 new FileInputStream ( filename) ); 314 } 317 catch( IOException ex ) 318 { 319 System.out.println( "can't open file '" + filename + "' : " + ex.getMessage() ); 320 } 321 try 323 { 324 XmlParser parser = new XmlParser(); 325 parser.setValidating(true); 326 XmlDefinitionsSet definitions = new XmlDefinitionsSet(); 327 System.out.println( " Parse file" ); 328 parser.parse( input, definitions); 329 System.out.println( " done." ); 332 System.out.println( " Result : " + definitions.toString() ); 333 } 334 catch( Exception ex ) 335 { 336 System.out.println( "Error during parsing '" + filename + "' : " + ex.getMessage() ); 337 ex.printStackTrace(); 338 } 339 } 340 341 } 342 | Popular Tags |