List of usage examples for org.dom4j DocumentHelper createElement
public static Element createElement(String name)
From source file:hk.hku.cecid.piazza.commons.util.PropertyTree.java
License:Open Source License
/** * Adds a property to this property tree. * /*ww w .j a va 2 s .co m*/ * @param xpath the property xpath. * @param value the property value. * @return true if the operation is successful. false otherwise. */ protected boolean addProperty(String xpath, String value) { try { if (xpath != null) { // retrieve the root element in the document Element curElement = dom.getRootElement(); String rootElementName = curElement == null ? "" : curElement.getName(); // basic check for the path validity xpath = xpath.trim(); if (xpath.startsWith("//") || xpath.startsWith("../")) { return false; } else if (xpath.startsWith("./")) { xpath = xpath.substring(2); } else if (xpath.startsWith("/" + rootElementName + "/")) { xpath = xpath.substring(2 + rootElementName.length()); } else if (xpath.equals("/" + rootElementName)) { xpath = ""; } else if (xpath.startsWith("/") && curElement != null) { return false; } // set the value to the root directly if there is no // sub-elements specified if ("".equals(xpath) && curElement != null) { curElement.setText(value); return true; } StringTokenizer pathElements = new StringTokenizer(xpath, "/"); // loop the elements specified in the path while (pathElements.hasMoreElements()) { // retrieve the element name String elementName = pathElements.nextToken().trim(); if (!"".equals(elementName)) { // parse the element name StringTokenizer elementNameCombo = new StringTokenizer(elementName, "[]"); boolean isIndexing = elementNameCombo.countTokens() > 1; // get all elements with the specified name elementName = elementNameCombo.nextToken(); List nextElements = curElement == null ? Collections.EMPTY_LIST : curElement.elements(elementName); // assume the element referring to the first child int elementPosition = 0; // get the element position specified in the path, if // any if (isIndexing) { try { elementPosition = Integer.parseInt(elementNameCombo.nextToken()) - 1; } catch (Exception e) { // assume the element referring to a new child // being appended at the end elementPosition = nextElements.size(); } } // adjust the element position if it is out of range if (elementPosition < 0) { elementPosition = 0; } else if (elementPosition > nextElements.size()) { elementPosition = nextElements.size(); } Element nextElement; if (elementPosition < nextElements.size()) { nextElement = (Element) nextElements.get(elementPosition); } else { if (curElement == null) { nextElement = DocumentHelper.createElement(elementName); dom.setRootElement(nextElement); } else { nextElement = curElement.addElement(elementName); } } /* // DEBUG (Print out the status in this turn of loop) Debugger.print("curElement: " + curElement + ", targetElement: " + elementName + ", targetElementPosition: " + elementPosition + ", targetExists: " + (elementPosition < nextElements.size()) + ", targetElementSizes: " + nextElements.size() + ", nextElement: " + nextElement); // DEBUG (Dump all child elements of the current // element) if (curElement != null) { Debugger.print(curElement.elements()); } // END OF DEBUG */ // move to the next element curElement = nextElement; // set the value if it is the last element if (!pathElements.hasMoreElements()) { curElement.setText(value); return true; } } } } return false; } catch (Exception e) { /*Sys.main.log.error("Error adding property '" + xpath + "' with value '" + value + "'", e);*/ return false; } }
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/* w w w .j a va 2 s.c om*/ * @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/*from ww w . ja v a 2 s .c o m*/ * @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/* w w w .j a va 2s.c o 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}// www . j a v a 2 s . c om * @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.index.lucene.DocSemanticEntityWrapper.java
License:Apache License
private List<Element> getXMLPrefLabels(String language) { QName xmlLang = new QName("lang", Namespaces.XML); List<Element> xmlAnnotations = new ArrayList<Element>(); if (language != null) { String prefLabel = this.getPrefLabel(language); if (prefLabel != null) { Element element = DocumentHelper.createElement(new QName(PREF_LABEL, Namespaces.SKOS)) .addText(prefLabel); element.addAttribute(xmlLang, language); xmlAnnotations.add(element); }/*from www. j av a 2 s.c o m*/ } else { List<String> languages = this.getPrefLabelLanguages(); for (String lang : languages) { String annotation = this.getPrefLabel(lang); if (annotation != null) { Element element = DocumentHelper.createElement(new QName(PREF_LABEL, Namespaces.SKOS)) .addText(annotation); if (lang != null && !lang.isEmpty()) element.addAttribute(xmlLang, language); xmlAnnotations.add(element); } } } return xmlAnnotations; }
From source file:ie.cmrc.smtx.skos.model.AbstractSKOSResource.java
License:Apache License
/** * {@inheritDoc}// w w w.j av a 2 s . 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(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.AbstractSKOSResource.java
License:Apache License
private List<Element> getXMLAnnotations(SKOSAnnotationProperty property, String language) { QName xmlLang = new QName("lang", Namespaces.XML); List<Element> xmlAnnotations = new ArrayList<Element>(); if (language != null) { List<String> annotations = this.getAnnotations(property, language); for (String annotation : annotations) { Element element = DocumentHelper.createElement(new QName(property.name(), Namespaces.SKOS)) .addText(annotation); element.addAttribute(xmlLang, language); xmlAnnotations.add(element); }/* w w w .j a va2 s . c om*/ } else { List<Term> annotationTerms = this.getAnnotations(property); for (Term annotationTerm : annotationTerms) { String annotation = annotationTerm.getString(); String lang = annotationTerm.getLanguage(); Element element = DocumentHelper.createElement(new QName(property.name(), Namespaces.SKOS)) .addText(annotation); if (lang != null && !lang.isEmpty()) element.addAttribute(xmlLang, language); xmlAnnotations.add(element); } } return xmlAnnotations; }
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);// w ww .ja va 2 s . com exceptionReport.add(sws); //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; } }
From source file:io.mashin.oep.model.Workflow.java
License:Open Source License
@Override public void write(org.dom4j.Element parent) { Document document = parent.getDocument(); parent.detach();/*from w ww . j a v a2 s . co m*/ Element rootElement = document.addElement("workflow-app"); Element graphicalInfoElement = DocumentHelper.createElement("workflow"); XMLWriteUtils.writeWorkflowSchemaVersion(getSchemaVersion(), rootElement); XMLWriteUtils.writeSLAVersion(this, rootElement); XMLWriteUtils.writeTextPropertyAsAttribute(name, rootElement, "name"); XMLWriteUtils.writePropertiesCollection(parameters, rootElement, "parameters", "property"); XMLWriteUtils.writeGlobalProperty(global, rootElement); XMLWriteUtils.writeCredentialsCollection(credentials, rootElement); startNode.write(rootElement); for (Node node : nodes) { if (!(node.equals(startNode) || node.equals(endNode))) { node.write(rootElement); } graphicalInfoElement.addElement("node").addAttribute("name", node.getName()) .addAttribute("x", node.getPosition().x + "").addAttribute("y", node.getPosition().y + ""); } endNode.write(rootElement); XMLWriteUtils.writeSLAProperty(this, sla, rootElement); Comment graphicalInfoNode = null; try { StringWriter stringWriter = new StringWriter(); XMLWriter writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()); writer.write(graphicalInfoElement); writer.flush(); graphicalInfoNode = DocumentHelper.createComment(stringWriter.toString()); } catch (Exception e) { graphicalInfoNode = DocumentHelper.createComment(graphicalInfoElement.asXML()); } document.add(graphicalInfoNode); }