private static void toXml(XmlWriter w, Resource resource) throws IOException { w.element(RepositoryParser.RESOURCE) .attribute(Resource.ID, resource.getId()) .attribute(Resource.SYMBOLIC_NAME, resource.getSymbolicName()) .attribute(Resource.PRESENTATION_NAME, resource.getPresentationName()) .attribute(Resource.URI, getRelativeUri(resource, Resource.URI)) .attribute(Resource.VERSION, resource.getVersion().toString()); w.textElement(Resource.DESCRIPTION, resource.getProperties().get(Resource.DESCRIPTION)) .textElement(Resource.SIZE, resource.getProperties().get(Resource.SIZE)) .textElement( Resource.DOCUMENTATION_URI, getRelativeUri(resource, Resource.DOCUMENTATION_URI)) .textElement(Resource.SOURCE_URI, getRelativeUri(resource, Resource.SOURCE_URI)) .textElement(Resource.JAVADOC_URI, getRelativeUri(resource, Resource.JAVADOC_URI)) .textElement(Resource.LICENSE_URI, getRelativeUri(resource, Resource.LICENSE_URI)); String[] categories = resource.getCategories(); for (int i = 0; categories != null && i < categories.length; i++) { w.element(RepositoryParser.CATEGORY).attribute(RepositoryParser.ID, categories[i]).end(); } Capability[] capabilities = resource.getCapabilities(); for (int i = 0; capabilities != null && i < capabilities.length; i++) { toXml(w, capabilities[i]); } Requirement[] requirements = resource.getRequirements(); for (int i = 0; requirements != null && i < requirements.length; i++) { toXml(w, requirements[i]); } w.end(); }
public static Element ranking_Genes(Document doc, IndividualStat is, int number) { Element genes = doc.createElement(Consts.XML_TAG_GENES); doc.getElementsByTagName(Consts.DATA_ROOT).item(0).appendChild(genes); float[] scores = null; Integer[] index; if (number > Symbols.length) number = Symbols.length; if (is != null) { index = is.get_Order(); scores = is.get_GeneScores(Symbols.length - 1, 0); for (int i = Symbols.length - 1; i >= Symbols.length - number; i--) { Element gene = doc.createElement(Consts.XML_TAG_GENE); gene.setAttribute(Consts.XML_TAG_ID, Symbols[index[i]]); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_CHROMOSOME, ChrList[Chrs[index[i]]]); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_FROM, Integer.toString(Starts[index[i]] + 1)); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_TO, Integer.toString(Ends[index[i]])); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_SCORE, Float.toString(Math.round(scores[index[i]] * 10) / 10)); genes.appendChild(gene); } } return genes; }
public static Element overlap_Genes( Document doc, String chr, int start, int end, IndividualStat is) { Element genes = doc.createElement(Consts.XML_TAG_GENES); doc.getElementsByTagName(Consts.DATA_ROOT).item(0).appendChild(genes); if (ChrMap.containsKey(chr)) { int[] range = binarySearchOverlap(ChrMap.get(chr), start, end); if (range != null) { float[] scores = null; if (is != null) scores = is.get_GeneScores(range[1], range[0]); for (int i = range[0]; i <= range[1]; i++) { Element gene = doc.createElement(Consts.XML_TAG_GENE); gene.setAttribute(Consts.XML_TAG_ID, Symbols[i]); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_FROM, Integer.toString(Starts[i] + 1)); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_TO, Integer.toString(Ends[i])); if (is != null) XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_SCORE, Float.toString(Math.round(scores[i - range[0]] * 10) / 10)); genes.appendChild(gene); } } } return genes; }
public static Element find_Gene(Document doc, String prefix) { prefix = prefix.toUpperCase(); if (prefix.matches("C[XY0-9]+ORF")) { prefix = prefix.replaceFirst("ORF", "orf"); } else if (prefix.matches("C[XY0-9]+OR")) { prefix = prefix.replaceFirst("OR", "or"); } else if (prefix.matches("^C[XY0-9]+O")) { prefix = prefix.replaceFirst("O", "o"); } Element genes = doc.createElement(Consts.XML_TAG_GENES); doc.getElementsByTagName(Consts.DATA_ROOT).item(0).appendChild(genes); int match = binarySearchPrefix(prefix); if (match >= 0) { for (int i = match; i < match + 5; i++) { if (i >= Symbols_sorted.length || !Symbols_sorted[i].startsWith(prefix)) return genes; else { Element gene = doc.createElement(Consts.XML_TAG_GENE); gene.setAttribute(Consts.XML_TAG_ID, Symbols_sorted[i]); int index = SymbolMap.get(Symbols_sorted[i]); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_CHROMOSOME, ChrList[Chrs[index]]); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_FROM, Integer.toString(Starts[index] + 1)); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_TO, Integer.toString(Ends[index])); genes.appendChild(gene); } } } return genes; }
private static void toXml(XmlWriter w, Capability capability) throws IOException { w.element(RepositoryParser.CAPABILITY).attribute(RepositoryParser.NAME, capability.getName()); Property[] props = capability.getProperties(); for (int j = 0; props != null && j < props.length; j++) { toXml(w, props[j]); } w.end(); }
private static void write(XmlWriter xmlWriter, XmlElementHandler xmlElementHandler) throws Exception { while (xmlElementHandler.hasNext()) { xmlWriter.writeStartElement(xmlElementHandler.getTagName()); write(xmlWriter, xmlElementHandler.handleNext(xmlWriter)); xmlWriter.writeEndElement(); } }
public static Element gene_Info(Document doc, String symbol) { Element genes = doc.createElement(Consts.XML_TAG_GENES); doc.getElementsByTagName(Consts.DATA_ROOT).item(0).appendChild(genes); if (SymbolMap.containsKey(symbol)) { int index = SymbolMap.get(symbol); Element gene = doc.createElement(Consts.XML_TAG_GENE); gene.setAttribute(Consts.XML_TAG_ID, Symbols[index]); genes.appendChild(gene); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_CHROMOSOME, ChrList[Chrs[index]]); XmlWriter.append_text_element( doc, gene, Consts.XML_TAG_FROM, Integer.toString(Starts[index] + 1)); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_TO, Integer.toString(Ends[index])); if (!RefSeqs[index].equals("")) XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_REFSEQ, RefSeqs[index]); if (!UCSCs[index].equals("")) XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_UCSC, UCSCs[index]); if (!Ensembls[index].equals("")) XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_ENSEMBL, Ensembls[index]); if (!Entrezs[index].equals("")) XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_ENTREZ, Entrezs[index]); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_HGNC, HGNCs[index]); XmlWriter.append_text_element(doc, gene, Consts.XML_TAG_NAME, Names[index]); } return genes; }
private void serializeValue(final HttpServletResponse pResponse, Object value) throws TransformerException, IOException, FactoryConfigurationError { if (value instanceof Source) { setContentType(pResponse, "application/binary"); // Unknown content type Sources.writeToStream((Source) value, pResponse.getOutputStream()); } else if (value instanceof Node) { pResponse.setContentType("text/xml"); Sources.writeToStream(new DOMSource((Node) value), pResponse.getOutputStream()); } else if (value instanceof XmlSerializable) { pResponse.setContentType("text/xml"); XMLOutputFactory factory = XMLOutputFactory.newInstance(); factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); try { XmlWriter out = XmlStreaming.newWriter( pResponse.getOutputStream(), pResponse.getCharacterEncoding(), true); try { out.startDocument(null, null, null); ((XmlSerializable) value).serialize(out); out.endDocument(); } finally { out.close(); } } catch (XmlException e) { throw new TransformerException(e); } } else if (value instanceof Collection) { final XmlElementWrapper annotation = getElementWrapper(); if (annotation != null) { setContentType(pResponse, "text/xml"); try (OutputStream outStream = pResponse.getOutputStream()) { writeCollection( outStream, getGenericReturnType(), (Collection<?>) value, getQName(annotation)); } } } else if (value instanceof CharSequence) { setContentType(pResponse, "text/plain"); pResponse.getWriter().append((CharSequence) value); } else { if (value != null) { try { final JAXBContext jaxbContext = JAXBContext.newInstance(getReturnType()); setContentType(pResponse, "text/xml"); final JAXBSource jaxbSource = new JAXBSource(jaxbContext, value); Sources.writeToStream(jaxbSource, pResponse.getOutputStream()); } catch (final JAXBException e) { throw new MessagingException(e); } } } }
public void testSimpleXml() { OutputStream byteOut = new ByteArrayOutputStream(); XmlWriter writer = new XmlWriter(byteOut); writer.start("templates"); writer.start("template"); writer.addAttribute("name", "reset"); writer.end(); writer.end(); String xml = "<templates>" + NL + " <template name=\"reset\"/>" + NL + "</templates>" + NL; assertEquals(xml, byteOut.toString()); }
private static void saveUserListWithoutLock() throws TransformerConfigurationException, SAXException, IOException, SearchLibException { ConfigFileRotation cfr = configFiles.get(StartStopListener.OPENSEARCHSERVER_DATA_FILE, "users.xml"); try { XmlWriter xmlWriter = new XmlWriter(cfr.getTempPrintWriter("UTF-8"), "UTF-8"); getUserList().writeXml(xmlWriter); xmlWriter.endDocument(); cfr.rotate(); } finally { cfr.abort(); } }
private static void toXml(XmlWriter w, Property property) throws IOException { w.element(RepositoryParser.P) .attribute(RepositoryParser.N, property.getName()) .attribute(RepositoryParser.T, property.getType()) .attribute(RepositoryParser.V, property.getValue()) .end(); }
public static void main(String argv[]) { File file = new File("xml1.xml"); XmlReader reader; XmlWriter writer; XmlDocument document; String xmlout; // Construct a document and parse it System.out.println("We are going to parse from " + file + "."); System.out.println("The parser will be set to verbose mode\n"); document = new XmlDocument(file); reader = new XmlReader(); reader.setVerbose(true); try { reader.parse(document); } catch (Exception e) { e.printStackTrace(); } int errors = reader.getErrorCount(); int warnings = reader.getWarningCount(); System.err.println("Completed: " + errors + " errors, " + warnings + " warnings"); if (errors > 0) { System.err.println("Unrecoverable errors in XML. Stop."); return; } // Now print it System.out.println("\nHaving parsed the document, we will print it out."); System.out.println("Here is the DTD:\n"); writer = new XmlWriter(); Writer out = new OutputStreamWriter(System.out); try { writer.writeDTD(document, out); } catch (Exception e) { e.printStackTrace(); } System.out.println("\nHere is the XML:\n"); try { writer.write(document, out); } catch (Exception e) { e.printStackTrace(); } }
private static void toXml(XmlWriter w, Requirement requirement) throws IOException { w.element(RepositoryParser.REQUIRE) .attribute(RepositoryParser.NAME, requirement.getName()) .attribute(RepositoryParser.FILTER, requirement.getFilter()) .attribute(RepositoryParser.EXTEND, Boolean.toString(requirement.isExtend())) .attribute(RepositoryParser.MULTIPLE, Boolean.toString(requirement.isMultiple())) .attribute(RepositoryParser.OPTIONAL, Boolean.toString(requirement.isOptional())) .text(requirement.getComment().trim()) .end(); }
public void write(XmlVisitor xa) { ElementVisitor elementVisitor = xa.visitElement(name); // Attributes if (attributes != null) { for (Entry<String, TextWriter> entry : attributes.entrySet()) { String attrName = entry.getKey(); TextWriter textWriter = entry.getValue(); String value = textWriter.getText(); if (value != null) elementVisitor.visitAttribute(attrName, value); } } // Body if (body != null && !body.isEmpty()) { XmlVisitor bodyVisitor = elementVisitor.visitBody(); body.write(bodyVisitor); bodyVisitor.visitEndBody(); } elementVisitor.visitEndElement(); }
private static void toXml(XmlWriter w, Repository repository) throws IOException { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.SSS"); w.element(RepositoryParser.REPOSITORY) .attribute(RepositoryParser.NAME, repository.getName()) .attribute( RepositoryParser.LASTMODIFIED, format.format(new Date(repository.getLastModified()))); if (repository instanceof RepositoryImpl) { Referral[] referrals = ((RepositoryImpl) repository).getReferrals(); for (int i = 0; referrals != null && i < referrals.length; i++) { w.element(RepositoryParser.REFERRAL) .attribute(RepositoryParser.DEPTH, new Integer(referrals[i].getDepth())) .attribute(RepositoryParser.URL, referrals[i].getUrl()) .end(); } } Resource[] resources = repository.getResources(); for (int i = 0; resources != null && i < resources.length; i++) { toXml(w, resources[i]); } w.end(); }
public void execute(XmlDoc.Element args, Inputs in, Outputs out, XmlWriter w) throws Throwable { /* * get the cid argument value */ String id = args.value("cid"); /* * build the args for om.pssd.object.describe service. */ XmlDocMaker dm = new XmlDocMaker("args"); dm.add("id", id); /* * call om.pssd.object.describe service. */ XmlDoc.Element r = executor().execute("om.pssd.object.describe", dm.root()); /* * return the result. */ w.add(r.element("object")); }
public void serialize(Writer writer, BenchmarkDataset round) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element roundElement = doc.createElement("object"); doc.appendChild(roundElement); addTextChild(roundElement, "date", round.getProperties().getProperty("create.date")); addTextChild(roundElement, "label", round.getProperties().getProperty("label")); addTextChild( roundElement, "configuration", round.getProperties().getProperty("configuration")); addProperties(roundElement, round.getProperties(), "label", "create.date", "configuration"); Element benchmarksElement = addChild(roundElement, "benchmarkruns"); benchmarksElement.setAttribute("type", "list"); for (BenchmarkRun benchmarkRun : round.getBenchmarkRuns()) { Element runElement = addChild(benchmarksElement, "object"); addTextChild(runElement, "label", benchmarkRun.getProperties().getProperty("label")); addTextChild( runElement, "iterations", benchmarkRun.getProperties().getProperty("iterations")); addTextChild( runElement, "warmupIterations", benchmarkRun.getProperties().getProperty("warmupIterations")); addProperties( runElement, benchmarkRun.getProperties(), "label", "warmupIterations", "iterations"); Element gcSamplesElement = addChild(runElement, "gcSamples"); gcSamplesElement.setAttribute("type", "list"); Element memoryUsageSamplesElement = addChild(runElement, "memorySamples"); memoryUsageSamplesElement.setAttribute("type", "list"); Element timeSamplesElement = addChild(runElement, "timeSamples"); timeSamplesElement.setAttribute("type", "list"); for (BenchmarkRunIteration iteration : benchmarkRun.getIterations()) { Properties iterationProperties = iteration.getProperties(); if (iterationProperties.containsKey("gc.collection.count.post")) { Element s = addChild(gcSamplesElement, "object"); addTextChild(s, "label", "pre"); addTextChild( s, "count", String.valueOf(iterationProperties.getProperty("gc.collection.count.pre"))) .setAttribute("type", "integer"); long nanoseconds = (long) 1e6 * Long.parseLong( iterationProperties.getProperty("gc.collection.time.milli.pre")); addTextChild(s, "nanoseconds", String.valueOf(nanoseconds)); s = addChild(gcSamplesElement, "object"); addTextChild(s, "label", "post"); addTextChild( s, "count", String.valueOf(iterationProperties.getProperty("gc.collection.count.post"))) .setAttribute("type", "integer"); nanoseconds = (long) 1e6 * Long.parseLong( iterationProperties.getProperty("gc.collection.time.milli.post")); addTextChild(s, "nanoseconds", String.valueOf(nanoseconds)); } if (iterationProperties.containsKey("nanoseconds.post")) { Element s = addChild(timeSamplesElement, "object"); addTextChild(s, "label", "pre"); long nanoseconds = Long.parseLong(iterationProperties.getProperty("nanoseconds.pre")); addTextChild(s, "nanoseconds", String.valueOf(nanoseconds)); s = addChild(timeSamplesElement, "object"); addTextChild(s, "label", "post"); nanoseconds = Long.parseLong(iterationProperties.getProperty("nanoseconds.post")); addTextChild(s, "nanoseconds", String.valueOf(nanoseconds)); } for (Entry<Object, Object> entry : iteration.getProperties().entrySet()) { String key = (String) entry.getKey(); if (key.equals("heap.allocation")) { String value = (String) entry.getValue(); Element s = addChild(memoryUsageSamplesElement, "object"); addTextChild(s, "label", "memoryAllocation"); addTextChild(s, "bytes", value); } } } } XmlWriter.Write(writer, doc); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
public void write(XmlVisitor xa) { for (XmlWriter child : children) child.write(xa); }