- Timestamp:
- 2007-05-19 14:13:37 (14 months ago)
- Location:
- FCKdtd2js/trunk
- Files:
-
- 2 added
- 4 modified
-
. (modified) (1 prop)
-
FCKdtd2js.jar (modified) (previous)
-
_source (modified) (1 prop)
-
_source/src/net/fckeditor/devutil/dtd/DTDJsGenerator.java (modified) (9 diffs)
-
_source/src/net/fckeditor/devutil/dtd/WutkaDTDParser.java (added)
-
_source/src/net/fckeditor/devutil/dtd/XmlDefinitionParser.java (added)
Legend:
- Unmodified
- Added
- Removed
-
FCKdtd2js/trunk
-
Property
svn:ignore set
to
.project
.classpath
.amateras
.settings
-
Property
svn:ignore set
to
-
FCKdtd2js/trunk/_source
-
Property
svn:ignore set
to
_jar
-
Property
svn:ignore set
to
-
FCKdtd2js/trunk/_source/src/net/fckeditor/devutil/dtd/DTDJsGenerator.java
r264 r317 25 25 import java.io.IOException; 26 26 import java.io.PrintStream; 27 import java.util.Collection; 27 import java.net.MalformedURLException; 28 import java.net.URI; 29 import java.net.URISyntaxException; 30 import java.net.URL; 31 import java.text.ParseException; 28 32 import java.util.Collections; 29 import java.util.HashMap;30 33 import java.util.HashSet; 31 34 import java.util.Iterator; … … 37 40 import java.util.TreeSet; 38 41 39 import com.wutka.dtd.DTD;40 import com.wutka.dtd.DTDContainer;41 import com.wutka.dtd.DTDElement;42 import com.wutka.dtd.DTDEmpty;43 import com.wutka.dtd.DTDItem;44 import com.wutka.dtd.DTDName;45 import com.wutka.dtd.DTDPCData;46 import com.wutka.dtd.DTDParser;47 48 42 /** 49 43 * Takes a DTD file as input, and produces a compressed JS map from it. … … 62 56 if(args.length < 1) writeHelp(System.out); 63 57 64 // Create a file object. 65 File dtdFile = new File(args[0]); 66 if(!dtdFile.exists()) { 67 System.err.println("Unable to locate DTD file ["+dtdFile.getPath()+"]"); 68 return; 58 // Prepare holder for URI. 59 URI dtdUri = null; 60 61 // First test for file 62 File dtdFile = new File(args[0].trim()); 63 if(dtdFile.exists()) { 64 // Convert File to URI 65 dtdUri = dtdFile.getAbsoluteFile().toURI(); 66 } 67 else { 68 // Try to parse as URI. 69 try { 70 dtdUri = new URI(args[0].trim()); 71 if(!dtdUri.isAbsolute()) { 72 System.err.println("URI is not absolute : " + args[0].trim()); 73 return; 74 } 75 } 76 catch(URISyntaxException e) { 77 System.err.println("Unable to parse : " + args[0].trim()); 78 return; 79 } 69 80 } 70 81 … … 81 92 // Create application instance, and execute the parsing. 82 93 try { 83 DTDJsGenerator generator = new DTDJsGenerator(dtd File, removeTags);94 DTDJsGenerator generator = new DTDJsGenerator(dtdUri, removeTags); 84 95 generator.run(); 85 96 } … … 102 113 //##################################################################### 103 114 104 protected File _dtdFile;115 protected URI _dtdUri; 105 116 protected Set _removeTags; 106 117 … … 111 122 * @param removeTags The tags to use as root tags. 112 123 */ 113 public DTDJsGenerator( File dtdFile, Set removeTags) {114 if(dtd File== null || removeTags == null) throw new IllegalArgumentException("File parameter cannot be null.");115 _dtd File = dtdFile;124 public DTDJsGenerator(URI dtdUri, Set removeTags) { 125 if(dtdUri == null || removeTags == null) throw new IllegalArgumentException("File parameter cannot be null."); 126 _dtdUri = dtdUri; 116 127 if(removeTags == null) removeTags = Collections.EMPTY_SET; 117 128 _removeTags = removeTags; … … 121 132 * Runs the generator. 122 133 */ 123 public void run() throws IOException {134 public void run() throws IOException, ParseException { 124 135 // Parse the DTD tree. 125 DTDParser parser = new DTDParser(_dtdFile); 126 DTD dtd = parser.parse(); 127 128 // Create a group map. 129 Map<String,ElementGroup> groupMap = createElementGroupMap(dtd); 136 XmlDefinitionParser xmlDefParser = new WutkaDTDParser(); 137 Map<String, ElementGroup> groupMap = xmlDefParser.parseXmlDefinition( _dtdUri ); 130 138 131 139 // Remove unwanted elements from the map. … … 144 152 145 153 // Build Javascript from the DTD map. 146 String comment = "This file was automatically generated from the file: " + _dtdFile.getName();154 String comment = "This file was automatically generated from : " + (new File(_dtdUri.getPath())).getName(); 147 155 148 156 ElementGroupMapJavascriptBuilder jsBuilder = new ElementGroupMapJavascriptBuilder(); … … 300 308 } 301 309 302 /** 303 * Creates a map over all elementgroups in the DTD. 304 * @param dtd The DTD to create map from 305 * @return The Map containing all elementgroups. 306 */ 307 protected Map<String,ElementGroup> createElementGroupMap(DTD dtd) { 308 // Create a common empty group... 309 ElementGroup<String> emptyGroup = new ElementGroup<String>(); 310 311 // Create map containing the element groups. 312 Map<String,ElementGroup> resultMap = new HashMap<String,ElementGroup>(); 313 // Get elements from DTD. 314 Collection elements = dtd.elements.values(); 315 316 // Loop through elements and register them. 317 for(Iterator it = elements.iterator(); it.hasNext(); ) { 318 DTDElement element = (DTDElement)it.next(); 319 320 // Check if element exists in nodemap. 321 if(element.content instanceof DTDContainer) { 322 323 //System.out.println("Container: " + element.name ); 324 325 // Create an element group to contain sub elements. 326 ElementGroup<String> newGroup = new ElementGroup<String>(); 327 328 // Call recursive method to get all nodes. 329 if(appendAllowedElementsRecursive((DTDContainer)element.content, newGroup)) { 330 // Add the group to the result map. 331 resultMap.put(element.name, newGroup); 332 } 333 } 334 else 335 if(element.content instanceof DTDEmpty){ 336 //System.out.println("Empty : " +element.name); 337 // Handle DTDEmpty 338 resultMap.put(element.name, emptyGroup); 339 } 340 } 341 342 return resultMap; 343 } 344 345 /** 346 * We need a recursive method in order to resolve all elements in a collection. 347 */ 348 protected boolean appendAllowedElementsRecursive(DTDContainer col, ElementGroup<String> targetGroup) { 349 // Loop through allowed children. 350 DTDItem[] allowedChilds = col.getItems(); 351 352 boolean hasRealContent = false; 353 for(int i = 0; i < allowedChilds.length; i++) { 354 // System.out.println(" - Allowed childs ["+col+"] : " + allowedChilds[i].getClass() ); 355 356 if(allowedChilds[i] instanceof DTDName) { 357 targetGroup.add(((DTDName)allowedChilds[i]).value); 358 hasRealContent = true; 359 } 360 else 361 if(allowedChilds[i] instanceof DTDContainer) { 362 boolean recResult = appendAllowedElementsRecursive((DTDContainer)allowedChilds[i], targetGroup); 363 hasRealContent = hasRealContent || recResult; 364 } 365 else 366 if(allowedChilds[i] instanceof DTDPCData) { 367 targetGroup.add("#"); // Indicates that text is allowed. 368 hasRealContent = true; 369 } 370 else 371 if(allowedChilds[i] instanceof DTDEmpty) { 372 hasRealContent = true; 373 } 374 } 375 return hasRealContent; 376 } 377 310 378 311 379 312 }