private Element createExcludeElement(String exclude) { Element element = document.createElementNS(RULESET_2_0_0_NS_URI, "exclude"); element.setAttribute("name", exclude); return element; }
private Element getRootNode() { // Create the root node named svg and append it to // the document. final Element svg = (Element) document.createElement("svg"); document.appendChild(svg); // Set some attributes on the root node that are // required for proper rendering. Note that the // approach used here is somewhat different from the // approach used in the earlier program named Svg01, // particularly with regard to the style. svg.setAttribute("xmlns", "http://www.w3.org/2000/svg"); svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); svg.setAttribute("version", "1.1"); return svg; }
private static void addVariable(final Element parentElement, final String variableName, final String variableValue) { final Element variableElement = parentElement.getOwnerDocument().createElement("variable"); variableElement.setAttribute("name", variableName); variableElement.setAttribute("value", variableValue); parentElement.appendChild(variableElement); }
/** * appends a new Element with the given name to currentElement, sets * currentElement to be new Element, and returns the new Element as well */ private Element appendElement(String name) { Element ret = doc.createElement(name); if (currentElement == null) { ret.setAttribute("format_version", Integer.toString(FormatVersion.VERSION)); doc.appendChild(ret); } else { currentElement.appendChild(ret); } currentElement = ret; return ret; }
Element node = factory.createElement("foundJar"); node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-"))); node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1)); node.appendChild(factory.createTextNode((String)subhash.get(keyStr))); container.appendChild(node); Element node = factory.createElement("foundJar"); node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString())); container.appendChild(node);
private void addFilter(Element filter, String name, String... data) { final Element elt = (Element) document.createElement(name); for (int i = 0; i < data.length; i += 2) { elt.setAttribute(data[i], data[i + 1]); } filter.appendChild(elt); }
/** * Use to append default types XML elements, use by the save() method. * * @param root * XML parent element use to append this default type element. * @param entry * The values to append. * @see #save(java.io.OutputStream) */ private void appendDefaultType(Element root, Entry<String, String> entry) { Element defaultType = root.getOwnerDocument().createElementNS(TYPES_NAMESPACE_URI, DEFAULT_TAG_NAME); defaultType.setAttribute(EXTENSION_ATTRIBUTE_NAME, entry.getKey()); defaultType.setAttribute(CONTENT_TYPE_ATTRIBUTE_NAME, entry.getValue()); root.appendChild(defaultType); }
static class LoggingErrorListener implements ErrorListener { // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html boolean strict; public LoggingErrorListener(boolean strict) { } public void warning(TransformerException exception) { log.warn(exception.getMessage(), exception); // Don't throw an exception and stop the processor // just for a warning; but do log the problem } public void error(TransformerException exception) throws TransformerException { log.error(exception.getMessage(), exception); // XSLT is not as draconian as XML. There are numerous errors // which the processor may but does not have to recover from; // e.g. multiple templates that match a node with the same // priority. If I do not want to allow that, I'd throw this // exception here. if (strict) { throw exception; }
private Element createServerElement(Document doc, ClusteringServiceConfiguration clusteringServiceConfiguration) { if (!(clusteringServiceConfiguration.getConnectionSource() instanceof ConnectionSource.ServerList)) { throw new IllegalArgumentException("When connection URL is null, source of connection MUST be of type ConnectionSource.ServerList.class"); } ConnectionSource.ServerList servers = (ConnectionSource.ServerList)clusteringServiceConfiguration.getConnectionSource(); Element rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + CLUSTER_ELEMENT_NAME); Element connElement = createConnectionElementWrapper(doc, clusteringServiceConfiguration); servers.getServers().forEach(server -> { Element serverElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + SERVER_ELEMENT_NAME); serverElement.setAttribute(HOST_ATTRIBUTE_NAME, server.getHostName()); /* If port is greater than 0, set the attribute. Otherwise, do not set. Default value will be taken. */ if (server.getPort() > 0) { serverElement.setAttribute(PORT_ATTRIBUTE_NAME, Integer.toString(server.getPort())); } connElement.appendChild(serverElement); }); rootElement.appendChild(connElement); return rootElement; }
public Document createComponentXML() throws ParserConfigurationException { DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); Document doc = fact.newDocumentBuilder().newDocument(); Element root = (Element) doc.appendChild(doc.createElementNS(installerNamespaceURI, "marytts-install")); Element desc = (Element) root.appendChild(doc.createElementNS(installerNamespaceURI, getComponentTypeString())); desc.setAttribute("locale", MaryUtils.locale2xmllang(locale)); desc.setAttribute("name", name); desc.setAttribute("version", version); Element descriptionElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "description")); descriptionElt.setTextContent(description); Element licenseElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "license")); if (license != null) { licenseElt.setAttribute("href", license.toString()); Element packageElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "package")); packageElt.setAttribute("size", Integer.toString(packageSize)); packageElt.setAttribute("md5sum", packageMD5); packageElt.setAttribute("filename", packageFilename); for (URL l : locations) { Element lElt = (Element) packageElt.appendChild(doc.createElementNS(installerNamespaceURI, "location")); lElt.setAttribute("href", urlString); lElt.setAttribute("folder", String.valueOf(isFolder));
/** * Enclose the elements' closest common ancestor. * * @param first * first * @param last * last */ protected void slowDown(Element first, Element last) { Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY); phonol.setAttribute("precision", "precise"); Document doc = phonol.getOwnerDocument(); Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY); prosody.setAttribute("rate", "-20%"); phonol.getParentNode().insertBefore(prosody, phonol); prosody.appendChild(phonol); } }
@Override public Document createComponentXML() throws ParserConfigurationException { Document doc = super.createComponentXML(); NodeList nodes = doc.getElementsByTagName(getComponentTypeString()); assert nodes.getLength() == 1; Element voiceElt = (Element) nodes.item(0); voiceElt.setAttribute("type", type); voiceElt.setAttribute("gender", gender); Element dependsElt = (Element) voiceElt.appendChild(doc.createElementNS(ComponentDescription.installerNamespaceURI, "depends")); dependsElt.setAttribute("language", dependsLanguage); dependsElt.setAttribute("version", dependsVersion); return doc; } }
protected MaryData createMaryDataFromText(String text, Locale locale) { Document doc = MaryXML.newDocument(); doc.getDocumentElement().setAttribute("xml:lang", MaryUtils.locale2xmllang(locale)); doc.getDocumentElement().appendChild(doc.createTextNode(text)); MaryData md = new MaryData(MaryDataType.RAWMARYXML, locale); md.setDocument(doc); return md; }
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); doc = docBuilder.newDocument(); } catch (final ParserConfigurationException e) { throw new RuntimeException("Can't create SAX parser / DOM builder.", e); throws SAXException { addTextIfNeeded(); final Element el = doc.createElement(qName); for (int i = 0; i < attributes.getLength(); i++) { el.setAttribute(attributes.getQName(i), attributes.getValue(i)); if (textBuffer.length() > 0) { final Element el = elementStack.peek(); final Node textNode = doc.createTextNode(textBuffer.toString()); el.appendChild(textNode); textBuffer.delete(0, textBuffer.length());
/** * Append one paragraph of text to the rawmaryxml document. If the text language (as determined by #getLanguage(text)) differs * from the enclosing document's language, the paragraph element is enclosed with a <code><voice xml:lang="..."></code> * element. * * @param text * the paragraph text. * @param root * the root node of the rawmaryxml document, where to insert the paragraph. * @param defaultLocale * the default locale, in case the language of the text cannot be determined. */ private void appendParagraph(String text, Element root, Locale defaultLocale) { Element insertHere = root; String rootLanguage = root.getAttribute("xml:lang"); String textLanguage = MaryUtils.locale2xmllang(determineLocale(text, defaultLocale)); if (!textLanguage.equals(rootLanguage)) { Element voiceElement = MaryXML.appendChildElement(root, MaryXML.VOICE); voiceElement.setAttribute("xml:lang", textLanguage); insertHere = voiceElement; } insertHere = MaryXML.appendChildElement(insertHere, MaryXML.PARAGRAPH); // Now insert the entire plain text as a single text node insertHere.appendChild(root.getOwnerDocument().createTextNode(text)); // And, for debugging, read it: Text textNode = (Text) insertHere.getFirstChild(); String textNodeString = textNode.getData(); logger.debug("textNodeString=`" + textNodeString + "'"); }
private static Element createMetaDataNode(String name, String value) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Element metaDataElement; try { DocumentBuilder db = dbf.newDocumentBuilder(); metaDataElement = db.newDocument().createElement("meta-data"); metaDataElement.setAttribute("android:name", name); metaDataElement.setAttribute("android:value", value); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } return metaDataElement; } }
protected static void copyAndReplaceElement(Element oldElement, Element newElement) { while(oldElement.getChildNodes().getLength() > 0) { newElement.appendChild(oldElement.getChildNodes().item(0)); } NamedNodeMap attrs = oldElement.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); newElement.setAttribute(ns.getNodeName(), ns.getNodeValue()); } ((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement); } }
public static void addTextTag(Document doc, Element element, String name, String namespace, String text, int indent) { Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent)); element.appendChild(node); Element child = doc.createElementNS(namespace, name); element.appendChild(child); child.setAttribute("value", text); }
private Element createDuplicationElement(Document doc, Match match) { Element duplication = doc.createElement("duplication"); duplication.setAttribute("lines", String.valueOf(match.getLineCount())); duplication.setAttribute("tokens", String.valueOf(match.getTokenCount())); return duplication; } }
private static Document makeErr(String msg) { Document d=XmlUtils.getNewDocumentBuilder().newDocument(); Element span = d.createElement("span"); span.setAttribute("style", "color:red;"); d.appendChild(span); Text err = d.createTextNode( msg ); span.appendChild(err); return d; }