public ArrayList<String> parseXML() throws Exception { ArrayList<String> ret = new ArrayList<String>(); handshake(); URL url = new URL( "http://mangaonweb.com/page.do?cdn=" + cdn + "&cpn=book.xml&crcod=" + crcod + "&rid=" + (int) (Math.random() * 10000)); String page = DownloaderUtils.getPage(url.toString(), "UTF-8", cookies); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(page)); Document d = builder.parse(is); Element doc = d.getDocumentElement(); NodeList pages = doc.getElementsByTagName("page"); total = pages.getLength(); for (int i = 0; i < pages.getLength(); i++) { Element e = (Element) pages.item(i); ret.add(e.getAttribute("path")); } return (ret); }
static { try { dbf.setNamespaceAware(true); db = dbf.newDocumentBuilder(); xpath.setNamespaceContext( new NamespaceContext() { @Override public Iterator<String> getPrefixes(String namespaceURI) { return Arrays.asList("md").iterator(); } @Override public String getPrefix(String namespaceURI) { return "md"; } @Override public String getNamespaceURI(String prefix) { return "http://www.osgi.org/xmlns/metatype/v1.1.0"; } }); } catch (ParserConfigurationException e) { e.printStackTrace(); throw new ExceptionInInitializerError(e); } }
Document parseDocument(String filename) throws Exception { FileReader reader = new FileReader(filename); String firstLine = new BufferedReader(reader).readLine(); reader.close(); Document document = null; if (firstLine.startsWith("<?xml")) { System.err.println("XML detected; using default XML parser."); } else { try { Class nekoParserClass = Class.forName("org.cyberneko.html.parsers.DOMParser"); Object parser = nekoParserClass.newInstance(); Method parse = nekoParserClass.getMethod("parse", new Class[] {String.class}); Method getDocument = nekoParserClass.getMethod("getDocument", new Class[0]); parse.invoke(parser, filename); document = (Document) getDocument.invoke(parser); } catch (Exception e) { System.err.println("NekoHTML HTML parser not found; HTML4 support disabled."); } } if (document == null) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_traffic factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e) { System.err.println("Warning: Could not disable external DTD loading"); } DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(filename); } return document; }
/** Create DOM builder using JAXP libraries. */ static DocumentBuilder makeBuilder(boolean validate) throws IOException, SAXException { DocumentBuilder builder; DocumentBuilderFactory factory; // create factory according to javax.xml.parsers.SAXParserFactory property // or platform default (i.e. com.sun...) try { factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(false); } catch (FactoryConfigurationError err) { notifyFactoryErr(err, "javax.xml.parsers.DocumentBuilderFactory"); // NOI18N throw err; } try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { SAXException sex = new SAXException("Configuration exception."); // NOI18N ErrorManager emgr = ErrorManager.getDefault(); emgr.annotate(sex, ex); emgr.annotate( sex, "Can not create a DOM builder!\nCheck javax.xml.parsers.DocumentBuilderFactory property and the builder library presence on classpath."); // NOI18N throw sex; } return builder; }
private void createDOM() throws ParserConfigurationException { if (dbf == null) { dbf = DocumentBuilderFactory.newInstance(); db = dbf.newDocumentBuilder(); dil = (DOMImplementationLS) db.getDOMImplementation(); dw = dil.createDOMWriter(); } }
/** * Create a new DocumentBuilder. * * <p>WARNING: Check how this is used in this file first before calling! */ private static DocumentBuilder newDocumentBuilder() { synchronized (documentBuilderFactory) { try { return documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new OXFException(e); } } }
/** Возвращает объект Document, который является объектным представлением XML документа. */ private static Document getDocument() throws Exception { try { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setValidating(false); DocumentBuilder builder = f.newDocumentBuilder(); return builder.parse(new File("app.xml")); } catch (Exception exception) { String message = "XML parsing error!"; throw new Exception(message); } }
public WikipediaMinerAnnotator(String configFile) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new FileInputStream(configFile)); url = getConfigValue("access", "url", doc); if (url.equals("")) throw new AnnotationException( "Configuration file " + configFile + " has missing value 'url'."); }
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { DateFormat df = DateFormat.getDateTimeInstance(); String titleStr = "C3P0 Status - " + df.format(new Date()); DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); DocumentBuilder db = fact.newDocumentBuilder(); Document doc = db.newDocument(); Element htmlElem = doc.createElement("html"); Element headElem = doc.createElement("head"); Element titleElem = doc.createElement("title"); titleElem.appendChild(doc.createTextNode(titleStr)); Element bodyElem = doc.createElement("body"); Element h1Elem = doc.createElement("h1"); h1Elem.appendChild(doc.createTextNode(titleStr)); Element h3Elem = doc.createElement("h3"); h3Elem.appendChild(doc.createTextNode("PooledDataSources")); Element pdsDlElem = doc.createElement("dl"); pdsDlElem.setAttribute("class", "PooledDataSources"); for (Iterator ii = C3P0Registry.getPooledDataSources().iterator(); ii.hasNext(); ) { PooledDataSource pds = (PooledDataSource) ii.next(); StatusReporter sr = findStatusReporter(pds, doc); pdsDlElem.appendChild(sr.reportDtElem()); pdsDlElem.appendChild(sr.reportDdElem()); } headElem.appendChild(titleElem); htmlElem.appendChild(headElem); bodyElem.appendChild(h1Elem); bodyElem.appendChild(h3Elem); bodyElem.appendChild(pdsDlElem); htmlElem.appendChild(bodyElem); res.setContentType("application/xhtml+xml"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); Source src = new DOMSource(doc); Result result = new StreamResult(res.getOutputStream()); transformer.transform(src, result); } catch (IOException e) { throw e; } catch (Exception e) { throw new ServletException(e); } }
public static final Skeleton loadSkeleton(File file) { try { FileInputStream input_stream = new FileInputStream(file); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new GeometryErrorHandler()); Document document = builder.parse(input_stream); Element root = document.getDocumentElement(); return parseSkeleton(root); } catch (Exception e) { throw new RuntimeException(e); } }
static { try { URL url = SpellCheckActivator.bundleContext.getBundle().getResource(RESOURCE_LOC); InputStream stream = url.openStream(); if (stream == null) throw new IOException(); // strict parsing options DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); // parses configuration xml /*- * Warning: Felix is unable to import the com.sun.rowset.internal * package, meaning this can't use the XmlErrorHandler. This causes * a warning and a default handler to be attached. Otherwise this * should have: builder.setErrorHandler(new XmlErrorHandler()); */ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); // iterates over nodes, parsing contents Node root = doc.getChildNodes().item(1); NodeList categories = root.getChildNodes(); for (int i = 0; i < categories.getLength(); ++i) { Node node = categories.item(i); if (node.getNodeName().equals(NODE_DEFAULTS)) { parseDefaults(node.getChildNodes()); } else if (node.getNodeName().equals(NODE_LOCALES)) { parseLocales(node.getChildNodes()); } else { logger.warn("Unrecognized category: " + node.getNodeName()); } } } catch (IOException exc) { logger.error("Unable to load spell checker parameters", exc); } catch (SAXException exc) { logger.error("Unable to parse spell checker parameters", exc); } catch (ParserConfigurationException exc) { logger.error("Unable to parse spell checker parameters", exc); } }
public void CaricamentoFile() { DocumentBuilderFactory factory; DocumentBuilder parser; Document document; try { factory = DocumentBuilderFactory.newInstance(); parser = factory.newDocumentBuilder(); InputStream is = getClass().getClassLoader().getResource("text/caselle.xml").openStream(); document = parser.parse(is); handleDocument(document); } catch (Exception ex) { System.out.println("Errore." + ex); ex.printStackTrace(); } }
/** * Reads an XML document from an input source and copies its values into the specified object * * @param ob The object to receive the values * @param source The location of the XML document * @throws IOException If there is an error reading the document */ public void readObject(Object ob, InputSource source) throws IOException { try { // Create a document builder to read the document DocumentBuilder builder = factory.newDocumentBuilder(); // Read the document Document doc = builder.parse(source); // Get the root element Element element = doc.getDocumentElement(); // Copy the root element into the bean readObject(ob, element); } catch (SAXException exc) { throw new IOException("Error parsing XML document: " + exc.toString()); } catch (ParserConfigurationException exc) { throw new IOException("Error parsing XML document: " + exc.toString()); } }
protected static Document GetXMLDocument(String url, int timeoutMS) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream input = HTMLTools.inputStream_GET(url, timeoutMS); InputStreamReader reader = new InputStreamReader(input, ENCODING_UTF8); InputSource inSrc = new InputSource(reader); inSrc.setEncoding(ENCODING_UTF8); return db.parse(inSrc); } catch (ParserConfigurationException pce) { Print.logError("Parse error: " + pce); return null; } catch (SAXException se) { Print.logError("Parse error: " + se); return null; } catch (IOException ioe) { Print.logError("IO error: " + ioe); return null; } }
/** * Save the XML description of the circuit * * @param output an output stream to write in * @return true if the dump was successful, false either */ public boolean dumpToXml(OutputStream output) { Document doc; Element root; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); doc = builder.newDocument(); } catch (ParserConfigurationException pce) { System.err.println("dumpToXmlFile: unable to write XML save file."); return false; } root = doc.createElement("Circuit"); root.setAttribute("name", this.getName()); for (iterNodes = this.nodes.iterator(); iterNodes.hasNext(); ) iterNodes.next().dumpToXml(doc, root); root.normalize(); doc.appendChild(root); try { TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer transformer = tffactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); transformer.transform(source, result); } catch (TransformerConfigurationException tce) { System.err.println("dumpToXmlFile: Configuration Transformer exception."); return false; } catch (TransformerException te) { System.err.println("dumpToXmlFile: Transformer exception."); return false; } return true; }
public void handlePageBody(PageContext pc) throws ServletException, IOException { ServletContext context = pc.getServletContext(); Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument(); } catch (Exception e) { throw new ServletException(e); } Element rootElem = doc.createElement("xaf"); doc.appendChild(rootElem); try { DatabaseContextFactory.createCatalog(pc, rootElem); transform(pc, doc, ACE_CONFIG_ITEM_PROPBROWSERXSL); } catch (NamingException e) { PrintWriter out = pc.getResponse().getWriter(); out.write(e.toString()); e.printStackTrace(out); } }
// ----------------------------------------------------- // Description: Using the specified template, create // csv records corresponding to the output xml criteria. // ----------------------------------------------------- void transformWithTemplate(String templateName, String fileName, String destinationDirectory) { System.out.println("fileName = " + fileName); String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.')); try { File fXmlFile = new File(templateLocation, templateName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("output"); // loop through all the output nodes for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String outputLine = getTagValue("line", eElement); FileWriter output = new FileWriter( destinationDirectory + File.separator + fileName_woext.substring(0, fileName.lastIndexOf('_')) + ".csv", true); BufferedWriter bufWrite = new BufferedWriter(output); System.out.println( destinationDirectory + File.separator + fileName_woext.substring(0, fileName.lastIndexOf("_")) + ".csv"); if (Settings.optionalIdentifier != null) { bufWrite.write("\"" + Settings.optionalIdentifier + "\""); } String[] outputItems = outputLine.split("#"); for (String outputItem : outputItems) { if ((temp == 0) && (Settings.optionalIdentifier == null)) { bufWrite.write( "\"" + interpretText(destinationDirectory, fileName_woext, outputItem) + "\""); } else { bufWrite.write( ",\"" + interpretText(destinationDirectory, fileName_woext, outputItem) + "\""); } } bufWrite.write("\r\n"); bufWrite.close(); output.close(); } } } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { try { DocumentBuilderFactory db = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = db.newDocumentBuilder(); Document dom = builder.parse("data/geographic-area-data.html"); XPathFactory xpath = XPathFactory.newInstance(); XPath path = xpath.newXPath(); XPathExpression table = path.compile("//div[@id='mw-content-text']/table[contains(@class,'wikitable')]/tr"); NodeList wikiData = (NodeList) table.evaluate(dom, XPathConstants.NODESET); NodeList children; String currentData, cleanData; /* Open output stream */ FileWriter fstream = new FileWriter("data/parsed.yaml"); BufferedWriter out = new BufferedWriter(fstream); for (int i = 0; i < wikiData.getLength(); i++) { if (i == 0) { continue; } out.write(new Integer(i).toString() + ":\n"); children = wikiData.item(i).getChildNodes(); for (int j = 0; j < children.getLength(); j++) { currentData = (String) children.item(j).getTextContent(); switch (j) { case 0: /* Current Data is empty */ break; case 1: cleanData = decompose(currentData).trim().replaceAll("[^a-zA-Z\\s]+", ""); out.write("\t\"Geographic entity\": \"" + cleanData + "\"\n"); break; case 2: /* Current Data is empty */ break; case 3: cleanData = decompose(currentData).trim().replaceAll(",", ""); out.write("\t\"Area\": \"" + cleanData + "\"\n"); break; case 4: /* Current Data is empty */ break; case 5: cleanData = decompose(currentData).trim(); out.write("\t\"Notes\": \"" + cleanData + "\"\n"); break; case 6: /* Current Data is empty */ break; default: /* System.out.println("[" + j + "] Hit default case statement. Current Data is: " + currentData); */ break; } } } /* Close output stream */ out.close(); } catch (Exception e) { System.out.println(e); } }
/** * Construct a factory object, which can be used to get XML documents from DICOM objects. * * @exception ParserConfigurationException */ public XMLRepresentationOfDicomObjectFactory() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); db = dbf.newDocumentBuilder(); }
/** * XML Circuit constructor * * @param file the file that contains the XML description of the circuit * @param g the graphics that will paint the node * @throws CircuitLoadingException if the internal circuit can not be loaded */ public CircuitUI(File file, Graphics g) throws CircuitLoadingException { this(""); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document doc; Element root; Hashtable<Integer, Link> linkstable = new Hashtable<Integer, Link>(); try { builder = factory.newDocumentBuilder(); doc = builder.parse(file); } catch (SAXException sxe) { throw new CircuitLoadingException("SAX exception raised, invalid XML file."); } catch (ParserConfigurationException pce) { throw new CircuitLoadingException( "Parser exception raised, parser configuration is invalid."); } catch (IOException ioe) { throw new CircuitLoadingException("I/O exception, file cannot be loaded."); } root = (Element) doc.getElementsByTagName("Circuit").item(0); this.setName(root.getAttribute("name")); NodeList nl = root.getElementsByTagName("Node"); Element e; Node n; Class cl; for (int i = 0; i < nl.getLength(); ++i) { e = (Element) nl.item(i); try { cl = Class.forName(e.getAttribute("class")); } catch (Exception exc) { System.err.println(exc.getMessage()); throw new RuntimeException("Circuit creation from xml."); } try { n = ((Node) cl.newInstance()); } catch (Exception exc) { System.err.println(exc.getMessage()); throw new RuntimeException("Circuit creation from xml."); } this.nodes.add(n); n.setLocation(new Integer(e.getAttribute("x")), new Integer(e.getAttribute("y"))); if (n instanceof giraffe.ui.Nameable) ((Nameable) n).setNodeName(e.getAttribute("node_name")); if (n instanceof giraffe.ui.CompositeNode) { try { ((CompositeNode) n) .load(new File(file.getParent() + "/" + e.getAttribute("file_name")), g); } catch (Exception exc) { /* try to load from the lib */ ((CompositeNode) n) .load(new File(giraffe.Giraffe.PATH + "/lib/" + e.getAttribute("file_name")), g); } } NodeList nlist = e.getElementsByTagName("Anchor"); Element el; for (int j = 0; j < nlist.getLength(); ++j) { el = (Element) nlist.item(j); Anchor a = n.getAnchor(new Integer(el.getAttribute("id"))); NodeList linklist = el.getElementsByTagName("Link"); Element link; Link l; for (int k = 0; k < linklist.getLength(); ++k) { link = (Element) linklist.item(k); int id = new Integer(link.getAttribute("id")); int index = new Integer(link.getAttribute("index")); if (id >= this.linkID) linkID = id + 1; if (linkstable.containsKey(id)) { l = linkstable.get(id); l.addLinkedAnchorAt(a, index); a.addLink(l); } else { l = new Link(id); l.addLinkedAnchorAt(a, index); this.links.add(l); linkstable.put(id, l); a.addLink(l); } } } } }
// ## operation readReactorOutputFile(ReactionModel) public SystemSnapshot readReactorOutputFile(ReactionModel p_reactionModel) { // #[ operation readReactorOutputFile(ReactionModel) try { // open output file and build the DOM tree String dir = System.getProperty("RMG.workingDirectory"); String filename = "chemkin/reactorOutput.xml"; File inputFile = new File(filename); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); // validate the document with the DTD factory.setIgnoringElementContentWhitespace(true); // ignore whitespace DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(inputFile); // get root element and its children Element root = doc.getDocumentElement(); NodeList rootchildren = root.getChildNodes(); // header is rootchildren.item(0) // get return message and check for successful run Element returnmessageElement = (Element) rootchildren.item(1); Text returnmessageText = (Text) returnmessageElement.getFirstChild(); String returnmessage = returnmessageText.toString(); returnmessage = returnmessage.trim(); if (!returnmessage.contains("SUCCESSFULLY COMPLETED RUN.")) { System.out.println("External reactor model failed!"); System.out.println("Reactor model error message: " + returnmessage); System.exit(0); } // get outputvalues element and its children Element outputvaluesElement = (Element) rootchildren.item(2); NodeList children = outputvaluesElement.getChildNodes(); // get time Element timeElement = (Element) children.item(0); Text timeText = (Text) timeElement.getFirstChild(); double time = Double.parseDouble(timeText.getData()); String timeUnits = timeElement.getAttribute("units"); // get systemstate element and its children Element systemstateElement = (Element) children.item(1); NodeList states = systemstateElement.getChildNodes(); // get temperature and its units Element temperatureElement = (Element) states.item(0); String tempUnits = temperatureElement.getAttribute("units"); Text temperatureText = (Text) temperatureElement.getFirstChild(); double temp = Double.parseDouble(temperatureText.getData()); Temperature T = new Temperature(temp, tempUnits); // get pressure and its units Element pressureElement = (Element) states.item(1); String presUnits = pressureElement.getAttribute("units"); Text pressureText = (Text) pressureElement.getFirstChild(); double pres = Double.parseDouble(pressureText.getData()); Pressure P = new Pressure(pres, presUnits); // get species amounts (e.g. concentrations) ArrayList speciesIDs = new ArrayList(); ArrayList amounts = new ArrayList(); ArrayList fluxes = new ArrayList(); String amountUnits = null; String fluxUnits = null; // loop thru all the species // begin at i=2, since T and P take already the first two position of states int nSpe = (states.getLength() - 2) / 2; int index = 0; LinkedHashMap inertGas = new LinkedHashMap(); for (int i = 2; i < nSpe + 2; i++) { // get amount element and the units Element amountElement = (Element) states.item(i); amountUnits = amountElement.getAttribute("units"); Element fluxElement = (Element) states.item(i + nSpe); fluxUnits = fluxElement.getAttribute("units"); // get speciesid and store in an array list String thisSpeciesID = amountElement.getAttribute("speciesid"); // get amount (e.g. concentraion) and store in an array list Text amountText = (Text) amountElement.getFirstChild(); double thisAmount = Double.parseDouble(amountText.getData()); if (thisAmount < 0) { double aTol = ReactionModelGenerator.getAtol(); // if (Math.abs(thisAmount) < aTol) thisAmount = 0; // else throw new NegativeConcentrationException("Negative concentration in // reactorOutput.xml: " + thisSpeciesID); if (thisAmount < -100.0 * aTol) throw new NegativeConcentrationException( "Species " + thisSpeciesID + " has negative concentration: " + String.valueOf(thisAmount)); } // get amount (e.g. concentraion) and store in an array list Text fluxText = (Text) fluxElement.getFirstChild(); double thisFlux = Double.parseDouble(fluxText.getData()); if (thisSpeciesID.compareToIgnoreCase("N2") == 0 || thisSpeciesID.compareToIgnoreCase("Ne") == 0 || thisSpeciesID.compareToIgnoreCase("Ar") == 0) { inertGas.put(thisSpeciesID, new Double(thisAmount)); } else { speciesIDs.add(index, thisSpeciesID); amounts.add(index, new Double(thisAmount)); fluxes.add(index, new Double(thisFlux)); index++; } } // print results for debugging purposes /** * System.out.println(returnmessage); System.out.println("Temp = " + temp + " " + tempUnits); * System.out.println("Pres = " + pres + " " + presUnits); for (int i = 0; i < amounts.size(); * i++) { System.out.println(speciesIDs.get(i) + " " + amounts.get(i) + " " + amountUnits); } */ ReactionTime rt = new ReactionTime(time, timeUnits); LinkedHashMap speStatus = generateSpeciesStatus(p_reactionModel, speciesIDs, amounts, fluxes); SystemSnapshot ss = new SystemSnapshot(rt, speStatus, T, P); ss.inertGas = inertGas; return ss; } catch (Exception e) { System.out.println("Error reading reactor model output: " + e.getMessage()); System.exit(0); return null; } // #] }
/** @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Model model; String insertstmt; String insertmodel = "", insertspecies = "", insertcompartment = "", insertfunction = "", insertunitdef = "", insertunits = "", insertreaction = "", insertreactant = "", insertproduct = ""; String insertmodifier = "", insertklaw = "", insertrules = "", insertconstraint = "", insertdelay = "", inserttrigger = "", insertevent = "", inserteventassign = "", insertparameter = ""; String insertstatement = ""; String server, user, password, dbname, filepath; String Filedata = ""; String cwd = System.getProperty("user.dir"); if (args.length == 0) { server = "localhost"; user = "******"; password = "******"; dbname = "sbmldb2"; /** * Path to extract the SBML files from database, where cwd is * "github\db2sbml\dbtosbml_standalone_Project\dbtosbml" so add a folder in this directory and * mention folder name instead of extractedbm folder */ filepath = cwd + "\\extractedbm\\"; } else { server = args[0]; user = args[1]; password = args[2]; dbname = args[3]; filepath = args[4]; } try { Filedata = readFileAsString(cwd + "\\sbmldbschema.sql"); } catch (Exception e) { e.printStackTrace(); } Mysqlconn sql = new Mysqlconn(server, user, password, dbname); // String modelids.getId() = "MorrisonAllegra" ; ASTNode math = null; int level = 0, version = 0; ArrayList<modellist> modelidlist = sql.getmodels(); insertstatement = "LOCK TABLES `model` WRITE,`species` WRITE,`compartment` WRITE,`functiondefinition` WRITE,"; insertstatement = insertstatement + "`listofunitdefinitions` WRITE,`listofunits` WRITE,`reaction` WRITE,`simplespeciesreference` WRITE,"; insertstatement = insertstatement + "`modifierspeciesreference` WRITE,`kineticlaw` WRITE,`parameter` WRITE,`sbmlconstraint` WRITE,"; insertstatement = insertstatement + "`event` WRITE,`sbmltrigger` WRITE,`delay` WRITE,`eventassignment` WRITE,`rules` WRITE" + ";"; for (modellist modelids : modelidlist) { ArrayList<modellist> modellevel = sql.getmodeldetails(modelids.getId()); for (modellist modellv : modellevel) { level = modellv.getlevel(); version = modellv.getversion(); } SBMLDocument doc = new SBMLDocument(level, version); ArrayList<modellist> modellists = sql.getmodeldetails(modelids.getId()); if (!modellists.isEmpty()) insertmodel = insertmodel + "\nInsert Into model (id, name,SBML_level,version,notes,annotation) Values"; for (modellist models : modellists) { insertmodel = insertmodel + "(\'" + models.getId() + "\',\'" + models.getName() + "\'," + models.getlevel() + "," + models.getversion() + ",\'" + models.getnotes() + "\',\'" + models.getannotation().toString() + "\'),"; model = doc.createModel(models.getId()); model.setName(models.getName()); // System.out.println("model : " + models.getId()); // model.setNotes(models.getnotes()); // there is some null exception is command line run // but run perfectly from netbeans so ommented out if (!models.getannotation().equals("")) { Annotation annot = new Annotation(models.getannotation().toString()); model.setAnnotation(annot); } doc.setModel(model); } if (!modellists.isEmpty()) { insertmodel = insertmodel.substring(0, insertmodel.length() - 1); insertmodel = insertmodel + ';'; } // insertmodel = insertmodel + "\nUNLOCK TABLES;"; // System.out.println(insertmodel); ArrayList<SpeciesList> specieslist = sql.getspecies(modelids.getId()); if (!specieslist.isEmpty()) insertspecies = insertspecies + "\nInsert Into species (id, name, compartment, initialAmount, initialConcentration,substanceUnits,hasOnlySubstanceUnits,boundaryCondition,constant,conversionFactor,model_id,annotation) Values"; for (SpeciesList species : specieslist) { insertspecies = insertspecies + "(\'" + species.getId() + "\',\'" + species.getName() + "\',\'" + species.getcompartment() + "\'," + species.getia() + "," + species.getic() + ",\'" + species.getsu() + "\'," + species.gethosu() + "," + species.getbc() + "," + species.getconstant() + "," + species.getcf() + ",\'" + modelids.getId() + "\',\'" + species.getannotation() + "\'),"; Species sp = doc.getModel().createSpecies(species.getId()); sp.setName(species.getName()); sp.setCompartment(species.getcompartment()); sp.setConstant(species.getconstant()); sp.setInitialAmount(species.getia()); sp.setInitialConcentration(species.getic()); sp.setHasOnlySubstanceUnits(species.gethosu()); if (doc.getModel().getLevel() == 3) sp.setConversionFactor(species.getcf()); sp.setBoundaryCondition(species.getbc()); sp.setSubstanceUnits(species.getsu()); if (!species.getannotation().equals("")) { Annotation annot = new Annotation(species.getannotation().toString()); sp.setAnnotation(annot); } // doc.getModel().addSpecies(sp) ; } if (!specieslist.isEmpty()) { insertspecies = insertspecies.substring(0, insertspecies.length() - 1); insertspecies = insertspecies + ';'; } ArrayList<CompartmentList> complist = sql.getcompartments(modelids.getId()); if (!complist.isEmpty()) insertcompartment = insertcompartment + "\nInsert Into compartment (id, name,constant,model_id,spacialDimensions,size,units) Values"; for (CompartmentList comp : complist) { insertcompartment = insertcompartment + "(\'" + comp.getId() + "\',\'" + comp.getName() + "\'," + comp.getconstant() + ",\'" + modelids.getId() + "\'," + comp.getspatialdimensions() + "," + comp.getsize() + "," + comp.getunits() + "\'),"; Compartment c = doc.getModel().createCompartment(comp.getId()); c.setName(comp.getName()); c.setConstant(comp.getconstant()); c.setSize(comp.getsize()); c.setSpatialDimensions(comp.getspatialdimensions()); if (comp.getspatialdimensions() != 0) c.setUnits(comp.getunits()); // doc.getModel().addSpecies(sp) ; } if (!complist.isEmpty()) { insertcompartment = insertcompartment.substring(0, insertcompartment.length() - 1); insertcompartment = insertcompartment + ';'; } ArrayList<functionList> funclist = sql.getfunctions(modelids.getId()); if (!funclist.isEmpty()) insertfunction = insertfunction + "\nInsert Into functiondefinition (id, xmlns,model_id) Values"; for (functionList func : funclist) { insertfunction = insertfunction + "(\'" + func.getId() + "\',\'" + func.getxmlns() + "\',\'" + modelids.getId() + "\'),"; FunctionDefinition fd = doc.getModel().createFunctionDefinition(func.getId()); try { math = ASTNode.parseFormula(func.getxmlns()); fd.setMath(math); } catch (Exception e) { e.printStackTrace(); } } if (!funclist.isEmpty()) { insertfunction = insertfunction.substring(0, insertfunction.length() - 1); insertfunction = insertfunction + ';'; } ArrayList<unitList> unitlist = sql.getunitlist(modelids.getId()); if (!unitlist.isEmpty()) insertunitdef = insertunitdef + "\nInsert Into listofunitdefinitions (id,name,model_id) Values"; for (unitList units : unitlist) { insertunitdef = insertunitdef + "(\'" + units.getId() + "\',\'" + units.getName() + "\',\'" + modelids.getId() + "\'),"; UnitDefinition ud = doc.getModel().createUnitDefinition(units.getId()); ud.setName(units.getName()); ArrayList<unitList> unitdeflist = sql.getunitdef(units.getId()); if (!unitdeflist.isEmpty()) insertunits = insertunits + "\nInsert Into listofunits (listofunitdefinitions_id,kind, scale,exponent,multiplier) Values"; for (unitList unitdef : unitdeflist) { insertunits = insertunits + "(\'" + units.getId() + "\',\'" + unitdef.getkind() + "\'," + unitdef.getscale() + "," + unitdef.getexponent() + "," + unitdef.getmultiplier() + "),"; Unit u = ud.createUnit(Unit.Kind.valueOf(unitdef.getkind())); u.setScale(unitdef.getscale()); u.setExponent(unitdef.getexponent()); u.setMultiplier(unitdef.getmultiplier()); } // doc.getModel().addSpecies(sp) ; if (!unitdeflist.isEmpty()) { insertunits = insertunits.substring(0, insertunits.length() - 1); insertunits = insertunits + ';'; } } if (!unitlist.isEmpty()) { insertunitdef = insertunitdef.substring(0, insertunitdef.length() - 1); insertunitdef = insertunitdef + ';'; } ArrayList<reactionList> reactionlist = sql.getreactons(modelids.getId()); if (!reactionlist.isEmpty()) insertreaction = insertreaction + "\nInsert Into reaction (id,name, reversible,fast,model_id,compartment,annotation) Values"; for (reactionList reaction : reactionlist) { insertreaction = insertreaction + "(\'" + reaction.getId() + "\',\'" + reaction.getName() + "\'," + reaction.getreversible() + "," + reaction.getfast() + ",\'" + modelids.getId() + "\',\'" + reaction.getcompartment() + "\',\'" + reaction.getannotation() + "\'),"; Reaction rn = doc.getModel().createReaction(reaction.getId()); rn.setName(reaction.getName()); if (doc.getModel().getLevel() == 3) rn.setCompartment(reaction.getcompartment()); rn.setFast(reaction.getfast()); rn.setReversible(reaction.getreversible()); if (!reaction.getannotation().equals("")) { Annotation annot = new Annotation(reaction.getannotation().toString()); rn.setAnnotation(annot); } ArrayList<reactionList> reactantlist = sql.getreactants(reaction.getId()); if (!reactantlist.isEmpty()) insertreactant = insertreactant + "\nInsert Into simplespeciesreference (reaction_id,species, sboTerm,stoichiometry,speciestype,constant) Values"; for (reactionList reactant : reactantlist) { insertreactant = insertreactant + "(\'" + reaction.getId() + "\',\'" + reactant.getspecies() + "\',\'" + reactant.getsboTerm() + "\'," + reactant.getstoichometry() + "," + reactant.getconstant() + ",\'reactants\'),"; SpeciesReference rt = new SpeciesReference(); rt.setName(reactant.getspecies()); rt.setSpecies(reactant.getspecies()); // rt.setSBOTerm(reactant.getsboTerm()); rt.setStoichiometry(reactant.getstoichometry()); // rt.setConstant(reactant.getconstant()); rn.addReactant(rt); } if (!reactantlist.isEmpty()) { insertreactant = insertreactant.substring(0, insertreactant.length() - 1); insertreactant = insertreactant + ';'; } ArrayList<reactionList> productlist = sql.getproducts(reaction.getId()); if (!productlist.isEmpty()) insertproduct = insertproduct + "\nInsert Into simplespeciesreference (reaction_id,species, sboTerm,stoichiometry,constant,speciestype) Values"; for (reactionList product : productlist) { insertproduct = insertproduct + "(\'" + reaction.getId() + "\',\'" + product.getspecies() + "\',\'" + product.getsboTerm() + "\'," + product.getstoichometry() + "," + product.getconstant() + ",\'products\'),"; SpeciesReference pr = new SpeciesReference(); pr.setName(product.getspecies()); pr.setSpecies(product.getspecies()); // pr.setSBOTerm(product.getsboTerm()); pr.setStoichiometry(product.getstoichometry()); // pr.setConstant(product.getconstant()); rn.addProduct(pr); } if (!productlist.isEmpty()) { insertproduct = insertproduct.substring(0, insertproduct.length() - 1); insertproduct = insertproduct + ';'; } ArrayList<reactionList> modifierlist = sql.getmodifiers(reaction.getId()); if (!modifierlist.isEmpty()) insertmodifier = insertmodifier + "\nInsert Into modifierspeciesreference (reaction_id,species, sboTerm,speciestype) Values"; for (reactionList modifier : modifierlist) { insertmodifier = insertmodifier + "(\'" + reaction.getId() + "\',\'" + modifier.getspecies() + "\',\'" + modifier.getsboTerm() + "\',\'modifiers\'),"; ModifierSpeciesReference m = new ModifierSpeciesReference(); m.setName(modifier.getspecies()); m.setSpecies(modifier.getspecies()); // m.setSBOTerm(modifier.getsboTerm()); rn.addModifier(m); } if (!modifierlist.isEmpty()) { insertmodifier = insertmodifier.substring(0, insertmodifier.length() - 1); insertmodifier = insertmodifier + ';'; } ArrayList<reactionList> klawlist = sql.getkineticlaws(reaction.getId()); if (!klawlist.isEmpty()) insertklaw = insertklaw + "\nInsert Into kineticlaw (reaction_id,kid, math,annotation) Values"; for (reactionList klaw : klawlist) { insertklaw = insertklaw + "(\'" + reaction.getId() + "\',\'" + klaw.getId() + "\',\'" + klaw.getmath() + "\',\'" + klaw.getannotation() + "\'),"; KineticLaw kl = rn.createKineticLaw(); try { math = ASTNode.parseFormula(klaw.getmath()); kl.setMath(math); if (!klaw.getannotation().equals("")) { Annotation annot = new Annotation(klaw.getannotation().toString()); kl.setAnnotation(annot); } } catch (Exception e) { e.printStackTrace(); } } if (!klawlist.isEmpty()) { insertklaw = insertklaw.substring(0, insertklaw.length() - 1); insertklaw = insertklaw + ';'; } } if (!reactionlist.isEmpty()) { insertreaction = insertreaction.substring(0, insertreaction.length() - 1); insertreaction = insertreaction + ';'; } ArrayList<parameterList> paralist = sql.getparameters(modelids.getId()); if (!paralist.isEmpty()) insertparameter = insertparameter + "\nInsert Into parameter (id,name,value,units,constant,model_id) Values"; for (parameterList para : paralist) { insertparameter = insertparameter + "(\'" + para.getId() + "\',\'" + para.getName() + "\'," + para.getvalue() + "," + para.getunits() + "," + para.getconstant() + ",\'" + modelids.getId() + "\'),"; Parameter par = doc.getModel().createParameter(para.getId()); par.setName(para.getId()); par.setConstant(para.getconstant()); par.setUnits(para.getunits()); par.setValue(para.getvalue()); } if (!paralist.isEmpty()) { insertparameter = insertparameter.substring(0, insertparameter.length() - 1); insertparameter = insertparameter + ';'; } ArrayList<constraintList> conslist = sql.getconstraints(modelids.getId()); if (!conslist.isEmpty()) insertconstraint = insertconstraint + "\nInsert Into sbmlconstraint (math,message,model_id) Values"; for (constraintList constraint : conslist) { insertconstraint = insertconstraint + "(\'" + constraint.getmath() + "\',\'" + constraint.getmessage() + "\',\'" + modelids.getId() + "\'),"; Constraint cons = doc.getModel().createConstraint(); try { math = ASTNode.parseFormula(constraint.getmath()); cons.setMath(math); cons.setMessage(constraint.getmessage()); } catch (Exception e) { e.printStackTrace(); } } if (!conslist.isEmpty()) { insertconstraint = insertconstraint.substring(0, insertconstraint.length() - 1); insertconstraint = insertconstraint + ';'; } ArrayList<eventsList> eventlist = sql.getevents(modelids.getId()); if (!eventlist.isEmpty()) insertevent = insertevent + "\nInsert Into event (id,name,UseValuesFromTriggerTime,model_id) Values"; for (eventsList events : eventlist) { insertevent = insertevent + "(\'" + events.getId() + "\',\'" + events.getName() + "\'," + events.getuservalues() + ",\'" + modelids.getId() + "\'),"; Event ev = doc.getModel().createEvent(events.getId()); ev.setName(events.getName()); // ev.setUseValuesFromTriggerTime(events.getuservalues()); ArrayList<eventsList> triggerlist = sql.gettriggers(events.getId()); if (!triggerlist.isEmpty()) inserttrigger = inserttrigger + "\nInsert Into sbmltrigger (event_id,initialvalue,persisent,math) Values"; for (eventsList triggers : triggerlist) { Trigger tr = doc.getModel().createTrigger(); try { math = ASTNode.parseFormula(triggers.getmath()); tr.setMath(math); tr.setInitialValue(triggers.getinitialval()); tr.setPersistent(triggers.getpersistent()); } catch (Exception e) { e.printStackTrace(); } } if (!triggerlist.isEmpty()) { inserttrigger = inserttrigger.substring(0, insertmodel.length() - 1); inserttrigger = inserttrigger + ';'; } ArrayList<eventsList> delaylist = sql.getdelays(events.getId()); if (!delaylist.isEmpty()) insertdelay = insertdelay + "\nInsert Into delay (event_id,math) Values"; for (eventsList delays : delaylist) { Delay d = doc.getModel().createDelay(); try { math = ASTNode.parseFormula(delays.getmath()); d.setMath(math); } catch (Exception e) { e.printStackTrace(); } } if (!delaylist.isEmpty()) { insertdelay = insertdelay.substring(0, insertdelay.length() - 1); insertdelay = insertdelay + ';'; } ArrayList<eventsList> evasslist = sql.geteventassignments(events.getId()); if (!evasslist.isEmpty()) inserteventassign = inserteventassign + "\nInsert Into eventassignment (event_id,variable,math) Values"; for (eventsList evassign : evasslist) { EventAssignment ea = doc.getModel().createEventAssignment(); try { math = ASTNode.parseFormula(evassign.getmath()); ea.setMath(math); } catch (Exception e) { e.printStackTrace(); } } if (!evasslist.isEmpty()) { inserteventassign = inserteventassign.substring(0, inserteventassign.length() - 1); inserteventassign = inserteventassign + ';'; } } if (!eventlist.isEmpty()) { insertevent = insertevent.substring(0, insertevent.length() - 1); insertevent = insertevent + ';'; } ArrayList<ruleslist> rulelist = sql.getrules(modelids.getId()); if (!rulelist.isEmpty()) insertrules = insertrules + "\nInsert Into rules (id,math,ruletype,model_id) Values"; for (ruleslist rules : rulelist) { insertrules = insertrules + "(\'" + rules.getId() + "\',\'" + rules.getmath() + "\',\'" + rules.getruletype() + "\',\'" + modelids.getId() + "\'),"; if (rules.getruletype().equals("assignmentrule")) { Rule r = doc.getModel().createAssignmentRule(); r.setMetaId(rules.getId()); try { math = ASTNode.parseFormula(rules.getmath()); r.setMath(math); } catch (Exception e) { e.printStackTrace(); } } } if (!rulelist.isEmpty()) { insertrules = insertrules.substring(0, insertrules.length() - 1); insertrules = insertrules + ';'; } SBMLWriter writer = new SBMLWriter(); try { String Path = filepath + modelids.getId() + ".xml"; writer.write(doc, Path); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(Path); Element root = document.getDocumentElement(); Element newdataset = document.createElement("dataset"); root.appendChild(newdataset); ArrayList<dataset> datasetlist = sql.getdataset(modelids.getId()); for (dataset ds : datasetlist) { // System.out.println(ds.getexpcond()); Element name = document.createElement("experimentalcondition"); name.setAttribute("bioelement", ds.getbioel()); name.setAttribute("name", ds.getName()); name.setAttribute("descr", ds.getdescr()); name.setAttribute("expcond", ds.getexpcond()); name.setAttribute("value", String.valueOf(ds.getvalue())); name.setAttribute("type", ds.gettype()); name.setAttribute("uri", ds.geturi()); newdataset.appendChild(name); } root.appendChild(newdataset); DOMSource source = new DOMSource(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StreamResult result = new StreamResult(filepath + modelids.getId() + "d.xml"); transformer.transform(source, result); System.out.println( "Files : " + modelids.getId() + ".xml and " + modelids.getId() + "d.xml have been generated successfully !!!"); } catch (Exception e) { e.printStackTrace(); } insertstatement = insertstatement + "\n\n" + insertmodel + "\n" + insertspecies + "\n" + insertcompartment + "\n" + insertfunction; insertstatement = insertstatement + "\n" + insertparameter + "\n" + insertreaction + "\n" + insertreactant + "\n" + insertproduct; insertstatement = insertstatement + "\n" + insertmodifier + "\n" + insertklaw + "\n" + insertunitdef + "\n" + insertunits; insertstatement = insertstatement + "\n" + insertrules + "\n" + insertconstraint + "\n" + insertevent + "\n" + inserttrigger + "\n" + insertdelay + "\n" + inserteventassign; insertcompartment = ""; insertmodel = ""; insertspecies = ""; // System.out.println("document : " + doc); } insertstatement = insertstatement + "\nUNLOCK TABLES;"; Filedata = Filedata + "\n\n\n" + insertstatement; try { wrtireStringToFile(Filedata, filepath + "sbmldb.sql"); } catch (IOException e) { e.printStackTrace(); } // System.out.println(insertstatement); }
public static MiningResult importFile(InputStream input) throws IOException { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc; // NodeList netNodes; dbf.setValidating(false); dbf.setIgnoringComments(true); dbf.setIgnoringElementContentWhitespace(true); // dbf.setExpandEntityReferences(false); // dbf.setNamespaceAware(false); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { if (systemId.indexOf("ARIS-Export") != -1) { return new InputSource("file:" + About.EXTLIBLOCATION() + "ARIS-Export101.dtd"); } else { return null; } } }); InputSource inpStream = new InputSource(input); inpStream.setSystemId("file:" + System.getProperty("user.dir", "")); doc = db.parse(inpStream); // check if root element is a aml tag Message.add("parsing done" + doc, Message.DEBUG); if (!(doc.getDocumentElement().getNodeName().equals("AML"))) { Message.add("aml tag not found", Message.ERROR); throw new Exception("aml tag not found"); } else { Message.add("aml root element found"); } EPCResult result = new EPCResult(null, (EPC) null); HashMap ObjDef_LinkId = new HashMap(); HashMap modelid_net = new HashMap(); HashMap ObjDef_Name = new HashMap(); HashMap function_LinkId = new HashMap(); HashMap ModelId_ModelType = new HashMap(); traverseAMLforObjectNames( ObjDef_Name, doc.getDocumentElement(), ObjDef_LinkId, ModelId_ModelType); Iterator findLinkToEpc = ObjDef_LinkId.keySet().iterator(); while (findLinkToEpc.hasNext()) { String currentObjDef = (String) findLinkToEpc.next(); String Links = (String) ObjDef_LinkId.get(currentObjDef); StringTokenizer linkSet = new StringTokenizer(Links); String realEpcLink = ""; while (linkSet.hasMoreTokens()) { String currentLink = linkSet.nextToken(); if (ModelId_ModelType.get(currentLink).equals("MT_EEPC")) { realEpcLink = currentLink; break; } } if (realEpcLink.equals(" ")) { ObjDef_LinkId.remove(currentObjDef); } else { ObjDef_LinkId.put(currentObjDef, realEpcLink); } } result = traverseAML( result, doc.getDocumentElement(), null, ObjDef_Name, ObjDef_LinkId, modelid_net, function_LinkId); Iterator hierarchicalFunctions = function_LinkId.keySet().iterator(); while (hierarchicalFunctions.hasNext()) { EPCSubstFunction f = (EPCSubstFunction) hierarchicalFunctions.next(); f.setSubstitutedEPC((EPC) modelid_net.get(function_LinkId.get(f))); // Message.add(f.getSubstitutedEPC().getName()); } return result; } catch (Throwable x) { Message.add(x.toString()); throw new IOException(x.getMessage()); } }
protected Document parse(InputStream inputStream) throws LibraryException { Map<Integer, SongBean> songMap = new HashMap<Integer, SongBean>(); Map<Integer, AlbumBean> albumMap = new HashMap<Integer, AlbumBean>(); Map<Integer, AlbumBean> secondaryAlbumMap = new HashMap<Integer, AlbumBean>(); warningList.clear(); try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); // Create the builder and parse the file DocumentBuilder builder = factory.newDocumentBuilder(); // Set an error listener and parse the document builder.setErrorHandler(new iTradeTunesLibraryErrorHandler()); builder.setEntityResolver(new iTradeTunesLibraryResolver()); Document document = builder.parse(inputStream); synchronized (libraryList) { // Query the library document and build the library list XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(XPATH_LIBRARY_LIST); NodeList nodelist = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); // Process the elements in the nodelist SongBean song = null; for (int i = 0; i < nodelist.getLength(); i++) { boolean isTrackID = false; // Get element and child nodes Element elem = (Element) nodelist.item(i); NodeList list = elem.getChildNodes(); // Get node value Node childKey = list.item(0); String key = childKey.getNodeValue(); // Check if we have to create a new bean if (SongBean.NAME_TRACK_ID.equals(key)) { isTrackID = true; SongBean previousSong = song; song = new SongBean(); if (previousSong != null && !("AAC audio file".equals(previousSong.getKind()) || "MPEG audio file".equals(previousSong.getKind()))) { songMap.remove(previousSong.getTrack_ID()); } else { // Add an album bean addOrUpdateAlbum(albumMap, previousSong, false); } } // The first parameter is the key String prop = childKey.getNodeValue().replace(' ', '_'); // The second parameter is the value i++; // Get element and child nodes elem = (Element) nodelist.item(i); // Check for boolean properties Object value = null; // Get node value list = elem.getChildNodes(); childKey = list.item(0); value = (childKey == null) ? elem.getNodeName() : childKey.getNodeValue(); if (isTrackID) { isTrackID = false; } // Set the property of the song bean Statement stmt = new Statement(song, "set" + prop, new Object[] {value}); try { stmt.execute(); } catch (Exception e) { // Ignore that field, we do not have it in our bean } // If the property is the track ID, add the song to the hash // map if (SongBean.NAME_TRACK_ID.equals(key)) { int trackID = Integer.valueOf((String) value); songMap.put(trackID, song); } } // Update album for last song addOrUpdateAlbum(albumMap, song, false); // Check the album map for inconsistencies Iterator<AlbumBean> albums = albumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // Add an inconsistent album only using the album title SongBean[] songs = album.getSongs(); for (int i = 0; i < songs.length; i++) { addOrUpdateAlbum(secondaryAlbumMap, songs[i], true); } } } // Check secondary album map for consistency albums = secondaryAlbumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // This album cannot be matched // TODO: Add to warning message } } setChanged(); } return document; } catch (IOException ioe) { // Log an expected connect exception throw new LibraryException(ioe); } catch (SAXException se) { // Catch all other exceptions throw new LibraryException(se); } catch (ParserConfigurationException pce) { // Catch all other exceptions Utils.logSevere(pce); throw new LibraryException(pce); } catch (XPathExpressionException xpe) { // Catch all other exceptions Utils.logSevere(xpe); throw new LibraryException(xpe); } catch (NumberFormatException nfe) { // Catch all other exceptions throw new LibraryException(nfe); } }
// ----------------------------------------------------- // Description: Using the specified template, extract // the template sections into PNG and TXT corresponding // parts. // ----------------------------------------------------- void extractWithTemplate(String templateName, String fileName, String destinationDirectory) { // from template int intOriginX = 0; int intOriginY = 0; // from template int intX = 0; int intY = 0; int intWidth = 0; int intHeight = 0; // calculated int viaTemplateX = 0; int viaTemplateY = 0; int viaTemplatePlusWidth = 0; int viaTemplatePlusHeight = 0; System.out.println("fileName = " + fileName); String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.')); String pathPlusFileName_woext = destinationDirectory + File.separator + fileName_woext + File.separator + fileName_woext; System.out.println("pathPlusFileName_woext = " + pathPlusFileName_woext); try { File fXmlFile = new File(templateLocation, templateName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("section"); Node nNode; for (int temp = 0; temp < nList.getLength(); temp++) { nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String myX = getTagValue("x", eElement); String myY = getTagValue("y", eElement); String myWidth = getTagValue("width", eElement); String myHeight = getTagValue("height", eElement); String mySuffix = getTagValue("suffix", eElement); intX = Integer.parseInt(myX); intY = Integer.parseInt(myY); intWidth = Integer.parseInt(myWidth); intHeight = Integer.parseInt(myHeight); viaTemplateX = intX - (intOriginX - pageX); viaTemplateY = intY - (intOriginY - pageY); viaTemplatePlusWidth = viaTemplateX + intWidth; viaTemplatePlusHeight = viaTemplateY + intHeight; Spawn.execute( Settings.Programs.CONVERT.path(), pathPlusFileName_woext + "_trim.png", "-crop", String.format("%dx%d+%d+%d", intWidth, intHeight, viaTemplateX, viaTemplateY), "+repage", pathPlusFileName_woext + "_" + mySuffix + ".png"); Spawn.execute( Settings.Programs.CONVERT.path(), pathPlusFileName_woext + "_trim.png", "-fill", "none", "-stroke", "red", "-strokewidth", "3", "-draw", String.format( "rectangle %d,%d %d,%d", viaTemplateX, viaTemplateY, viaTemplatePlusWidth, viaTemplatePlusHeight), "+repage", pathPlusFileName_woext + "_draw.png"); Spawn.execute( Settings.Programs.TESSERACT.path(), pathPlusFileName_woext + "_" + mySuffix + ".png", pathPlusFileName_woext + "_" + mySuffix); } } } catch (Exception e) { e.printStackTrace(); } }
// ----------------------------------------------------- // Description: Determine if a template exists for the // given file and return true on success. // ----------------------------------------------------- boolean templateExistFor(String fileName, String destinationDirectory) { templateIdentified = false; // from template int intOriginX = 0; int intOriginY = 0; // from template int intX = 0; int intY = 0; int intWidth = 0; int intHeight = 0; // calculated int viaTemplateX = 0; int viaTemplateY = 0; int viaTemplatePlusWidth = 0; int viaTemplatePlusHeight = 0; String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.')); String pathPlusFileName_woext = destinationDirectory + File.separator + fileName_woext + File.separator + fileName_woext; try { File folder = new File(templateLocation); File[] listOfFiles = folder.listFiles(); long startTime = System.currentTimeMillis(); for (File file : listOfFiles) { if (file.isFile()) { templateName = ""; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("condition"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String myX = getTagValue("x", eElement); String myY = getTagValue("y", eElement); String myWidth = getTagValue("width", eElement); String myHeight = getTagValue("height", eElement); String mySuffix = getTagValue("suffix", eElement); String myRequirements = getTagValue("required", eElement); intX = Integer.parseInt(myX); intY = Integer.parseInt(myY); intWidth = Integer.parseInt(myWidth); intHeight = Integer.parseInt(myHeight); viaTemplateX = intX - (intOriginX - pageX); viaTemplateY = intY - (intOriginY - pageY); viaTemplatePlusWidth = viaTemplateX + intWidth; viaTemplatePlusHeight = viaTemplateY + intHeight; Spawn.execute( Settings.Programs.CONVERT.path(), pathPlusFileName_woext + "_trim.png", "-crop", String.format("%dx%d+%d+%d", intWidth, intHeight, viaTemplateX, viaTemplateY), "+repage", pathPlusFileName_woext + "_" + mySuffix + ".png"); Spawn.execute( Settings.Programs.CONVERT.path(), pathPlusFileName_woext + "_trim.png", "-fill", "none", "-stroke", "red", "-strokewidth", "3", "-draw", String.format( "rectangle %d,%d %d,%d", viaTemplateX, viaTemplateY, viaTemplatePlusWidth, viaTemplatePlusHeight), "+repage", pathPlusFileName_woext + "_draw.png"); Spawn.execute( Settings.Programs.TESSERACT.path(), pathPlusFileName_woext + "_" + mySuffix + ".png", pathPlusFileName_woext + "_" + mySuffix); String line = ""; // String that holds current file line String accumulate = ""; try { FileReader input = new FileReader(pathPlusFileName_woext + "_" + mySuffix + ".txt"); BufferedReader bufRead = new BufferedReader(input); line = bufRead.readLine(); while (line != null) { accumulate += line; line = bufRead.readLine(); } bufRead.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } String[] requirements = myRequirements.split("#"); for (String requirement : requirements) { if (requirement.equals(accumulate.trim())) { templateName = file.getName(); templateIdentified = true; return templateIdentified; } } } } } } // record end time long endTime = System.currentTimeMillis(); System.out.println( "Template Search [" + fileName + "] " + (endTime - startTime) / 1000 + " seconds"); } catch (Exception e) { e.printStackTrace(); } return templateIdentified; }
@Override public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException { HashSet<ScoredAnnotation> res; try { res = new HashSet<ScoredAnnotation>(); lastTime = Calendar.getInstance().getTimeInMillis(); URL wikiApi = new URL(url); String parameters = "references=true&repeatMode=all&minProbability=0.0&source=" + URLEncoder.encode(text, "UTF-8"); HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection(); slConnection.setRequestProperty("accept", "text/xml"); slConnection.setDoOutput(true); slConnection.setDoInput(true); slConnection.setRequestMethod("POST"); slConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); slConnection.setRequestProperty("charset", "utf-8"); slConnection.setRequestProperty( "Content-Length", "" + Integer.toString(parameters.getBytes().length)); slConnection.setUseCaches(false); DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream()); wr.writeBytes(parameters); wr.flush(); wr.close(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(slConnection.getInputStream()); /* URL wikiApi = new URL(url+"?references=true&repeatMode=all&minProbability=0.0&source="+URLEncoder.encode(text, "UTF-8")); URLConnection wikiConnection = wikiApi.openConnection(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(wikiConnection.getInputStream()); */ lastTime = Calendar.getInstance().getTimeInMillis() - lastTime; XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression idExpr = xpath.compile("//detectedTopic/@id"); XPathExpression weightExpr = xpath.compile("//detectedTopic/@weight"); XPathExpression referenceExpr = xpath.compile("//detectedTopic/references"); NodeList ids = (NodeList) idExpr.evaluate(doc, XPathConstants.NODESET); NodeList weights = (NodeList) weightExpr.evaluate(doc, XPathConstants.NODESET); NodeList references = (NodeList) referenceExpr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < weights.getLength(); i++) { if (weights.item(i).getNodeType() != Node.TEXT_NODE) { int id = Integer.parseInt(ids.item(i).getNodeValue()); float weight = Float.parseFloat(weights.item(i).getNodeValue()); // System.out.println("ID="+ids.item(i).getNodeValue()+" weight="+weight); XPathExpression startExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@start"); XPathExpression endExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@end"); NodeList starts = (NodeList) startExpr.evaluate(references.item(i), XPathConstants.NODESET); NodeList ends = (NodeList) endExpr.evaluate(references.item(i), XPathConstants.NODESET); for (int j = 0; j < starts.getLength(); j++) { int start = Integer.parseInt(starts.item(j).getNodeValue()); int end = Integer.parseInt(ends.item(j).getNodeValue()); int len = end - start; res.add(new ScoredAnnotation(start, len, id, weight)); } } } } catch (Exception e) { e.printStackTrace(); throw new AnnotationException( "An error occurred while querying Wikipedia Miner API. Message: " + e.getMessage()); } return res; }
/** Forward to real factory, set error handler */ public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilder db = fact.newDocumentBuilder(); log.debug3("Created builder: " + db); db.setErrorHandler(new MyErrorHandler()); return db; }
public void CollectData(String link) { try { // Creating an empty XML Document DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); int flag = 0; // create the root element and add it to the document Element movie = doc.createElement("movie"); doc.appendChild(movie); movie.setAttribute("id", String.valueOf(n)); n++; // create sub elements Element genres = doc.createElement("genres"); Element actors = doc.createElement("actors"); Element reviews = doc.createElement("reviews"); URL movieUrl = new URL(link); URL reviewsURL = new URL(link + "reviews/#type=top_critics"); BufferedWriter bw3 = new BufferedWriter(new FileWriter("movies.xml", true)); int count = -1; String auth = ""; BufferedReader br3 = new BufferedReader(new InputStreamReader(movieUrl.openStream())); String str2 = ""; String info = ""; while (null != (str2 = br3.readLine())) { // start reading the html document if (str2.isEmpty()) continue; if (count == 14) break; if (count == 12) { if (!str2.contains("<h3>Cast</h3>")) continue; else count++; } if (count == 13) { if (str2.contains(">ADVERTISEMENT</p>")) { count++; movie.appendChild(actors); continue; } else { if (str2.contains("itemprop=\"name\">")) { Element actor = doc.createElement("actor"); actors.appendChild(actor); Text text = doc.createTextNode(Jsoup.parse(str2.toString()).text()); actor.appendChild(text); } else continue; } } if (count <= 11) { switch (count) { case -1: { if (!str2.contains("property=\"og:image\"")) continue; else { Pattern image = Pattern.compile("http://.*.jpg", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher match = image.matcher(str2); while (match.find()) { Element imageLink = doc.createElement("imageLink"); movie.appendChild(imageLink); Text text = doc.createTextNode(match.group()); imageLink.appendChild(text); count++; } } break; } case 0: { if (str2.contains("<title>")) { Element name = doc.createElement("name"); movie.appendChild(name); Text text = doc.createTextNode( Jsoup.parse(str2.toString().replace(" - Rotten Tomatoes", "")).text()); name.appendChild(text); count++; } break; } case 1: { if (!str2.contains("itemprop=\"ratingValue\"")) break; else { Element score = doc.createElement("score"); movie.appendChild(score); Text text = doc.createTextNode(Jsoup.parse(str2.toString()).text()); score.appendChild(text); count++; } break; } case 2: { if (!str2.contains("itemprop=\"description\">")) continue; else count++; break; } case 3: { if (!str2.contains("itemprop=\"duration\"")) info = info.concat(str2); else { Element MovieInfo = doc.createElement("MovieInfo"); movie.appendChild(MovieInfo); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); MovieInfo.appendChild(text); info = str2; count++; } break; } case 4: { if (!str2.contains("itemprop=\"genre\"")) info = info.concat(str2); else { Element duration = doc.createElement("duration"); movie.appendChild(duration); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); duration.appendChild(text); info = str2; count++; } break; } case 5: { if (info.contains("itemprop=\"genre\"")) { Element genre = doc.createElement("genre"); genres.appendChild(genre); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); genre.appendChild(text); info = ""; } if (str2.contains(">Directed By:<")) { count++; movie.appendChild(genres); continue; } else { if (str2.contains("itemprop=\"genre\"")) { Element genre = doc.createElement("genre"); genres.appendChild(genre); Text text = doc.createTextNode(Jsoup.parse(str2.toString()).text()); genre.appendChild(text); } else continue; } break; } case 6: { if (!str2.contains(">Written By:<")) { if (str2.contains(">In Theaters:<")) { Element director = doc.createElement("director"); movie.appendChild(director); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("Directed By: ", "")).text()); director.appendChild(text); info = str2; count += 2; break; } info = info.concat(str2); } else { Element director = doc.createElement("director"); movie.appendChild(director); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("Directed By: ", "")).text()); director.appendChild(text); info = ""; count++; } break; } case 7: { if (!str2.contains(">In Theaters:<")) { if (str2.contains(">On DVD:<")) { Element writer = doc.createElement("writer"); movie.appendChild(writer); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); writer.appendChild(text); info = str2; count += 2; break; } info = info.concat(str2); } else { Element writer = doc.createElement("writer"); movie.appendChild(writer); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); writer.appendChild(text); info = str2; count++; } break; } case 8: { if (!str2.contains(">On DVD:<")) info = info.concat(str2); else { Element TheatreRelease = doc.createElement("TheatreRelease"); movie.appendChild(TheatreRelease); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("In Theaters:", "")).text()); TheatreRelease.appendChild(text); info = str2; count++; } break; } case 9: { if (!str2.contains(">US Box Office:<")) { if (str2.contains("itemprop=\"productionCompany\"")) { Element DvdRelease = doc.createElement("DvdRelease"); movie.appendChild(DvdRelease); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("On DVD:", "")).text()); DvdRelease.appendChild(text); info = str2; count += 2; break; } info = info.concat(str2); } else { Element DvdRelease = doc.createElement("DvdRelease"); movie.appendChild(DvdRelease); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("On DVD:", "")).text()); DvdRelease.appendChild(text); info = str2; count++; } break; } case 10: { if (!str2.contains("itemprop=\"productionCompany\"")) info = info.concat(str2); else { Element BOCollection = doc.createElement("BOCollection"); movie.appendChild(BOCollection); Text text = doc.createTextNode( Jsoup.parse(info.toString().replace("US Box Office:", "")).text()); BOCollection.appendChild(text); info = str2; count++; } break; } case 11: { if (!str2.contains(">Official Site")) info = info.concat(str2); else { Element Production = doc.createElement("Production"); movie.appendChild(Production); Text text = doc.createTextNode(Jsoup.parse(info.toString()).text()); Production.appendChild(text); info = str2; count++; } break; } default: break; } } } BufferedReader br4 = new BufferedReader(new InputStreamReader(reviewsURL.openStream())); String str3 = ""; String info2 = ""; int count2 = 0; while (null != (str3 = br4.readLine())) { if (count2 == 0) { if (!str3.contains("<div class=\"reviewsnippet\">")) continue; else count2++; } if (count2 == 1) { if (!str3.contains("<p class=\"small subtle\">")) info2 = info2.concat(str3); else { Element review = doc.createElement("review"); reviews.appendChild(review); Text text = doc.createTextNode(Jsoup.parse(info2.toString()).text()); review.appendChild(text); info2 = ""; count2 = 0; } } } movie.appendChild(reviews); TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); String xmlString = sw.toString(); bw3.write(xmlString); br3.close(); br4.close(); bw3.close(); } catch (Exception ex) { ex.printStackTrace(); } }