Ejemplo n.º 1
0
 Parser(String uri) {
   try {
     SAXParserFactory parserFactory = SAXParserFactory.newInstance();
     SAXParser parser = parserFactory.newSAXParser();
     ConfigHandler handler = new ConfigHandler();
     parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", true);
     parser.parse(new File(uri), handler);
   } catch (IOException e) {
     System.out.println("Error reading URI: " + e.getMessage());
   } catch (SAXException e) {
     System.out.println("Error in parsing: " + e.getMessage());
   } catch (ParserConfigurationException e) {
     System.out.println("Error in XML parser configuration: " + e.getMessage());
   }
   // System.out.println("Number of Persons : " + Person.numberOfPersons());
   // nameLengthStatistics();
   // System.out.println("Number of Publications with authors/editors: " +
   //                   Publication.getNumberOfPublications());
   // System.out.println("Maximum number of authors/editors in a publication: " +
   //                           Publication.getMaxNumberOfAuthors());
   // publicationCountStatistics();
   // Person.enterPublications();
   // Person.printCoauthorTable();
   // Person.printNamePartTable();
   // Person.findSimilarNames();
 }
Ejemplo n.º 2
0
  /**
   * Create an Archival Unit
   *
   * @return true If successful
   */
  private boolean createAu() {

    Configuration config = getAuConfigFromForm();

    AuProxy au;
    Element element;

    try {
      au = getRemoteApi().createAndSaveAuConfiguration(getPlugin(), config);

    } catch (ArchivalUnit.ConfigurationException exception) {
      return error("Configuration failed: " + exception.getMessage());

    } catch (IOException exception) {
      return error("Unable to save configuration: " + exception.getMessage());
    }
    /*
     * Successful creation - add the AU name and ID to the response document
     */
    element = getXmlUtils().createElement(getResponseRoot(), AP_E_AU);
    XmlUtils.addText(element, au.getName());

    element = getXmlUtils().createElement(getResponseRoot(), AP_E_AUID);
    XmlUtils.addText(element, au.getAuId());

    return true;
  }
Ejemplo n.º 3
0
  public void write(SecureItemTable tbl, char[] password) throws IOException {
    OutputStream os = new FileOutputStream(file);
    OutputStream xmlout;

    if (password.length == 0) {
      xmlout = os;
      os = null;
    } else {
      PBEKeySpec keyspec = new PBEKeySpec(password);
      Cipher c;
      try {
        SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = fac.generateSecret(keyspec);

        c = Cipher.getInstance("PBEWithMD5AndDES");
        c.init(Cipher.ENCRYPT_MODE, key, pbeSpec);
      } catch (java.security.GeneralSecurityException exc) {
        os.close();
        IOException ioe = new IOException("Security exception during write");
        ioe.initCause(exc);
        throw ioe;
      }

      CipherOutputStream out = new CipherOutputStream(os, c);
      xmlout = out;
    }

    try {
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer t = tf.newTransformer();

      DOMSource src = new DOMSource(tbl.getDocument());
      StringWriter writer = new StringWriter();
      StreamResult sr = new StreamResult(writer);
      t.transform(src, sr);

      OutputStreamWriter osw = new OutputStreamWriter(xmlout, StandardCharsets.UTF_8);
      osw.write(writer.toString());
      osw.close();
    } catch (Exception exc) {
      IOException ioe = new IOException("Unable to serialize XML");
      ioe.initCause(exc);
      throw ioe;
    } finally {
      xmlout.close();
      if (os != null) os.close();
    }

    tbl.setDirty(false);
    return;
  }
 private Document getXMLDocument(InputStream is) {
   SAXBuilder builder = new SAXBuilder();
   builder.setValidation(false);
   builder.setIgnoringElementContentWhitespace(true);
   Document doc = null;
   try {
     doc = builder.build(is);
   } catch (JDOMException e) {
     e.printStackTrace();
     System.exit(-1);
   } catch (IOException e) {
     e.printStackTrace();
     System.exit(-1);
   }
   return doc;
 }
Ejemplo n.º 5
0
 public void save() throws IOException {
   synchronized (this) {
     FileWriter fw = null;
     try {
       fw = new FileWriter(file);
       fw.write(toString());
     } finally {
       if (fw != null)
         try {
           fw.close();
         } catch (IOException e) {
           e.printStackTrace();
         }
     }
   }
 }
