List of usage examples for org.w3c.dom Element setAttributeNS
public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;
From source file:org.apache.ode.utils.DOMUtils.java
/** * Adds namespaces including all prefixes. * This is needed for correct handling of xsi:type attributes. * @param domElement An element wi which the namespace attributes should be added. * @param nscontext A namespace context. * @author k.petrauskas/*from w w w .java 2 s. c o m*/ */ public static void injectNamespacesWithAllPrefixes(Element domElement, NSContext nscontext) { if (__log.isDebugEnabled()) __log.debug("injectNamespacesWithAllPrefixes: element=" + domToString(domElement) + " nscontext=" + nscontext); for (Map.Entry<String, String> entry : nscontext.toMap().entrySet()) { String prefix = entry.getKey(); String uri = entry.getValue(); if (prefix == null || "".equals(prefix)) domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", uri); else domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, uri); if (__log.isDebugEnabled()) __log.debug("injectNamespacesWithAllPrefixes: added namespace: prefix=\"" + prefix + "\" uri=\"" + uri + "\""); } if (__log.isDebugEnabled()) __log.debug("injectNamespacesWithAllPrefixes: result: element=" + domToString(domElement)); }
From source file:org.apache.ode.utils.DOMUtils.java
public static void copyNSContext(Element source, Element dest) { Map<String, String> sourceNS = getParentNamespaces(source); sourceNS.putAll(getMyNamespaces(source)); Map<String, String> destNS = getParentNamespaces(dest); destNS.putAll(getMyNamespaces(dest)); // (source - dest) to avoid adding twice the same ns on dest for (String pr : destNS.keySet()) sourceNS.remove(pr);// w w w .j a v a 2 s . c o m for (Map.Entry<String, String> entry : sourceNS.entrySet()) { String prefix = entry.getKey(); String uri = entry.getValue(); if (prefix == null || "".equals(prefix)) dest.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", uri); else dest.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, uri); } }
From source file:org.apache.ode.utils.DOMUtils.java
private static void declare(Element node, String uri, String prefix) { if (prefix != null && prefix.length() > 0) { node.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefix, uri); } else {/* w w w . j a v a 2 s .c om*/ if (uri != null) { node.setAttributeNS(NS_URI_XMLNS, "xmlns", uri); } } }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Parse the text in the cloneChild for any embedded prefixes, and define it in it's parent element * * @param sourceNode/*www. ja va2 s . co m*/ * @param clonedNode * @param clonedChild */ private static void parseEmbeddedPrefixes(Node sourceNode, Node clonedNode, Node clonedChild) { Element clonedElement = null; if (clonedNode instanceof Attr) { clonedElement = ((Attr) clonedNode).getOwnerElement(); } else if (clonedNode instanceof Element) { clonedElement = (Element) clonedNode; } if (clonedElement == null) { // couldn't find an element to set prefixes on, so bail out return; } String text = ((Text) clonedChild).getNodeValue(); if (text != null && text.indexOf(":") > 0) { Name11Checker nameChecker = Name11Checker.getInstance(); for (int colonIndex = text.indexOf(":"); colonIndex != -1 && colonIndex < text.length(); colonIndex = text.indexOf(":", colonIndex + 1)) { StringBuffer prefixString = new StringBuffer(); for (int prefixIndex = colonIndex - 1; prefixIndex >= 0 && nameChecker.isNCNameChar(text.charAt(prefixIndex)); prefixIndex--) { prefixString.append(text.charAt(prefixIndex)); } prefixString.reverse(); if (prefixString.length() > 0) { String uri = sourceNode.lookupNamespaceURI(prefixString.toString()); if (uri != null) { clonedElement.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefixString, uri); } } } } }
From source file:org.apache.rahas.TrustUtil.java
public static Element createSecurityTokenReferenceWithTokenType(Document doc, String refUri, String refValueType, String tokenType) { Reference ref = new Reference(doc); ref.setURI("#" + refUri); if (refValueType != null) { ref.setValueType(refValueType);//from w w w . j a va2 s . c o m } SecurityTokenReference str = new SecurityTokenReference(doc); str.setReference(ref); Element securityTokenReference = str.getElement(); securityTokenReference.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:wsse11", "http://docs.oasis-open.org/wss/oasis-wsswssecurity-secext-1.1.xsd"); securityTokenReference.setAttributeNS( "http://docs.oasis-open" + ".org/wss/oasis-wsswssecurity-secext-1.1.xsd", "wsse11:TokenType", tokenType); return securityTokenReference; }
From source file:org.apache.servicemix.jbi.runtime.impl.ServiceEndpointImpl.java
public DocumentFragment getAsReference(QName operationName) { try {/* w w w .j a va 2 s.co m*/ Document doc = DOMUtil.newDocument(); DocumentFragment fragment = doc.createDocumentFragment(); Element epr = doc.createElementNS(JBI_NAMESPACE, JBI_PREFIX + JBI_ENDPOINT_REFERENCE); epr.setAttributeNS(XMLNS_NAMESPACE, "xmlns:sns", getServiceName().getNamespaceURI()); epr.setAttributeNS(JBI_NAMESPACE, JBI_PREFIX + JBI_SERVICE_NAME, "sns:" + getServiceName().getLocalPart()); epr.setAttributeNS(JBI_NAMESPACE, JBI_PREFIX + JBI_ENDPOINT_NAME, getEndpointName()); fragment.appendChild(epr); return fragment; } catch (Exception e) { LOG.warn("Unable to create reference for ServiceEndpoint " + this, e); return null; } }
From source file:org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil.java
/** * Copy the attribues on one element to the other *///from w w w . ja v a 2 s . c om public static void copyAttributes(Element from, Element to) { // lets copy across all the remainingattributes NamedNodeMap attributes = from.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr node = (Attr) attributes.item(i); to.setAttributeNS(node.getNamespaceURI(), node.getName(), node.getValue()); } }
From source file:org.apache.shindig.gadgets.servlet.CajaContentRewriter.java
public void rewrite(Gadget gadget, MutableContent mc) { if (!cajaEnabled(gadget)) return;/*from w w w . j a va 2 s .co m*/ GadgetContext gadgetContext = gadget.getContext(); boolean debug = gadgetContext.getDebug(); Document doc = mc.getDocument(); // Serialize outside of MutableContent, to prevent a re-parse. String docContent = HtmlSerialization.serialize(doc); String cacheKey = HashUtil.checksum(docContent.getBytes()); Node root = doc.createDocumentFragment(); root.appendChild(doc.getDocumentElement()); Node cajoledData = null; if (cajoledCache != null && !debug) { Element cajoledOutput = cajoledCache.getElement(cacheKey); if (cajoledOutput != null) { cajoledData = doc.adoptNode(cajoledOutput); createContainerFor(doc, cajoledData); mc.documentChanged(); } } if (cajoledData == null) { UriFetcher fetcher = makeFetcher(gadget); UriPolicy policy = makePolicy(gadget); URI javaGadgetUri = gadgetContext.getUrl().toJavaUri(); MessageQueue mq = new SimpleMessageQueue(); MessageContext context = new MessageContext(); PluginMeta meta = new PluginMeta(fetcher, policy); PluginCompiler compiler = makePluginCompiler(meta, mq); compiler.setMessageContext(context); if (debug) { // This will load cajita-debugmode.js gadget.addFeature("caja-debug"); compiler.setGoals(compiler.getGoals().without(PipelineMaker.ONE_CAJOLED_MODULE) .with(PipelineMaker.ONE_CAJOLED_MODULE_DEBUG)); } InputSource is = new InputSource(javaGadgetUri); boolean safe = false; compiler.addInput(new Dom(root), javaGadgetUri); try { if (!compiler.run()) { throw new GadgetRewriteException("Gadget has compile errors"); } StringBuilder scriptBody = new StringBuilder(); CajoledModule cajoled = compiler.getJavascript(); TokenConsumer tc = debug ? new JsPrettyPrinter(new Concatenator(scriptBody)) : new JsMinimalPrinter(new Concatenator(scriptBody)); cajoled.render(new RenderContext(tc).withAsciiOnly(true).withEmbeddable(true)); tc.noMoreTokens(); Node html = compiler.getStaticHtml(); Element script = doc.createElementNS(Namespaces.HTML_NAMESPACE_URI, "script"); script.setAttributeNS(Namespaces.HTML_NAMESPACE_URI, "type", "text/javascript"); script.appendChild(doc.createTextNode(scriptBody.toString())); Element cajoledOutput = doc.createElement("div"); cajoledOutput.setAttribute("id", "cajoled-output"); cajoledOutput.setAttribute("classes", "g___"); cajoledOutput.setAttribute("style", "position: relative;"); cajoledOutput.appendChild(doc.adoptNode(html)); cajoledOutput.appendChild(tameCajaClientApi(doc)); cajoledOutput.appendChild(doc.adoptNode(script)); Element messagesNode = formatErrors(doc, is, docContent, mq, /* is invisible */ false); cajoledOutput.appendChild(messagesNode); if (cajoledCache != null && !debug) { cajoledCache.addElement(cacheKey, cajoledOutput); } cajoledData = cajoledOutput; createContainerFor(doc, cajoledData); mc.documentChanged(); safe = true; HtmlSerialization.attach(doc, htmlSerializer, null); } catch (GadgetRewriteException e) { // There were cajoling errors // Content is only used to produce useful snippets with error messages createContainerFor(doc, formatErrors(doc, is, docContent, mq, true /* visible */)); logException(e, mq); safe = true; } finally { if (!safe) { // Fail safe mc.setContent(""); } } } }
From source file:org.apache.sling.its.servlets.ItsImportServlet.java
/** * Store the element and its attribute. The child node of global rules are * specially handled so they will not be traversed. * * @param path/* ww w . j a va 2 s.c o m*/ * the target path * @param resourceType * the resourceType * @param doc * the document * @param file * the file. * @param isExternalDoc * true if this is for storing global rules for external documents */ private void store(String path, final String resourceType, final Document doc, final File file, final boolean isExternalDoc) { final ITraversal itsEng = applyITSRules(doc, file, null, false); itsEng.startTraversal(); Node node; while ((node = itsEng.nextNode()) != null) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; // Use !backTracking() to get to the elements only once // and to include the empty elements (for attributes). if (itsEng.backTracking()) { if (!SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) { path = backTrack(path); } } else { if (element.isSameNode(doc.getDocumentElement()) && !isExternalDoc) { path += "/" + element.getNodeName(); output(path, null, null); setAttributes(element, path); } else if (SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) { storeGlobalRule(element, resourceType, itsEng); } else if (!isExternalDoc && !SlingItsConstants.getGlobalRules().containsKey(element.getLocalName()) && !(element.getParentNode().getLocalName().equals(SlingItsConstants.ITS_RULES) && element.getParentNode().getPrefix() != null)) { if (element.getLocalName().equals(SlingItsConstants.ITS_RULES) && element.getPrefix() != null) { this.hasGlobalRules = true; } if (element.getPrefix() != null) { path += String.format("/%s(%d)", element.getLocalName(), getCounter(path + "/" + element.getLocalName())); element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix()); } else if (element.getNodeName().equals("link") && StringUtils.endsWith(element.getAttribute("rel"), "-rules")) { path += String.format("/%s(%d)", SlingItsConstants.ITS_RULES, getCounter(path + "/" + SlingItsConstants.ITS_RULES)); final String prefix = StringUtils.substringBefore(element.getAttribute("rel"), "-rules"); element.setAttribute(SlingItsConstants.NODE_PREFIX, prefix); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + prefix, Namespaces.ITS_NS_URI); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:h", Namespaces.HTML_NS_URI); element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:jcr", NamespaceRegistry.NAMESPACE_JCR); this.hasGlobalRules = true; } else { path += String.format("/%s(%d)", element.getNodeName(), getCounter(path + "/" + element.getNodeName())); } output(path, null, null); setAttributes(element, path); if (!element.hasChildNodes()) // Empty elements: { path = backTrack(path); } } } break; case Node.TEXT_NODE: if (StringUtils.isNotBlank(node.getNodeValue()) && !isExternalDoc) { path += String.format("/%s(%d)", SlingItsConstants.TEXT_CONTENT_NODE, getCounter(path + "/" + SlingItsConstants.TEXT_CONTENT_NODE)); output(path, null, node.getNodeValue()); path = backTrack(path); } break; default: break; } } }
From source file:org.apache.sling.its.servlets.ItsServlet.java
/** * Add its, sling-its, jcr and sling namespaces to the root element of the * xml document./*from w ww . ja v a 2 s. c o m*/ * * @param rootElement * the root element of the Document. */ private void addNamespaces(final Element rootElement) { rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + Namespaces.ITS_NS_PREFIX, Namespaces.ITS_NS_URI); rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + "sling-its", "http://www.w3.org/2013/7/sling-its"); rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + NamespaceRegistry.PREFIX_JCR, NamespaceRegistry.NAMESPACE_JCR); rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + SlingConstants.NAMESPACE_PREFIX, "http://sling.apache.org/jcr/sling/1.0"); }