private static Annotation toAnnotation(String xml) throws IOException { Element docElem; try { Builder parser = new Builder(); StringReader in = new StringReader(xml); docElem = parser.build(in).getRootElement(); } catch (ParsingException | IOException e) { throw new RuntimeException(String.format("error:\n%s\ninput:\n%s", e, xml)); Element textElem = docElem.getFirstChildElement("TEXT"); StringBuilder text = new StringBuilder(); int offset = 0; List<CoreMap> sentences = new ArrayList<>(); Elements sentenceElements = textElem.getChildElements("SENT"); for (int crtsent = 0; crtsent < sentenceElements.size(); crtsent ++){ Element sentElem = sentenceElements.get(crtsent); CoreMap sentence = new ArrayCoreMap(); sentence.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset); Tree tree = Tree.valueOf(sentElem.getChild(0).getValue()); // XXX ms: is this the same as sentElem.getText() in JDOM? List<CoreLabel> tokens = new ArrayList<>(); List<Tree> preTerminals = preTerminals(tree);
public static void writeRequestCollectionXML(final List<Request> requests, final File f) throws IOException, XMLException { XmlPersistenceWrite xUtl = new XmlPersistenceWrite(); Element eRoot = new Element("request-collection"); eRoot.addAttribute(new Attribute("version", Versions.CURRENT)); for(Request req: requests) { Element e = xUtl.getRequestElement(req); eRoot.appendChild(e); } Document doc = new Document(eRoot); xUtl.writeXML(doc, f); }
@Override public String convert(Document original) { return original.toXML(); } }
public static Element getRootElement(File file) throws ParsingException, IOException { Builder builder = new Builder(); Document document = builder.build(file); return document.getRootElement(); }
public void beforeParsing(Document document) { nu.xom.Element html = document.getRootElement(); nu.xom.Element head = html.getFirstChildElement("head"); Check.notNull(head, "<head> section is missing from document"); script = new nu.xom.Element("script"); script.addAttribute(new Attribute("type", "text/javascript")); // Fix for Issue #26: Strict XHTML DTD requires an explicit end tag for <script> element // Thanks to Matthias Schwegler for reporting and supplying a fix for this. script.appendChild(""); head.appendChild(script); }
ProcessingInstruction processingInstruction = new ProcessingInstruction("xml-stylesheet", "href=\"http://www.w3.org/2000/08/w3c-synd/style.css\" type=\"text/css\""); Element rdfElement = new Element("rdf:RDF", NS_RDF); rdfElement.addNamespaceDeclaration("", NS_RSS10); rdfElement.addNamespaceDeclaration("mn", "http://usefulinc.com/rss/manifest/"); rdfElement.addNamespaceDeclaration("dc", NS_DCELEMENTS); rdfElement.addNamespaceDeclaration("cml", Convertor.NS_CML); Document doc = new Document(rdfElement); doc.insertChild(processingInstruction, 0); Element channelElement = new Element("channel", NS_RSS10); Element titleElement = new Element("title", NS_RSS10); seqElement.appendChild(imageElement2); writer.write(doc.toXML()); writer.flush(); } catch (IOException ex) {
Element rootNode = doc.getRootElement(); if (!"rest-client".equals(rootNode.getQualifiedName())) { throw new XMLException("Root node is not <rest-client>"); Versions.versionValidCheck(rootNode.getAttributeValue("version")); if (rootNode.getChildElements().size() != 1) { throw new XMLException("There can be only one child node for root node: <response>");
void updateProperty(Document pom, String propertyName, String newVersion) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query("//mvn:properties", context); if (nodes.size() > 0) { final Element propertiesElement = (Element) nodes.get(0); Elements properties = propertiesElement.getChildElements(); for (int i = 0; i < properties.size(); i++) { Element property = properties.get(i); if (property.getLocalName().equals(propertyName)) { Element newRange = new Element(propertyName, "http://maven.apache.org/POM/4.0.0"); newRange.appendChild(newVersion); propertiesElement.replaceChild(property, newRange); } } } }
public People(String fileName) throws Exception { Document doc = new Builder().build(new File(fileName)); Elements elements = doc.getRootElement().getChildElements(); for(int i = 0; i < elements.size(); i++) add(new APerson(elements.get(i))); } public static void
public static List<Request> getRequestCollectionFromXMLFile(final File f) throws IOException, XMLException { XmlPersistenceRead xUtlRead = new XmlPersistenceRead(); List<Request> out = new ArrayList<>(); Document doc = xUtlRead.getDocumentFromFile(f); Element eRoot = doc.getRootElement(); if(!"request-collection".equals(eRoot.getLocalName())) { throw new XMLException("Expecting root element <request-collection>, but found: " + eRoot.getLocalName()); } final String version = eRoot.getAttributeValue("version"); try { Versions.versionValidCheck(version); } catch(Versions.VersionValidationException ex) { throw new XMLException(ex); } xUtlRead.setReadVersion(version); Elements eRequests = doc.getRootElement().getChildElements(); for(int i=0; i<eRequests.size(); i++) { Element eRequest = eRequests.get(i); Request req = xUtlRead.getRequestBean(eRequest); out.add(req); } return out; }
protected Request xml2Request(final Document doc) throws MalformedURLException, XMLException { // get the rootNode Element rootNode = doc.getRootElement(); if (!"rest-client".equals(rootNode.getQualifiedName())) { throw new XMLException("Root node is not <rest-client>"); } // checking correct rest version final String rcVersion = rootNode.getAttributeValue("version"); try { Versions.versionValidCheck(rcVersion); } catch(Versions.VersionValidationException ex) { throw new XMLException(ex); } readVersion = rcVersion; // if more than two request element is present then throw the exception if (rootNode.getChildElements().size() != 1) { throw new XMLException("There can be only one child node for root node: <request>"); } // minimum one request element is present in xml if (rootNode.getFirstChildElement("request") == null) { throw new XMLException("The child node of <rest-client> should be <request>"); } Element requestNode = rootNode.getFirstChildElement("request"); return getRequestBean(requestNode); }
/** Wraps each item in the result sequence into a decorated element wrapper. */ private static Document wrapSequence(Nodes nodes) { // make a copy of the template for sequences: Element items = (Element) TEMPLATES.get(Nodes.class.getName()); items = new Element(items); int size = nodes.size(); for (int i=0; i < size; i++) { items.appendChild(wrap(nodes.get(i))); } return new Document(items); }
public static Element stringToXml(String xml) { if(xml == null) return null; try { Document doc = PARSER.build(new StringReader(xml)); Element elm = doc.getRootElement(); return (Element) elm.copy(); } catch (Exception e) { throw new RuntimeException(e); } }
@Test // See Issue 26 public void xmlOutputContainsAnExplicitEndTagForScriptElement() { JavaScriptLinker javaScriptLinker = new JavaScriptLinker(NOT_NEEDED_PARAMETER); Element html = new Element("html"); Element head = new Element("head"); html.appendChild(head); javaScriptLinker.beforeParsing(new Document(html)); assertEquals("<head><script type=\"text/javascript\"></script></head>", head.toXML()); } }
@Override protected void appendCommentToDocument(String comment) throws SAXException { try { Element root = document.getRootElement(); if ("http://www.xom.nu/fakeRoot".equals(root.getNamespaceURI())) { document.insertChild(nodeFactory.makeComment(comment), document.indexOf(root)); } else { document.appendChild(nodeFactory.makeComment(comment)); } } catch (XMLException e) { fatal(e); } }
void bumpMajorVersion(Document pom) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes project = pom.query("/mvn:project", context); if (project.size() == 0) throw new MojoExecutionException("Pom is broken"); Nodes nodes = pom.query("/mvn:project/mvn:version", context); if (nodes.size() != 1) throw new MojoExecutionException("Version is not declared correctly"); Element e = (Element) nodes.get(0); String[] components = e.getValue().split("\\."); components[0] = Integer.toString(Integer.valueOf(components[0]) + 1); String bumpedVersion = String.join(".", components); Element newVersion = new Element("version", "http://maven.apache.org/POM/4.0.0"); newVersion.appendChild(bumpedVersion); ((Element) project.get(0)).replaceChild(e, newVersion); // TODO check that the next version does not already exist }
public static ParsedBroadcastMessage createFromPlaintext(String plaintextBroadcastMessage, String languageCode, IdentityStore identityStore, long timeCreated){ Element rootElement = new Element(TrConstants.FormatInfo.ROOT); Document broadcastMessageDocument = new Document(rootElement); //For checking purposes, implementation should be changed. broadcastMessageDocument.setBaseURI(""); Element plainText = generatePlainTextElement(plaintextBroadcastMessage, identityStore, languageCode); rootElement.appendChild(plainText); return new ParsedBroadcastMessage(broadcastMessageDocument, timeCreated); }
/** * @since 1.4 */ public XomReader(Document document, NameCoder nameCoder) { super(document.getRootElement(), nameCoder); }
protected Document request2XML(final Request bean) throws XMLException { Element reqRootElement = getRootElement(); reqRootElement.appendChild(getRequestElement(bean)); Document xomDocument = new Document(reqRootElement); return xomDocument; }