Changeset 1111


Ignore:
Timestamp:
Jun 26, 2011, 10:42:56 AM (6 years ago)
Author:
stefan
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/jlatexeditor/PackagesExtractor.java

    r1106 r1111  
    11package jlatexeditor;
    22
    3 import my.XML.XMLDocument;
    4 import my.XML.XMLParser;
    53import org.xml.sax.*;
    64import org.xml.sax.helpers.DefaultHandler;
     
    97import util.TrieSet;
    108
    11 import javax.xml.parsers.SAXParserFactory;
     9import java.io.FileNotFoundException;
    1210import java.io.IOException;
    13 import java.util.ArrayList;
    14 import java.util.HashMap;
    1511
    1612/**
     
    1814 */
    1915public class PackagesExtractor {
    20   private static final String FILENAME = "data/codehelper/packages.xml";
     16  private static final String PACKAGES_FILE = "data/codehelper/packages.xml";
     17  private static final String DOCCLASSES_FILE = "data/codehelper/docclasses.xml";
     18
     19  private static PackageParser packageParser;
     20  private static PackageParser docclassesParser;
    2121
    2222  public static void main(String[] args) {
    23     XMLParser xmlParser = new XMLParser();
    24     XMLDocument commandsDocument;
    2523    try {
    2624      long startTime = System.nanoTime();
    2725
    28       try {
    29         XMLReader parser = XMLReaderFactory.createXMLReader();
    30         DefaultHandler handler = new DebugHandler();
    31         parser.setContentHandler(handler);
    32         parser.setErrorHandler(handler);
    33         try {
    34           parser.parse(new InputSource(StreamUtils.getInputStream(FILENAME)));
    35         } catch (IOException ioe) {
    36           ioe.printStackTrace();
    37         }
    38       } catch (SAXException se) {
    39         se.printStackTrace();
    40       }
     26      packageParser = new PackageParser(PACKAGES_FILE);
     27      docclassesParser = new PackageParser(DOCCLASSES_FILE);
    4128
    4229      System.out.println((System.nanoTime() - startTime) / (1000 * 1000));
    4330    } catch (Exception e) {
    4431      e.printStackTrace();
    45       return;
    4632    }
    4733  }
     
    5036   * Test Handler that prints debug statements
    5137   */
    52   public static class DebugHandler extends org.xml.sax.helpers.DefaultHandler implements org.xml.sax.ContentHandler {
    53     private String pack;
     38  public static class PackageParser extends DefaultHandler implements ContentHandler {
     39    private Package pack;
    5440    private String debPack;
     41    private TrieSet<Package> packages = new TrieSet<Package>();
    5542    private TrieSet<Command> commands = new TrieSet<Command>();
    5643
    57     public DebugHandler() {
    58       super();
     44    public PackageParser(String fileName) {
     45      parse(fileName);
    5946    }
    6047
    61     /*
    62     public void characters(char[] chars, int start, int length) {
    63       String string = new String(chars, start, length);
    64       System.out.println("chars=" + string + "!");
    65     }
    66     */
    67 
    68     public void startDocument() {
    69       //System.out.println("start document");
    70     }
    71 
    72     public void endDocument() {
    73       //System.out.println("end document");
     48    public void parse(String fileName) {
     49      try {
     50        XMLReader parser = XMLReaderFactory.createXMLReader();
     51        parser.setContentHandler(this);
     52        parser.setErrorHandler(this);
     53        parser.parse(new InputSource(StreamUtils.getInputStream(fileName)));
     54      } catch (Exception se) {
     55        se.printStackTrace();
     56      }
    7457    }
    7558
     
    8063        int argCount = Integer.parseInt(attrList.getValue("argCount"));
    8164        String optionalArg = attrList.getValue("optionalArg");
    82         Command command = new Command(name, argCount, optionalArg, pack, debPack);
     65        Command command = new Command(name, argCount, optionalArg, pack);
    8366        commands.add(name, command);
    8467      } else
    8568      if (localName.equals("package")) {
    86         pack = attrList.getValue("name");
    87         debPack = attrList.getValue("debPackage");
     69        pack = new Package(attrList.getValue("name"), attrList.getValue("debPackage"));
     70        packages.add(pack.name, pack);
    8871      }
    89     }
    90 
    91     public void endElement(String namespaceURI, String localName, String qName) {
    92       //System.out.println("end: " + localName);
    9372    }
    9473
     
    10685  }
    10786
     87  public static class Package {
     88    private String name;
     89    private String debPackage;
     90
     91    public Package(String name, String debPackage) {
     92      this.name = name;
     93      this.debPackage = debPackage;
     94    }
     95
     96    @Override
     97    public boolean equals(Object obj) {
     98      if (obj instanceof Package) {
     99        Package that = (Package) obj;
     100        return this.name.equals(that.name);
     101      }
     102      return false;
     103    }
     104
     105    @Override
     106    public int hashCode() {
     107      return name.hashCode();
     108    }
     109
     110    public String getName() {
     111      return name;
     112    }
     113
     114    public String getDebPackage() {
     115      return debPackage;
     116    }
     117  }
     118
    108119  public static class Command {
    109120    private String name;
    110121    private int argCount;
    111122    private String optionalArg;
    112     private String pack;
    113     private String debPack;
     123    private Package pack;
    114124
    115     public Command(String name, int argCount, String optionalArg, String pack, String debPack) {
     125    public Command(String name, int argCount, String optionalArg, Package pack) {
    116126      this.name = name;
    117127      this.argCount = argCount;
    118128      this.optionalArg = optionalArg;
    119129      this.pack = pack;
    120       this.debPack = debPack;
     130    }
     131
     132    @Override
     133    public boolean equals(Object obj) {
     134      if (obj instanceof Command) {
     135        Command that = (Command) obj;
     136        return this.name.equals(that.name) && this.pack.equals(that.pack);
     137      }
     138      return false;
    121139    }
    122140
     
    126144    }
    127145
    128     @Override
    129     public boolean equals(Object obj) {
    130       if (obj instanceof Command) {
    131         Command that = (Command) obj;
    132         return this.pack.equals(that.pack) && this.name.equals(that.name);
    133       }
    134       return false;
     146    public String getName() {
     147      return name;
     148    }
     149
     150    public int getArgCount() {
     151      return argCount;
     152    }
     153
     154    public String getOptionalArg() {
     155      return optionalArg;
     156    }
     157
     158    public Package getPack() {
     159      return pack;
    135160    }
    136161  }
Note: See TracChangeset for help on using the changeset viewer.