Ejemplo n.º 6
0
 public static void loadDict(String strDict) {
   try {
     BufferedReader br = new BufferedReader(new FileReader(strDict));
     String line = null;
     while ((line = br.readLine()) != null) {
       int idx = line.indexOf(',');
       if (idx > 0) {
         htDict.put(line.substring(0, idx), line.substring(idx + 1));
       }
     }
   } catch (FileNotFoundException fnfe) {
     fnfe.printStackTrace(System.out);
   } catch (IOException ioe) {
     ioe.printStackTrace(System.out);
   }
 }
Ejemplo n.º 7
0
  /**
   * EPML2PNML
   *
   * @param args String[]
   */
  private static void EPML2PNML(String[] args) {
    if (args.length != 2) {
      System.out.println("���ṩEPML�ļ�·����PNML���Ŀ¼!");
      System.exit(-1);
    }
    epmlImport epml = new epmlImport();
    // load the single epml file
    String filename = args[0];
    try {
      EPCResult epcRes = (EPCResult) epml.importFile(new FileInputStream(filename));
      // convert all epc models to pnml files
      ArrayList<ConfigurableEPC> alEPCs = epcRes.getAllEPCs();
      PnmlExport export = new PnmlExport();
      for (ConfigurableEPC epc : alEPCs) {
        String id = epc.getIdentifier();
        if (id.equals("1An_klol")
            || id.equals("1An_l1y8")
            || id.equals("1Ex_dzq9")
            || id.equals("1Ex_e6dx")
            || id.equals("1Ku_9soy")
            || id.equals("1Ku_a4cg")
            || id.equals("1Or_lojl")
            || id.equals("1Pr_d1ur")
            || id.equals("1Pr_djki")
            || id.equals("1Pr_dkfa")
            || id.equals("1Pr_dl73")
            || id.equals("1Ve_musj")
            || id.equals("1Ve_mvwz")) continue;
        // save pnml files to the same folder
        File outFile = new File(args[1] + "/" + id + ".pnml");
        if (outFile.exists()) continue;
        FileOutputStream fos = new FileOutputStream(outFile);
        PetriNet petri = AMLtoPNML.convert(epc);
        try {
          export.export(new ProvidedObject("PetriNet", new Object[] {petri}), fos);
        } catch (Exception ex1) {
          ex1.printStackTrace(System.out);
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace(System.out);
    }

    System.out.println("EPML Conversion Done");
  }
Ejemplo n.º 8
0
  /**
   * start the parsing
   *
   * @param file to parse
   * @return Vector containing the test cases
   */
  public XMLcpimParser(String fileLocation) {
    try {
      SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
      saxParser = saxParserFactory.newSAXParser().getXMLReader();
      saxParser.setContentHandler(this);
      saxParser.setFeature("http://xml.org/sax/features/validation", true);
      // parse the xml specification for the event tags.
      saxParser.parse(fileLocation);

    } catch (SAXParseException spe) {
      spe.printStackTrace();
    } catch (SAXException sxe) {
      sxe.printStackTrace();
    } catch (IOException ioe) {
      // I/O error
      ioe.printStackTrace();
    } catch (Exception pce) {
      // Parser with specified options can't be built
      pce.printStackTrace();
    }
  }
Ejemplo n.º 9
0
  private static void AML2PNML(String[] args) {
    if (args.length != 3) {
      System.out.println("���ṩAML����Ŀ¼��PNML���Ŀ¼�Լ��ֵ��ļ�!");
      System.exit(-1);
    }
    System.out.println("����Ŀ¼��" + args[0]);
    System.out.println("���Ŀ¼��" + args[1]);
    System.out.println("�ֵ��ļ���" + args[2]);
    // load the dict
    AMLtoPNML.loadDict(args[2]);
    File srcDir = new File(args[0]);
    File[] lstAML = srcDir.listFiles();
    PnmlExport export = new PnmlExport();
    for (int i = 0; i < lstAML.length; i++) {
      if (lstAML[i].isDirectory()) {
        continue;
      }
      System.out.print(lstAML[i].getName() + "==>");
      try {
        FileInputStream fis = new FileInputStream(lstAML[i]);
        int idx = lstAML[i].getName().indexOf(".xml");
        File outFile = new File(args[1] + "/" + lstAML[i].getName().substring(0, idx) + ".pnml");
        FileOutputStream fos = new FileOutputStream(outFile);
        EPCResult epcRes = (EPCResult) AMLtoPNML.importFile(fis);
        ConfigurableEPC epc = epcRes.getMainEPC();
        PetriNet petri = AMLtoPNML.convert(epc);
        export.export(new ProvidedObject("PetriNet", new Object[] {petri}), fos);
        System.out.println(outFile.getName());
      } catch (FileNotFoundException ex) {
        ex.printStackTrace(System.out);
      } catch (IOException ioe) {
        ioe.printStackTrace(System.out);
      } catch (Exception e) {
        e.printStackTrace(System.out);
      }
    }

    System.out.println("Conversion Done");
  }
Ejemplo n.º 10
0
 public static StringProperty[] initialize(String xmlFile) {
   FileInputStream fi = null;
   StaticInitializeSaxHandler handler = null;
   try {
     handler = new StaticInitializeSaxHandler();
     XMLReader parser = null;
     parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
     parser.setContentHandler(handler);
     fi = new FileInputStream(xmlFile);
     parser.parse(new InputSource(new InputStreamReader(fi, "UTF-8")));
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (fi != null)
       try {
         fi.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
   }
   return handler.getProps();
 }
  @Override
  public String parseFulltext(String html) {
    if (!judge(html)) return null;
    Parser theParser = null;

    HTMLSchema theSchema = null;
    XMLReader r;
    if (theParser == null) theParser = new Parser();
    r = theParser;
    theSchema = new HTMLSchema();
    try {
      r.setProperty(Parser.schemaProperty, theSchema);
      r.setFeature(Parser.namespacesFeature, false);
    } catch (SAXNotRecognizedException e) {
      logger.fatal(e.getMessage());
      return null;
    } catch (SAXNotSupportedException e) {
      logger.fatal(e.getMessage());
      return null;
    }

    RejuvenationResearchParserHandler h = new RejuvenationResearchParserHandler();
    r.setContentHandler(h);
    InputStream fin = new ByteArrayInputStream(html.getBytes());
    InputSource s = new InputSource(fin);
    s.setEncoding("utf8");
    try {
      r.parse(s);
      fin.close();
      return h.getContent();
    } catch (IOException e) {
      logger.fatal(e.getMessage());
    } catch (SAXException e) {
      logger.fatal(e.getMessage());
    }

    return null;
  }
Ejemplo n.º 12
0
  public static SolrInputDocument indexMetadataFile(
      SolrInputDocument doc, Collection<DcsMetadataRef> set, List<SolrInputDocument> docs) {

    int foundFgdc = 0;
    // Assuming the DCS file is available in the Sip
    for (DcsMetadataRef ref : set) {
      DcsFile file = null;
      for (SolrInputDocument document : docs) {
        String refStr = ref.getRef();
        String id = (String) document.getFieldValue(EntityField.ID.solrName());
        if (refStr.equals(id))
          try {
            file = (DcsFile) fromSolr(toSolrDocument(document));
          } catch (IOException e) {
            e
                .printStackTrace(); // To change body of catch statement use File | Settings | File
                                    // Templates.
          }
      }
      if (file == null) continue;
      // get the file from SIP or from index
      //  DcsFile file = new DcsFile();
      for (DcsFormat format : file.getFormats()) {
        if (format.getFormat().contains("fgdc")) {
          MetadataDocument fgdcDoc = null;
          try {
            String fileSource = file.getSource().replace("file://", "");

            fgdcDoc = MetadataDocument.Factory.parse(new File(fileSource));
            ThemeType[] keywords = fgdcDoc.getMetadata().getIdinfo().getKeywords().getThemeArray();
            PlaceType[] places = fgdcDoc.getMetadata().getIdinfo().getKeywords().getPlaceArray();

            Set<String> themes = new HashSet<String>();
            for (ThemeType theme : keywords) {
              for (String themeStr : theme.getThemekeyArray()) themes.add(themeStr);
            }
            addStrings(doc, themes, CoreMetadataField.SUBJECT);

            Set<String> placeKeys = new HashSet<String>();
            for (PlaceType place : places) {
              for (String placeStr : place.getPlacekeyArray()) placeKeys.add(placeStr);
            }
            addStrings(doc, placeKeys, SeadSolrField.DeliverableUnitField.LOCATION);

          } catch (XmlException e) {
            e
                .printStackTrace(); // To change body of catch statement use File | Settings | File
                                    // Templates.
          } catch (IOException e) {
            e
                .printStackTrace(); // To change body of catch statement use File | Settings | File
                                    // Templates.
          }
          foundFgdc = 1;
        }
        if (foundFgdc == 1) break;
      }
      if (foundFgdc == 1) break;
    }
    return doc;
  }