Show
Ignore:
Timestamp:
2007-05-19 14:13:37 (16 months ago)
Author:
johnnyege
Message:

- Separated XML definition parsers out from main code, and made them implement a common interface. This will make implementation of a Schema parser possible.
- Implemented support for URLs to DTD's.

Location:
FCKdtd2js/trunk
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • FCKdtd2js/trunk

    • Property svn:ignore set to
      .project

      .classpath

      .amateras

      .settings
  • FCKdtd2js/trunk/_source

    • Property svn:ignore set to
      _jar
  • FCKdtd2js/trunk/_source/src/net/fckeditor/devutil/dtd/DTDJsGenerator.java

    r264 r317  
    2525import java.io.IOException; 
    2626import java.io.PrintStream; 
    27 import java.util.Collection; 
     27import java.net.MalformedURLException; 
     28import java.net.URI; 
     29import java.net.URISyntaxException; 
     30import java.net.URL; 
     31import java.text.ParseException; 
    2832import java.util.Collections; 
    29 import java.util.HashMap; 
    3033import java.util.HashSet; 
    3134import java.util.Iterator; 
     
    3740import java.util.TreeSet; 
    3841 
    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  
    4842/** 
    4943 * Takes a DTD file as input, and produces a compressed JS map from it. 
     
    6256                if(args.length < 1) writeHelp(System.out); 
    6357                 
    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                        } 
    6980                } 
    7081                 
     
    8192                // Create application instance, and execute the parsing. 
    8293                try { 
    83                         DTDJsGenerator generator = new DTDJsGenerator(dtdFile, removeTags); 
     94                        DTDJsGenerator generator = new DTDJsGenerator(dtdUri, removeTags); 
    8495                        generator.run(); 
    8596                } 
     
    102113        //##################################################################### 
    103114         
    104         protected File  _dtdFile; 
     115        protected URI   _dtdUri; 
    105116        protected Set   _removeTags; 
    106117         
     
    111122         * @param removeTags The tags to use as root tags. 
    112123         */ 
    113         public DTDJsGenerator(File dtdFile, Set removeTags) { 
    114                 if(dtdFile == null || removeTags == null) throw new IllegalArgumentException("File parameter cannot be null."); 
    115                 _dtdFile = 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; 
    116127                if(removeTags == null) removeTags = Collections.EMPTY_SET; 
    117128                _removeTags = removeTags; 
     
    121132         * Runs the generator. 
    122133         */ 
    123         public void run() throws IOException { 
     134        public void run() throws IOException, ParseException { 
    124135                // 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 ); 
    130138                 
    131139                // Remove unwanted elements from the map. 
     
    144152                 
    145153                // 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(); 
    147155                 
    148156                ElementGroupMapJavascriptBuilder jsBuilder = new ElementGroupMapJavascriptBuilder(); 
     
    300308        } 
    301309 
    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 
    378311         
    379312}