List of usage examples for org.w3c.dom Document createTextNode
public Text createTextNode(String data);
Text
node given the specified string. From source file:fi.helsinki.lib.simplerest.BundleResource.java
@Get("xml") public Representation get() { Context c = null;/*from w w w. j a v a 2s.c o m*/ Bundle bundle = null; DomRepresentation representation = null; Document d = null; try { c = new Context(); bundle = Bundle.find(c, this.bundleId); if (bundle == null) { return errorNotFound(c, "Could not find the bundle."); } representation = new DomRepresentation(MediaType.TEXT_HTML); d = representation.getDocument(); } catch (Exception e) { return errorInternal(c, e.toString()); } Element html = d.createElement("html"); d.appendChild(html); Element head = d.createElement("head"); html.appendChild(head); Element title = d.createElement("title"); head.appendChild(title); title.appendChild(d.createTextNode("Bundle " + bundle.getName())); Element body = d.createElement("body"); html.appendChild(body); Element dl = d.createElement("dl"); setId(dl, "attributes"); body.appendChild(dl); addDtDd(d, dl, "primarybitstreamid", Integer.toString(bundle.getPrimaryBitstreamID())); addDtDd(d, dl, "name", bundle.getName()); Element ulBitstreams = d.createElement("ul"); setId(ulBitstreams, "bitstreams"); body.appendChild(ulBitstreams); String base = baseUrl(); for (Bitstream bitstream : bundle.getBitstreams()) { Element li = d.createElement("li"); Element a = d.createElement("a"); String href = base + BitstreamResource.relativeUrl(bitstream.getID()); setAttribute(a, "href", href); a.appendChild(d.createTextNode(bitstream.getName())); li.appendChild(a); ulBitstreams.appendChild(li); } c.abort(); // Same as c.complete() because we didn't modify the db. return representation; }
From source file:de.xplib.xdbm.util.Config.java
/** * // www . jav a2 s .c om */ public void save() { Document doc = null; try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element config = (Element) doc.appendChild(doc.createElementNS(CONFIG_NS, "config")); config.setAttribute("xmlns", CONFIG_NS); Element ui = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "ui-settings")); Node lang = ui.appendChild(doc.createElementNS(CONFIG_NS, "language")); lang.appendChild(doc.createTextNode(this.locale.getLanguage())); Element drivers = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "drivers")); Iterator dIt = this.dbDrivers.keySet().iterator(); while (dIt.hasNext()) { String jar = (String) dIt.next(); Element driver = (Element) drivers.appendChild(doc.createElementNS(CONFIG_NS, "driver")); driver.setAttribute("jar", jar); driver.setAttribute("class", (String) this.dbDrivers.get(jar)); if (!this.dbUris.containsKey(jar)) { continue; } String value = ""; ArrayList uris = (ArrayList) this.dbUris.get(jar); for (int i = 0, l = uris.size(); i < l; i++) { value += uris.get(i) + "||"; } value = value.substring(0, value.length() - 2); driver.setAttribute("uris", value); } Element conns = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "connections")); for (int i = 0, s = this.connections.size(); i < s; i++) { Connection conn = (Connection) this.connections.get(i); Element conne = (Element) conns.appendChild(doc.createElementNS(CONFIG_NS, "connection")); conne.setAttribute("jar", conn.getJarFile()); conne.setAttribute("class", conn.getClassName()); conne.setAttribute("uri", conn.getUri()); } Element panels = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "plugins")); Iterator it = this.plugins.keySet().iterator(); while (it.hasNext()) { PluginFile pluginFile = (PluginFile) this.plugins.get(it.next()); Element elem = (Element) panels.appendChild(doc.createElementNS(CONFIG_NS, "plugin")); elem.setAttribute("jar", pluginFile.getJarFile()); elem.setAttribute("class", pluginFile.getClassName()); elem.setAttribute("id", pluginFile.getId()); elem.setAttribute("position", pluginFile.getPosition()); } } catch (ParserConfigurationException e) { e.printStackTrace(); } if (doc != null) { OutputFormat of = new OutputFormat(doc); of.setIndenting(true); of.setIndent(1); of.setStandalone(true); StringWriter sw = new StringWriter(); XMLSerializer xs = new XMLSerializer(sw, of); xs.setOutputCharStream(sw); try { xs.serialize(doc); sw.flush(); System.out.println(sw.toString()); sw.close(); } catch (IOException e1) { e1.printStackTrace(); } try { FileWriter fw = new FileWriter(this.cfgFile); fw.write(sw.toString()); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:fi.helsinki.lib.simplerest.CollectionsResource.java
@Get("xml") public Representation toXml() { Context c = null;//w ww .j a va2 s. co m Community community = null; DomRepresentation representation = null; Document d = null; try { c = new Context(); community = Community.find(c, this.communityId); if (community == null) { return errorNotFound(c, "Could not find the community."); } representation = new DomRepresentation(MediaType.TEXT_HTML); d = representation.getDocument(); } catch (Exception e) { return errorInternal(c, e.toString()); } Element html = d.createElement("html"); d.appendChild(html); Element head = d.createElement("head"); html.appendChild(head); Element title = d.createElement("title"); head.appendChild(title); title.appendChild(d.createTextNode("Community " + community.getName())); Element body = d.createElement("body"); html.appendChild(body); Collection[] collections; try { collections = community.getCollections(); } catch (SQLException e) { return errorInternal(c, e.toString()); } String url = getRequest().getResourceRef().getIdentifier(); url = url.substring(0, url.lastIndexOf('/', url.lastIndexOf('/', url.lastIndexOf('/') - 1) - 1)); url += "/collection/"; Element ulCollections = d.createElement("ul"); setId(ulCollections, "collections"); body.appendChild(ulCollections); for (Collection collection : collections) { Element li = d.createElement("li"); Element a = d.createElement("a"); String href = url + Integer.toString(collection.getID()); setAttribute(a, "href", href); a.appendChild(d.createTextNode(collection.getName())); li.appendChild(a); ulCollections.appendChild(li); } Element form = d.createElement("form"); form.setAttribute("enctype", "multipart/form-data"); form.setAttribute("method", "post"); makeInputRow(d, form, "name", "Name"); makeInputRow(d, form, "short_description", "Short description"); makeInputRow(d, form, "introductory_text", "Introductory text"); makeInputRow(d, form, "copyright_text", "Copyright text"); makeInputRow(d, form, "side_bar_text", "Side bar text"); makeInputRow(d, form, "provenance_description", "Provenance Description"); makeInputRow(d, form, "license", "License"); makeInputRow(d, form, "logo", "Logo", "file"); Element submitButton = d.createElement("input"); submitButton.setAttribute("type", "submit"); submitButton.setAttribute("value", "Create a new collection"); form.appendChild(submitButton); body.appendChild(form); c.abort(); // Same as c.complete() because we didn't modify the db. return representation; }
From source file:com.verisign.epp.codec.gen.EPPUtil.java
/** * Encode a <code>List</code> of <code>Integer</code>'s in XML with a given * XML namespace and tag name.// ww w . j a va2 s . c o m * * @param aDocument * DOM Document of <code>aRoot</code>. This parameter also acts * as a factory for XML elements. * @param aRoot * XML Element to add children nodes to. For example, the root * node could be <domain:update> * @param aList * <code>List</code> of <code>Integer</code>'s to add. * @param aNS * XML namespace of the elements. For example, for domain element * this is "urn:iana:xmlns:domain". * @param aTagName * Tag name of the element including an optional namespace * prefix. For example, the tag name for the domain name servers * is "domain:server". * * @exception EPPEncodeException * Error encoding the <code>List</code> of * <code>Integer</code>'s. */ public static void encodeIntegerList(Document aDocument, Element aRoot, List aList, String aNS, String aTagName) throws EPPEncodeException { Element currElm; Text currVal; if (aList != null) { Iterator elms = aList.iterator(); while (elms.hasNext()) { currElm = aDocument.createElementNS(aNS, aTagName); currVal = aDocument.createTextNode(elms.next().toString()); currElm.appendChild(currVal); aRoot.appendChild(currElm); } // end while (elms.hasNext()) } // end if (aList != null) }
From source file:com.verisign.epp.codec.gen.EPPUtil.java
/** * Encode a <code>List</code> of <code>String</code>'s in XML with a given * XML namespace and tag name.// w w w.j a v a 2 s. c o m * * @param aDocument * DOM Document of <code>aRoot</code>. This parameter also acts * as a factory for XML elements. * @param aRoot * XML Element to add children nodes to. For example, the root * node could be <domain:update> * @param aList * <code>List</code> of <code>String's</code> to add. * @param aNS * XML namespace of the elements. For example, for domain element * this is "urn:iana:xmlns:domain". * @param aTagName * Tag name of the element including an optional namespace * prefix. For example, the tag name for the domain name servers * is "domain:server". * @exception EPPEncodeException * Error encoding the <code>List</code> of * <code>String</code>'s. */ public static void encodeList(Document aDocument, Element aRoot, List aList, String aNS, String aTagName) throws EPPEncodeException { Element currElm; Text currVal; if (aList != null) { Iterator elms = aList.iterator(); while (elms.hasNext()) { currElm = aDocument.createElementNS(aNS, aTagName); currVal = aDocument.createTextNode(elms.next().toString()); currElm.appendChild(currVal); aRoot.appendChild(currElm); } // end while (elms.hasMoreElements()) } // end if (aList != null) }
From source file:Main.java
private static Node copyNodeExceptNamespace(Document document, Node srcNode, Set<String> namespace, List<String> exceptNamespaces) { if (srcNode.getNodeType() == Node.ELEMENT_NODE) { String nodeName = srcNode.getNodeName(); nodeName = nodeName.substring(nodeName.indexOf(":") + 1); Element element = document.createElement(nodeName); for (int i = 0; i < srcNode.getAttributes().getLength(); i++) { Attr attr = (Attr) srcNode.getAttributes().item(i); String name = attr.getName(); if (name.startsWith("xmlns:")) { String suffix = name.substring(6); if (!exceptNamespaces.contains(suffix)) { namespace.add(suffix); }/*from www . ja v a 2 s. co m*/ continue; } } for (int i = 0; i < srcNode.getAttributes().getLength(); i++) { Attr attr = (Attr) srcNode.getAttributes().item(i); String name = attr.getName(); if (name.startsWith("xmlns:")) { continue; } int semi = name.indexOf(":"); if (semi > 0) { if (namespace.contains(name.substring(0, semi))) { name = name.substring(semi + 1); } } element.setAttribute(name, attr.getValue()); } NodeList nodeList = srcNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { element.appendChild(document.createTextNode(childNode.getTextContent())); } else if (childNode.getNodeType() == Node.ELEMENT_NODE) { Node node = copyNodeExceptNamespace(document, childNode, namespace, exceptNamespaces); element.appendChild(node); } } return element; } if (srcNode.getNodeType() == Node.TEXT_NODE) { Text text = document.createTextNode(srcNode.getTextContent()); return text; } return null; }
From source file:de.qucosa.webapi.v1.DocumentResource.java
private void addIdentifierUrn(Document qucosaDocument, String urn) { Element elIdentifierUrn = qucosaDocument.createElement("IdentifierUrn"); Element elValue = qucosaDocument.createElement("Value"); Text elText = qucosaDocument.createTextNode(urn); elValue.appendChild(elText);/*from w w w .j av a 2 s . c o m*/ elIdentifierUrn.appendChild(elValue); qucosaDocument.getElementsByTagName("Opus_Document").item(0).appendChild(elIdentifierUrn); }
From source file:com.verisign.epp.codec.gen.EPPUtil.java
/** * Encode a <code>Vector</code> of <code>String</code>'s in XML with a given * XML namespace and tag name.//w ww .ja va 2 s. c o m * * @param aDocument * DOM Document of <code>aRoot</code>. This parameter also acts * as a factory for XML elements. * @param aRoot * XML Element to add children nodes to. For example, the root * node could be <domain:update> * @param aVector * <code>Vector</code> of <code>String's</code> to add. * @param aNS * XML namespace of the elements. For example, for domain element * this is "urn:iana:xmlns:domain". * @param aTagName * Tag name of the element including an optional namespace * prefix. For example, the tag name for the domain name servers * is "domain:server". * @exception EPPEncodeException * Error encoding the <code>Vector</code> of * <code>String</code>'s. */ public static void encodeVector(Document aDocument, Element aRoot, Vector aVector, String aNS, String aTagName) throws EPPEncodeException { Element currElm; Text currVal; if (aVector != null) { Enumeration elms = aVector.elements(); while (elms.hasMoreElements()) { currElm = aDocument.createElementNS(aNS, aTagName); currVal = aDocument.createTextNode(elms.nextElement().toString()); currElm.appendChild(currVal); aRoot.appendChild(currElm); } // end while (elms.hasMoreElements()) } // end if (aVector != null) }
From source file:com.autentia.mvn.plugin.changes.BugzillaChangesMojo.java
/** * Addapts bugzilla XML document for transformations * //from w w w . j a v a 2 s . c o m * @param docBugzilla */ private void cleanBugzillaDocument(final Document docBugzilla) { // quitamos el DTD final Node docType = docBugzilla.getDoctype(); docBugzilla.removeChild(docType); // ponemos el ttulo final Element title = docBugzilla.createElement(ELEMENT_TITLE); title.appendChild(docBugzilla.createTextNode(this.project.getName())); docBugzilla.getDocumentElement().appendChild(title); // ponemos los atributos de version para la ordenacin final NodeList target_milestones = docBugzilla.getElementsByTagName(ELEMENT_TARGET_MILESTONE); for (int i = 0; i < target_milestones.getLength(); i++) { final Element target_milestone = (Element) target_milestones.item(i); final String version = target_milestone.getTextContent(); final String[] versions = version.split("\\."); // solo tenemos en cuenta las dos primeras // (ej. para version="1.23" tenemos la version1="1" y version2="23"; // y para version="1.23.3" se tiene lo mismo) String version1 = ""; if (versions.length > 0) { version1 = versions[0]; } String version2 = ""; if (versions.length >= 2) { version2 = versions[1]; } target_milestone.setAttribute(ATTRIBUTE_VERSION1, version1); target_milestone.setAttribute(ATTRIBUTE_VERSION2, version2); } // si hay que ajustar los desarrolladores lo procesamos if (this.fitDevelopers) { final NodeList assigned_tos = docBugzilla.getElementsByTagName(ELEMENT_ASSIGNED_TO); for (int i = 0; i < assigned_tos.getLength(); i++) { final Element assigned_to = (Element) assigned_tos.item(i); String developer = assigned_to.getTextContent(); final int index = developer.indexOf("@"); if (index != -1) { developer = developer.substring(0, index); } // quitamos el texto final NodeList childs = assigned_to.getChildNodes(); for (int j = 0; j < childs.getLength(); j++) { final Node child = childs.item(j); assigned_to.removeChild(child); // disminuimos j debido a que tambin se quita del nodelist j--; } assigned_to.appendChild(docBugzilla.createTextNode(developer)); } } // eliminamos los nodos que no son necesarios final String[] nodes2Clean = { "creation_ts", "reporter_accessible", "cclist_accessible", "classification_id", "classification", "product", "component", "version", "rep_platform", "op_sys", "bug_status", "resolution", "priority", "everconfirmed", "estimated_time", "remaining_time", "actual_time", "who", "thetext" }; for (final String node2clean : nodes2Clean) { this.removeNodes(docBugzilla.getElementsByTagName(node2clean)); } }
From source file:com.verisign.epp.codec.gen.EPPUtil.java
public static void encodeStringList(Document aDocument, Element aRoot, List aList, String aNS, String aTagName) {//from ww w . j a v a 2s.co m String aString; Element aElement; Text aValue; if (aList != null) { Iterator elms = aList.iterator(); while (elms.hasNext()) { aString = (String) elms.next(); aElement = aDocument.createElementNS(aNS, aTagName); aValue = aDocument.createTextNode(aString); aElement.appendChild(aValue); aRoot.appendChild(aElement); } } }