Esempio n. 1
0
  /** @return whether or not the MetaDD should be written out at the end. */
  private static boolean processMetaDD(Config config) throws IOException {
    boolean needToWriteMetaDD = false;
    MetaDD mdd;
    //
    //	Either creates the metaDD file or read the existing one
    //
    if (config.getMddFile() != null || config.getMddIn() != null) {
      File file = config.getMddFile();

      if (config.getMddIn() == null && !file.exists()) {
        config.setDoGeneration(false);
        if (!config.isAuto()) {
          if (!askYesOrNo(
              config.messageOut,
              "The mdd file "
                  + config.getMddFile()
                  + // NOI18N
                  " doesn't exist. Should we create it (y/n)? ")) { // NOI18N
            config.messageOut.println("Generation aborted."); // NOI18N
            return false;
          }
        }
        needToWriteMetaDD = true;
        mdd = new MetaDD();
      } else {
        try {
          InputStream is = config.getMddIn();
          if (config.getMddIn() == null) {
            is = new FileInputStream(config.getMddFile());
            config.messageOut.println(Common.getMessage("MSG_UsingMdd", config.getMddFile()));
          }
          mdd = MetaDD.read(is);
          if (config.getMddIn() == null) {
            is.close();
          }
        } catch (IOException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (javax.xml.parsers.ParserConfigurationException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (org.xml.sax.SAXException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        }
      }
    } else {
      // Create a MetaDD to look stuff up in later on, even though
      // it wasn't read in, and we're not going to write it out.
      mdd = new MetaDD();
    }
    config.setMetaDD(mdd);
    return needToWriteMetaDD;
  }
Esempio n. 2
0
  public boolean performTest(String arg) {
    boolean passed = true;
    try {
      Writer writer = new Writer(false);
      DOMParser parser = new DOMParser();
      if (!arg.equals("delete2") && !arg.equals("insert2")) {
        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < tests.length; i++) {
          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + tests[i] + ":");
          parser.parse(new InputSource(new StringReader(tests[i])));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Range range = document.createRange();
          Node root = document.getDocumentElement();
          boolean surround = false;
          Node surroundNode = document.createElement(SURROUND);
          if (arg.equals("surround")) {
            surround = true;
          }

          if (i == 0) {
            range.setStart(root.getFirstChild(), 1);
            range.setEndBefore(root.getLastChild());
            if (surround) range.setEnd(root.getLastChild(), 1);

          } else if (i == 1) {
            Node n1 = root.getFirstChild().getNextSibling().getFirstChild();
            range.setStart(n1, 1);
            range.setEnd(root.getLastChild(), 1);
            if (surround) range.setEnd(n1, 2);
          } else if (i == 2) {
            range.setStart(root.getFirstChild(), 1);
            Node n2 = root.getFirstChild().getNextSibling().getFirstChild();
            range.setEnd(n2, 1);
            if (surround) range.setEndBefore(root.getLastChild());
          } else if (i == 3) {
            Node n3 = root.getFirstChild().getFirstChild();
            range.setStart(n3, 1);
            range.setEnd(root.getLastChild().getFirstChild(), 1);
            if (surround) {
              range.selectNode(root.getFirstChild().getNextSibling());
            }
          } else if (i == 4) {
            Node n4 = root.getFirstChild().getFirstChild().getNextSibling().getFirstChild();
            range.setStartAfter(n4);
            range.setEndAfter(root.getLastChild().getFirstChild());
            if (surround) {
              range.selectNodeContents(root.getFirstChild().getNextSibling());
            }
          }

          if (fStdOut) System.out.println("range.toString=" + range.toString());
          DocumentFragment frag = null;

          if (arg.equals("surround")) {
            try {
              if (fStdOut) System.out.println("surroundNode=" + surroundNode);
              range.surroundContents(surroundNode);
            } catch (org.w3c.dom.ranges.RangeException e) {
              if (fStdOut) System.out.println(e);
            }
            String result = toString(document);
            if (fStdOut) System.out.println("After surround: document=" + result + ":");
            if (!result.equals(surroundResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + surroundResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Surround document Test[" + i + "] FAILED!");
            }
          }

          if (arg.equals("insert")) {
            range.insertNode(document.createTextNode(INSERT));
            String result = toString(document);
            if (fStdOut) System.out.println("After  insert: document=" + result + ":");
            if (!result.equals(insertResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + insertResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Insert document Test[" + i + "] FAILED!");
            }

          } else if (arg.equals("delete")) {
            range.deleteContents();
            String result = toString(document);
            if (fStdOut) System.out.println("After delete:" + result + ":");
            if (!result.equals(deleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + deleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Delete document Test[" + i + "] FAILED!");
            }
          } else if (arg.equals("extract")) {
            frag = range.extractContents();
            // range.insertNode(document.createTextNode("^"));
            String result = toString(document);
            if (fStdOut) System.out.println("After extract: document=" + result + ":");
            if (!result.equals(deleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + deleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Extract document Test[" + i + "] FAILED!");
            }
            String fragResult = toString(frag);
            if (fStdOut) System.out.println("After extract: fragment=" + fragResult + ":");
            if (!fragResult.equals(extractResult[i])) {
              if (fStdOut) System.out.println("Should be: fragment=" + extractResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Extract Fragment Test[" + i + "] FAILED!");
            }
          } else if (arg.equals("clone")) {
            frag = range.cloneContents();
            String fragResult = toString(frag);
            if (fStdOut) System.out.println("After clone: fragment=" + fragResult);
            if (!fragResult.equals(extractResult[i])) {
              if (fStdOut) System.out.println("Should be: fragment=" + extractResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Clone Fragment Test[" + i + "] FAILED!");
            }
          }
        }

      } else if (arg.equals("insert2")) {
        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < 4; i++) {

          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + INSERT2 + ":");
          parser.parse(new InputSource(new StringReader(INSERT2)));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Node root = document.getDocumentElement();
          Range range = document.createRange();
          range.setStart(root.getFirstChild(), 11);
          range.setEnd(root.getFirstChild(), 18);
          Range rangei = document.createRange();
          if (i == 0) {
            rangei.setStart(root.getFirstChild(), 10);
            rangei.setEnd(root.getFirstChild(), 10);
          }
          if (i == 1) {
            rangei.setStart(root.getFirstChild(), 11);
            rangei.setEnd(root.getFirstChild(), 11);
          }
          if (i == 2) {
            rangei.setStart(root.getFirstChild(), 12);
            rangei.setEnd(root.getFirstChild(), 12);
          }
          if (i == 3) {
            rangei.setStart(root.getFirstChild(), 17);
            rangei.setEnd(root.getFirstChild(), 17);
          }
          // if (fStdOut) System.out.println("range: start1=="+range.getStartContainer());
          // root.insertBefore(document.createTextNode("YES!"), root.getFirstChild());
          // if (fStdOut) System.out.println("range: start2=="+range.getStartContainer());

          if (DEBUG)
            if (fStdOut) System.out.println("before insert start=" + range.getStartOffset());
          if (DEBUG) if (fStdOut) System.out.println("before insert end=" + range.getEndOffset());
          rangei.insertNode(document.createTextNode(INSERTED_TEXT));
          if (DEBUG)
            if (fStdOut) System.out.println("after insert start=" + range.getStartOffset());
          if (DEBUG) if (fStdOut) System.out.println("after insert end=" + range.getEndOffset());

          String result = toString(document);
          if (fStdOut) System.out.println("After insert2: document=" + result + ":");
          if (!result.equals(rangeInsertResult[i])) {
            if (fStdOut) System.out.println("Should be: document=" + rangeInsertResult[i] + ":");
            passed = false;
            if (fStdOut) System.out.println("Test FAILED!");
            if (fStdOut) System.out.println("*** Delete Ranges document Test[" + i + "] FAILED!");
          }
        }
      } else if (arg.equals("delete2")) {
        //
        // Range Deletion, acting upon another range.
        //

        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < rangeDelete.length; i++) {
          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + rangeDelete[i] + ":");
          parser.parse(new InputSource(new StringReader(rangeDelete[i])));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Range range = document.createRange();
          Range ranged = document.createRange();
          Node root = document.getDocumentElement();
          boolean surround = false;
          Node surroundNode = document.createElement(SURROUND);
          if (arg.equals("surround")) {
            surround = true;
          }

          if (i == 0) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 14);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 19);
          } else if (i == 1) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 22);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 21);
          } else if (i == 2) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild().getNextSibling().getFirstChild(), 1);

            range.setStart(root.getFirstChild(), 11);

            range.setEndAfter(root.getFirstChild().getNextSibling().getFirstChild());
          } else if (i == 3) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 11);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 21);
          } else if (i == 4) {
            ranged.selectNode(root.getFirstChild().getNextSibling());

            range.setStart(root.getFirstChild().getNextSibling().getFirstChild(), 6);
            range.setEnd(root.getFirstChild().getNextSibling().getFirstChild(), 15);
          }

          DocumentFragment frag = null;

          if (arg.equals("delete2")) {
            if (DEBUG) {
              if (fStdOut) System.out.println("BEFORE deleteContents()");
              if (fStdOut) System.out.println("ranged: startc=" + ranged.getStartContainer());
              if (fStdOut) System.out.println("ranged: starto=" + ranged.getStartOffset());
              if (fStdOut) System.out.println("ranged:   endc=" + ranged.getEndContainer());
              if (fStdOut) System.out.println("ranged:   endo=" + ranged.getEndOffset());

              if (fStdOut) System.out.println("range: startc=" + range.getStartContainer());
              if (fStdOut) System.out.println("range: starto=" + range.getStartOffset());
              if (fStdOut) System.out.println("range:   endc=" + range.getEndContainer());
              if (fStdOut) System.out.println("range:   endo=" + range.getEndOffset());
            }
            ranged.deleteContents();
            String result = null;
            if (DEBUG) {
              if (fStdOut) System.out.println("AFTER deleteContents()");
              result = toString(document);
              if (fStdOut) System.out.println("ranged: startc=" + ranged.getStartContainer());
              if (fStdOut) System.out.println("ranged: starto=" + ranged.getStartOffset());
              if (fStdOut) System.out.println("ranged:   endc=" + ranged.getEndContainer());
              if (fStdOut) System.out.println("ranged:   endo=" + ranged.getEndOffset());

              if (fStdOut) System.out.println("range: startc=" + range.getStartContainer());
              if (fStdOut) System.out.println("range: starto=" + range.getStartOffset());
              if (fStdOut) System.out.println("range:   endc=" + range.getEndContainer());
              if (fStdOut) System.out.println("range:   endo=" + range.getEndOffset());
            }

            ranged.insertNode(document.createTextNode("^"));

            result = toString(document);
            if (fStdOut) System.out.println("After delete2: document=" + result + ":");
            if (!result.equals(rangeDeleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + rangeDeleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Delete Ranges document Test[" + i + "] FAILED!");
            }
          }
        }
      }

    } catch (org.xml.sax.SAXParseException spe) {
      passed = false;
    } catch (org.xml.sax.SAXException se) {
      if (se.getException() != null) se.getException().printStackTrace(System.err);
      else se.printStackTrace(System.err);
      passed = false;
    } catch (Exception e) {
      e.printStackTrace(System.err);
      passed = false;
    }
    if (!passed) if (fStdOut) System.out.println("*** The " + arg + " Test FAILED! ***");

    return passed;
  }
