List of usage examples for org.dom4j Element add
void add(Namespace namespace);
Namespace
to this element. From source file:hello.SampleSimpleApplication.java
License:Apache License
private void addAutoTile(Document domFromStream) { DOMElement autoTileElement1 = (DOMElement) domFromStream .selectSingleNode("/html/body//div[@id='page1-div']"); Element autoTileElement = autoTileElement1; if (autoTileElement != null) { autoTileElement.attribute("id").setValue("auto_tile_" + autoTileNr); changeImgUrl(autoTileElement);/*from w w w.j a va 2 s . c o m*/ addBreadcrumbBefore(autoTileElement1); cleanSymbols(autoTileElement); Element detach = (Element) autoTileElement.detach(); autoDocBody.add(detach); } else { //audi Element autoTileElementFromStream = (Element) domFromStream .selectSingleNode("/html/body/div/table//td[div/h2]"); changeImgUrl(autoTileElementFromStream); /* Element autoTileNameElement = (Element) autoTileElement2.selectSingleNode("div/h2"); autoTileNameElement.setText(autoTileName); * */ /* * */ List<Element> breadcrumOld = autoTileElementFromStream.selectNodes("div/h3"); for (Element element : breadcrumOld) { element.detach(); } autoTileElement = autoDocBody.addElement("div"); autoTileElement.addAttribute("id", "auto_tile_" + autoTileNr); addBreadcrumb(autoTileElement); cleanSymbols(autoTileElementFromStream); for (Iterator iterator = autoTileElementFromStream.elementIterator(); iterator.hasNext();) { Element element = (Element) iterator.next(); autoTileElement.add(element.detach()); } } /* neccesary * */ }
From source file:hk.hku.cecid.piazza.commons.util.PropertyTree.java
License:Open Source License
/** * Appends a property sheet to this property tree. * The specified property sheet can only be appended if it is of the PropertyTree type. * /*www .j a v a2 s . c om*/ * @param p the property sheet to be appended. * @return true if the operation is successful. false otherwise. * @see hk.hku.cecid.piazza.commons.util.PropertySheet#append(hk.hku.cecid.piazza.commons.util.PropertySheet) */ public boolean append(PropertySheet p) { Document dom2; if (p instanceof PropertyTree) { dom2 = ((PropertyTree) p).getDOM(); } else { return false; } Element rootElement = dom.getRootElement(); Element rootElement2 = dom2.getRootElement(); if (rootElement2 == null) { return true; } if (rootElement == null) { rootElement2.detach(); dom.setRootElement(rootElement2); return true; } else { String rootElementName = rootElement.getName(); String rootElementName2 = rootElement2.getName(); if (rootElementName.equals(rootElementName2)) { Iterator elements = rootElement2.elements().iterator(); while (elements.hasNext()) { Element element = (Element) elements.next(); element.detach(); rootElement.add(element); } return true; } else { return false; } } }
From source file:hudson.model.Api.java
License:Open Source License
/** * Exposes the bean as XML.//www .ja v a2 s. c o m */ public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter String xpath, @QueryParameter String wrapper, @QueryParameter String tree, @QueryParameter int depth) throws IOException, ServletException { setHeaders(rsp); String[] excludes = req.getParameterValues("exclude"); if (xpath == null && excludes == null) { // serve the whole thing rsp.serveExposedBean(req, bean, Flavor.XML); return; } StringWriter sw = new StringWriter(); // first write to String Model p = MODEL_BUILDER.get(bean.getClass()); TreePruner pruner = (tree != null) ? new NamedPathPruner(tree) : new ByDepth(1 - depth); p.writeTo(bean, pruner, Flavor.XML.createDataWriter(bean, sw)); // apply XPath FilteredFunctionContext functionContext = new FilteredFunctionContext(); Object result; try { Document dom = new SAXReader().read(new StringReader(sw.toString())); // apply exclusions if (excludes != null) { for (String exclude : excludes) { XPath xExclude = dom.createXPath(exclude); xExclude.setFunctionContext(functionContext); List<org.dom4j.Node> list = (List<org.dom4j.Node>) xExclude.selectNodes(dom); for (org.dom4j.Node n : list) { Element parent = n.getParent(); if (parent != null) parent.remove(n); } } } if (xpath == null) { result = dom; } else { XPath comp = dom.createXPath(xpath); comp.setFunctionContext(functionContext); List list = comp.selectNodes(dom); if (wrapper != null) { Element root = DocumentFactory.getInstance().createElement(wrapper); for (Object o : list) { if (o instanceof String) { root.addText(o.toString()); } else { root.add(((org.dom4j.Node) o).detach()); } } result = root; } else if (list.isEmpty()) { rsp.setStatus(HttpServletResponse.SC_NOT_FOUND); rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath)); return; } else if (list.size() > 1) { rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size())); return; } else { result = list.get(0); } } } catch (DocumentException e) { LOGGER.log(Level.FINER, "Failed to do XPath/wrapper handling. XML is as follows:" + sw, e); throw new IOException("Failed to do XPath/wrapper handling. Turn on FINER logging to view XML.", e); } if (isSimpleOutput(result) && !permit(req)) { // simple output prohibited rsp.sendError(HttpURLConnection.HTTP_FORBIDDEN, "primitive XPath result sets forbidden; implement jenkins.security.SecureRequester"); return; } // switch to gzipped output OutputStream o = rsp.getCompressedOutputStream(req); try { if (isSimpleOutput(result)) { // simple output allowed rsp.setContentType("text/plain;charset=UTF-8"); String text = result instanceof CharacterData ? ((CharacterData) result).getText() : result.toString(); o.write(text.getBytes("UTF-8")); return; } // otherwise XML rsp.setContentType("application/xml;charset=UTF-8"); new XMLWriter(o).write(result); } finally { o.close(); } }
From source file:ie.cmrc.smtx.base.serialisation.rdfxml.RDFXMLSerialiser.java
License:Apache License
/** * Creates an RDF document containing the XML representation of the provided * resource//from w ww . jav a 2 s . co m * @param resource An {@link RDFXMLisable} instance * @param elementSet Specifies the level of information to serialise * (see {@link ElementSetName} for more details) * @param language Language code. Only annotations in this language will * be included in the XML. * @return XML document */ public static Document makeRDFXMLDocument(RDFXMLisable resource, ElementSetName elementSet, String language) { Element rdfElt = DocumentHelper.createElement(new QName("RDF", Namespaces.RDF)); rdfElt.add(Namespaces.RDF); rdfElt.add(Namespaces.RDFS); rdfElt.add(Namespaces.OWL); rdfElt.add(Namespaces.SKOS); rdfElt.add(Namespaces.XSD); if (resource != null) { Element xmlElement = resource.toXMLElement(elementSet, language); if (xmlElement != null) rdfElt.add(xmlElement); } Document doc = DocumentHelper.createDocument(rdfElt); return doc; }
From source file:ie.cmrc.smtx.base.serialisation.rdfxml.RDFXMLSerialiser.java
License:Apache License
/** * Creates an RDF document containing the XML representation of the provided * resources// w w w . j a v a 2 s.c om * @param resources A collection of {@link RDFXMLisable} instances * @param elementSet Specifies the level of information to serialise * (see {@link ElementSetName} for more details) * @param language Language code. Only annotations in this language will * be included in the XML. * @return XML document */ public static Document makeRDFXMLDocument(Collection<? extends RDFXMLisable> resources, ElementSetName elementSet, String language) { Element rdfElt = DocumentHelper.createElement(new QName("RDF", Namespaces.RDF)); rdfElt.add(Namespaces.RDF); rdfElt.add(Namespaces.RDFS); rdfElt.add(Namespaces.OWL); rdfElt.add(Namespaces.SKOS); rdfElt.add(Namespaces.XSD); if (resources != null) { for (RDFXMLisable resource : resources) { if (resource != null) { Element xmlElement = resource.toXMLElement(elementSet, language); if (xmlElement != null) rdfElt.add(xmlElement); } } } Document doc = DocumentHelper.createDocument(rdfElt); return doc; }
From source file:ie.cmrc.smtx.base.serialisation.rdfxml.RDFXMLSerialiser.java
License:Apache License
/** * Creates an RDF document containing the XML representation of the provided * resources//from www .j a v a 2 s .co m * @param resources An iterator over {@link RDFXMLisable} instances * @param elementSet Specifies the level of information to serialise * (see {@link ElementSetName} for more details) * @param language Language code. Only annotations in this language will * be included in the XML. * @return XML document */ public static Document makeRDFXMLDocument(Iterator<? extends RDFXMLisable> resources, ElementSetName elementSet, String language) { Element rdfElt = DocumentHelper.createElement(new QName("RDF", Namespaces.RDF)); rdfElt.add(Namespaces.RDF); rdfElt.add(Namespaces.RDFS); rdfElt.add(Namespaces.OWL); rdfElt.add(Namespaces.SKOS); rdfElt.add(Namespaces.XSD); if (resources != null) { while (resources.hasNext()) { RDFXMLisable resource = resources.next(); if (resource != null) { Element xmlElement = resource.toXMLElement(elementSet, language); if (xmlElement != null) rdfElt.add(xmlElement); } } } Document doc = DocumentHelper.createDocument(rdfElt); return doc; }
From source file:ie.cmrc.smtx.skos.index.lucene.DocSemanticEntityWrapper.java
License:Apache License
/** * {@inheritDoc}//from w ww . ja v a 2s.c o m * @param elementSet {@inheritDoc} * @param language {@inheritDoc} * @return {@inheritDoc} */ @Override public Element toXMLElement(ElementSetName elementSet, String language) { ElementSetName esn = elementSet; if (esn == null) esn = ElementSetName.BRIEF; if (this.getURI() != null && !this.getURI().isEmpty()) { Element resourceElt; resourceElt = DocumentHelper.createElement(new QName(CONCEPT, Namespaces.SKOS)); QName aboutQN = new QName("about", Namespaces.RDF); QName resourceQN = new QName("resource", Namespaces.RDF); resourceElt.addAttribute(aboutQN, this.getURI()); if (esn.compareTo(ElementSetName.BRIEF) >= 0) { QName xmlLang = new QName("lang", Namespaces.XML); List<Element> prefLabelElts = this.getXMLPrefLabels(language); for (Element prefLabelElt : prefLabelElts) resourceElt.add(prefLabelElt); } return resourceElt; } else return null; }
From source file:ie.cmrc.smtx.skos.model.AbstractSKOSResource.java
License:Apache License
/** * {@inheritDoc}//ww w . j a va2 s . com * @param elementSet {@inheritDoc} * @param language {@inheritDoc} * @return {@inheritDoc} */ @Override public Element toXMLElement(ElementSetName elementSet, String language) { ElementSetName esn = elementSet; if (esn == null) esn = ElementSetName.BRIEF; if (this.getURI() != null && !this.getURI().isEmpty()) { Element resourceElt; resourceElt = DocumentHelper.createElement(new QName(this.getSkosType().name(), Namespaces.SKOS)); QName aboutQN = new QName("about", Namespaces.RDF); QName resourceQN = new QName("resource", Namespaces.RDF); resourceElt.addAttribute(aboutQN, this.getURI()); if (esn.compareTo(ElementSetName.BRIEF) >= 0) { QName xmlLang = new QName("lang", Namespaces.XML); List<Element> prefLabelElts = this.getXMLAnnotations(SKOSAnnotationProperty.prefLabel, language); for (Element prefLabelElt : prefLabelElts) resourceElt.add(prefLabelElt); if (esn.compareTo(ElementSetName.SUMMARY) >= 0) { QName inSchemeQN = new QName(SKOSElementProperty.inScheme.name(), Namespaces.SKOS); QName conceptSchemeQN = new QName(SKOSType.ConceptScheme.name(), Namespaces.SKOS); CloseableIterator<SKOSResource> inSchemeIter = this.listRelations(SKOSElementProperty.inScheme); while (inSchemeIter.hasNext()) { SKOSResource cs = inSchemeIter.next(); //resourceElt.addElement(inSchemeQN).addAttribute(resourceQN, cs.getURI()); resourceElt.addElement(inSchemeQN).addElement(conceptSchemeQN).addAttribute(aboutQN, cs.getURI()); } inSchemeIter.close(); List<Element> defElts = this.getXMLAnnotations(SKOSAnnotationProperty.definition, language); for (Element defElt : defElts) resourceElt.add(defElt); if (esn.compareTo(ElementSetName.FULL) >= 0) { List<Element> altLabelElts = this.getXMLAnnotations(SKOSAnnotationProperty.altLabel, language); for (Element altLabelElt : altLabelElts) resourceElt.add(altLabelElt); if (esn.compareTo(ElementSetName.EXTENDED) >= 0) { QName topConceptOfQN = new QName(SKOSElementProperty.topConceptOf.name(), Namespaces.SKOS); CloseableIterator<SKOSResource> topConceptOf = this .listRelations(SKOSElementProperty.topConceptOf); while (topConceptOf.hasNext()) { SKOSResource cs = topConceptOf.next(); //resourceElt.addElement(inSchemeQN).addAttribute(resourceQN, cs.getURI()); resourceElt.addElement(topConceptOfQN).addElement(conceptSchemeQN) .addAttribute(aboutQN, cs.getURI()); } topConceptOf.close(); for (SKOSAnnotationProperty property : SKOSAnnotationProperty.values()) { if (property != SKOSAnnotationProperty.prefLabel && property != SKOSAnnotationProperty.altLabel && property != SKOSAnnotationProperty.definition) { List<Element> annotationElts = this.getXMLAnnotations(property, language); for (Element annotationElt : annotationElts) resourceElt.add(annotationElt); } } } } } } return resourceElt; } else return null; }
From source file:ie.cmrc.smtx.skos.model.hierarchy.DefaultSKOSConceptNode.java
License:Apache License
/** * {@inheritDoc}//www . j a v a 2s. co m * @param elementSet {@inheritDoc} * @param language {@inheritDoc} * @return {@inheritDoc} */ @Override public Element toXMLElement(ElementSetName elementSet, String language) { if (this.concept != null) { Element elt = this.concept.toXMLElement(elementSet, language); if (elt != null && this.hasChildren()) { for (SKOSConceptNode child : this.children) { if (child != null) { Element childNodeElt = child.toXMLElement(elementSet, language); if (childNodeElt != null) { Element narrower = elt .addElement(new QName(SKOSSemanticProperty.narrower.name(), Namespaces.SKOS)); narrower.add(childNodeElt); } } } } return elt; } return null; }
From source file:ie.cmrc.smtx.sws.exceptions.SWSExceptionReport.java
License:Apache License
public Document getXML() { if (!this.isEmpty()) { Namespace sws = new Namespace("sws", swsURI); Namespace xml = new Namespace("xml", xmlURI); //Namespace xsi = new Namespace("xsi", xsiURI); QName exReportQN = new QName("ExceptionReport", sws); Element exceptionReport = DocumentHelper.createElement(exReportQN); exceptionReport.add(xml); exceptionReport.add(sws);/* ww w.j a v a 2s . c om*/ //exceptionReport.add(xsi); //QName schemaLocQN = new QName ("schemaLocation", xsi); //exceptionReport.addAttribute(schemaLocQN, this.xsiSchameLocation); //Language if (this.language != null) { if (!this.language.trim().equals("")) { QName langQN = new QName("lang", xml); exceptionReport.addAttribute(langQN, this.language.trim()); } } //Version exceptionReport.addAttribute("version", this.version); QName exceptionQN = new QName("Exception", sws); for (SWSException e : this.exceptions) { if (e != null) { Element exception = exceptionReport.addElement(exceptionQN).addAttribute("code", e.getCode()); if (e.getLocator() != null) { if (!e.getLocator().trim().equals("")) { exception.addAttribute("locator", e.getLocator().trim()); } } if (e.getMessage() != null) { QName messageQN = new QName("Message", sws); exception.addElement(messageQN).addText(e.getMessage()); } } } Document exceptionReportDoc = DocumentHelper.createDocument(exceptionReport); return exceptionReportDoc; } else { //Raise Empty exceptionReport exception return null; } }