Esempio n. 3
0
  public static void main(String args[]) {

    if (args.length < 3) {
      System.err.println("Usage: Generate model-file ( -s length | -c count length )");
      System.exit(1);
    }

    try {
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();
      saxFactory.setNamespaceAware(true);
      SAXParser saxParser = saxFactory.newSAXParser();
      XMLReader xmlReader = saxParser.getXMLReader();

      XMLModelReader modelReader = new XMLModelReader(xmlReader);
      FileReader modelInput = new FileReader(args[0]);
      InputSource source = new InputSource(modelInput);

      Model model = modelReader.load(source);

      modelInput.close();

      model.check();

      if (args[1].equals("-s")) {
        int[][] result = model.generateSequence(Integer.parseInt(args[2]));

        List lexicon = model.getLexicon();
        for (int i = 0; i < result[0].length; i++) {
          System.out.print((Character) lexicon.get(result[0][i]));
        }
        System.out.println();
        int numberOfStates = model.getNumberOfStates();
        if (numberOfStates > 9) {
          for (int i = 0; i < result[1].length; i++) {
            System.out.print(result[1][i]);
            System.out.print(',');
          }
        } else {
          for (int i = 0; i < result[1].length; i++) {
            System.out.print(result[1][i]);
          }
        }
        System.out.println();
        for (int i = 1; i < numberOfStates; i++) {
          System.out.println(i + " = " + model.getStateName(i));
        }
      } else if (args[1].equals("-c")) {
        List lexicon = model.getLexicon();
        int count = Integer.parseInt(args[2]);
        int length = Integer.parseInt(args[3]);
        ListOfSequences uniqueSeqs = new ListOfSequences();
        for (int i = 0; i < count; i++) {
          int[][] result = model.generateSequence(length);
          uniqueSeqs.addSequence(result[0]);
        }
        Iterator seqs = uniqueSeqs.iterator();
        while (seqs.hasNext()) {
          SequenceCount seq = (SequenceCount) seqs.next();
          int[] key = seq.getSequence();
          for (int i = 0; i < key.length; i++) {
            System.out.print((Character) lexicon.get(key[i]));
          }
          System.out.print(',');
          System.out.println(seq.getCount());
        }
      }

    } catch (java.io.IOException ex) {
      ex.printStackTrace();
    } catch (org.xml.sax.SAXException ex) {
      System.err.println(ex.getMessage());
    } catch (javax.xml.parsers.ParserConfigurationException ex) {
      ex.printStackTrace();
    }
  }