Example usage for org.w3c.dom Element setAttributeNS

List of usage examples for org.w3c.dom Element setAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttributeNS.

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:Utils.java

/**
 * Set a namespace/prefix on an element if it is not set already. First off, it searches for the element
 * for the prefix associated with the specified namespace. If the prefix isn't null, then this is
 * returned. Otherwise, it creates a new attribute using the namespace/prefix passed as parameters.
 * /*from w  ww  . j av  a2s.  co m*/
 * @param element
 * @param namespace
 * @param prefix
 * @return the prefix associated with the set namespace
 */
public static String setNamespace(Element element, String namespace, String prefix) {
    String pre = getPrefixRecursive(element, namespace);
    if (pre != null) {
        return pre;
    }
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespace);
    return prefix;
}

From source file:Main.java

public static Element createElement(final Document document, final String qName,
        final Optional<String> namespaceURI) {
    if (namespaceURI.isPresent()) {
        final Element element = document.createElementNS(namespaceURI.get(), qName);
        String name = XMLNS_ATTRIBUTE_KEY;
        if (element.getPrefix() != null) {
            name += ":" + element.getPrefix();
        }/*from  w  w w  . jav  a  2s  . c  om*/
        element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
        return element;
    }
    return document.createElement(qName);
}

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Prepare XMP Saving Put data necessary to make a well-formed XMP
 * //from   w w w. j a  v a 2 s .co  m
 * @param metadata
 *            metadata concerned by the serialization processing
 * @param intoXPacket
 *            true if Processing instruction must be embedded
 * @return The DOM Document which will represent the serialized metadata
 */
protected static Document prepareSaving(XMPMetadata metadata, boolean intoXPacket) {
    Document newdoc = (Document) metadata.getFuturOwner().cloneNode(true);
    if (intoXPacket) {
        ProcessingInstruction beginXPacket = newdoc.createProcessingInstruction("xpacket",
                "begin=\"" + metadata.getXpacketBegin() + "\" id=\"" + metadata.getXpacketId() + "\"");
        newdoc.appendChild(beginXPacket);
    }

    Element xmpMeta = newdoc.createElementNS("adobe:ns:meta/", "x:xmpmeta");
    xmpMeta.setAttributeNS(XMPSchema.NS_NAMESPACE, "xmlns:x", "adobe:ns:meta/");

    newdoc.appendChild(xmpMeta);
    Element elem = (Element) metadata.getContainerElement().cloneNode(true);
    newdoc.adoptNode(elem);
    xmpMeta.appendChild(elem);

    if (intoXPacket) {
        ProcessingInstruction endXPacket = newdoc.createProcessingInstruction("xpacket",
                metadata.getEndXPacket());
        newdoc.appendChild(endXPacket);
    }
    return newdoc;
}

From source file:Main.java

/**
 * Processes any attributes and add them into the element.
 *
 * @param element       the element where to add the attributes.
 * @param anyAttributes the any attributes to process.
 *///from   ww w  .ja  va  2  s.c om
public static void processAnyAttributes(Element element, Map<QName, String> anyAttributes) {
    for (Map.Entry<QName, String> attribute : anyAttributes.entrySet()) {
        String localName = attribute.getKey().getLocalPart();
        String prefix = attribute.getKey().getPrefix();
        String namespace = attribute.getKey().getNamespaceURI();
        element.setAttributeNS(namespace, prefix + ":" + localName, attribute.getValue());
    }
}

From source file:Main.java

/**
 * Return a string for a particular QName, mapping a new prefix
 * if necessary./*from   w ww  .ja  v a2s  .  c o  m*/
 */
public static String getStringForQName(QName qname, Element e) {
    String uri = qname.getNamespaceURI();
    String prefix = getPrefix(uri, e);
    if (prefix == null) {
        int i = 1;
        prefix = "ns" + i;
        while (getNamespace(prefix, e) != null) {
            i++;
            prefix = "ns" + i;
        }
        e.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefix, uri);
    }
    return prefix + ":" + qname.getLocalPart();
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.ooxml.AbstractOOXMLSignatureService.java

private static void addOriginSigsRels(final String signatureZipEntryName, final ZipOutputStream zipOutputStream)
        throws ParserConfigurationException, IOException, TransformerException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);

    final Document originSignRelsDocument = documentBuilderFactory.newDocumentBuilder().newDocument();

    final Element relationshipsElement = originSignRelsDocument.createElementNS(RELATIONSHIPS_SCHEMA,
            "Relationships"); //$NON-NLS-1$
    relationshipsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", RELATIONSHIPS_SCHEMA); //$NON-NLS-1$
    originSignRelsDocument.appendChild(relationshipsElement);

    final Element relationshipElement = originSignRelsDocument.createElementNS(RELATIONSHIPS_SCHEMA,
            "Relationship"); //$NON-NLS-1$
    final String relationshipId = "rel-" + UUID.randomUUID().toString(); //$NON-NLS-1$
    relationshipElement.setAttribute("Id", relationshipId); //$NON-NLS-1$
    relationshipElement.setAttribute("Type", //$NON-NLS-1$
            "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"); //$NON-NLS-1$

    relationshipElement.setAttribute("Target", FilenameUtils.getName(signatureZipEntryName)); //$NON-NLS-1$
    relationshipsElement.appendChild(relationshipElement);

    zipOutputStream.putNextEntry(new ZipEntry("_xmlsignatures/_rels/origin.sigs.rels")); //$NON-NLS-1$
    writeDocumentNoClosing(originSignRelsDocument, zipOutputStream, false);
}

From source file:Main.java

/**
 * Sets namespaces on the given XML element.
 *
 * @param element    the XML element on which the namespaces will be set.
 * @param namespaces the namespace mappings.
 *//*from  ww  w . j a v a 2s .c o m*/
public static void setNamespaces(Element element, Map namespaces) {
    if (element == null) {
        throw new IllegalArgumentException("nullArgument: element");
        //                    i18n.getMessage("nullArgument", "element"));
    }
    if (namespaces != null) {
        for (Object o : namespaces.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            element.setAttributeNS("http://www.w3.org/2000/xmlns/", (String) entry.getValue(),
                    (String) entry.getKey());
        }
    }
}

From source file:Main.java

/**
 * Creates a new DOM Tree with a root element containing the schema attributes.
 *
 * @param rootName        The name of the root element.
 * @param namespaceURI    The uri of the namespace of the document.
 * @param namespacePrefix The prefix to use for the namespace (ie. the part
 *                        before the ':' in the root element.
 * @param namespaceXSD    The name of the xsd file used to validate the file.
 *                        Should be given relative to xsdBase.
 *///from ww  w . j a v a2 s  .co m
public static Document newXMLTree(String rootName, String namespaceURI, String namespacePrefix, String xsdBase,
        String namespaceXSD) {
    try {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();

        DocumentBuilder b = f.newDocumentBuilder();
        Document doc = b.newDocument();
        Element rootNode;
        if (namespaceURI == null) {
            rootNode = doc.createElement(rootName);
        } else {
            rootNode = doc.createElementNS(namespaceURI, rootName);

            if (namespacePrefix != null) {
                rootNode.setPrefix(namespacePrefix);
            }

            rootNode.setAttributeNS(xmlnsURI, "xmlns:xsi", schemaInstanceNS);
            if (namespacePrefix == null || namespacePrefix.isEmpty()) {
                rootNode.setAttributeNS(xmlnsURI, "xmlns", namespaceURI);
            } else {
                rootNode.setAttributeNS(xmlnsURI, "xmlns:" + namespacePrefix, namespaceURI);
            }

            if (xsdBase != null && namespaceXSD != null) {
                rootNode.setAttributeNS(schemaInstanceNS, "xsi:schemaLocation",
                        namespaceURI + " " + xsdBase + namespaceXSD);
            }
        }
        doc.appendChild(rootNode);
        return doc;
    } catch (Exception e) {
        return null;
    }
}

From source file:at.ac.dbisinformatik.snowprofile.web.svgcreator.SVGCreator.java

/**
 * creates a SVG-Graph for given jsonDocument which includes ExtJS-SVG-Objects. The created SVG-Document will be converted in the given export type. 
 * //from  ww w. jav  a  2  s  .  c o  m
 * @param jsonDocument
 * @param exportType
 * @param profileID
 * @return
 * @throws TransformerException
 * @throws URISyntaxException
 * @throws TranscoderException
 * @throws IOException
 */
public static ByteArrayOutputStream svgDocument(JsonArray jsonDocument, String exportType, String profileID)
        throws TransformerException, URISyntaxException, TranscoderException, IOException {

    ByteArrayOutputStream ret = null;

    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    Document doc = impl.createDocument(svgNS, "svg", null);

    // Get the root element (the 'svg' element).
    Element svgRoot = doc.getDocumentElement();

    // Set the width and height attributes on the root 'svg' element.
    svgRoot.setAttributeNS(null, "width", "1500");
    svgRoot.setAttributeNS(null, "height", "1500");
    JsonArray items = jsonDocument;

    for (int i = 0; i < items.size(); ++i) {
        String type = items.get(i).getAsJsonObject().get("type").getAsString();
        Element element = null;
        org.w3c.dom.Text test = null;
        String path = "";
        String width = "";
        String height = "";
        String x = "";
        String y = "";
        String fill = "";
        String text = "";
        String font = "";
        String fontFamily = "";
        String fontSize = "";
        String degrees = "";
        String stroke = "";
        String opacity = "";
        String src = "";
        switch (type) {
        case "rect":
            width = items.get(i).getAsJsonObject().get("width").getAsString();
            height = items.get(i).getAsJsonObject().get("height").getAsString();
            x = items.get(i).getAsJsonObject().get("x").getAsString();
            y = items.get(i).getAsJsonObject().get("y").getAsString();
            fill = items.get(i).getAsJsonObject().get("fill").getAsString();
            stroke = items.get(i).getAsJsonObject().get("stroke").getAsString();
            opacity = items.get(i).getAsJsonObject().get("opacity").getAsString();

            // Create the rectangle.
            element = doc.createElementNS(svgNS, "rect");
            element.setAttributeNS(null, "width", width);
            element.setAttributeNS(null, "height", height);
            element.setAttributeNS(null, "x", x);
            element.setAttributeNS(null, "y", y);
            element.setAttributeNS(null, "fill", fill);
            element.setAttributeNS(null, "stroke", stroke);
            element.setAttributeNS(null, "opacity", opacity);
            break;

        case "path":
            path = items.get(i).getAsJsonObject().get("path").getAsString();
            fill = items.get(i).getAsJsonObject().get("fill").getAsString();
            stroke = items.get(i).getAsJsonObject().get("stroke").getAsString();

            // Create the path.
            element = doc.createElementNS(svgNS, "path");
            element.setAttributeNS(null, "d", path);
            element.setAttributeNS(null, "fill", fill);
            element.setAttributeNS(null, "stroke", stroke);
            break;

        case "image":
            x = items.get(i).getAsJsonObject().get("x").getAsString();
            y = items.get(i).getAsJsonObject().get("y").getAsString();
            width = items.get(i).getAsJsonObject().get("width").getAsString();
            height = items.get(i).getAsJsonObject().get("height").getAsString();
            src = System.class.getResource("/at/ac/dbisinformatik/snowprofile/web/resources/"
                    + items.get(i).getAsJsonObject().get("src").getAsString()).toString();

            // Create the path.
            element = doc.createElementNS(svgNS, "image");
            element.setAttributeNS(null, "x", x);
            element.setAttributeNS(null, "y", y);
            element.setAttributeNS(null, "width", width);
            element.setAttributeNS(null, "height", height);
            element.setAttributeNS(null, "xlink:href", src);
            break;

        case "text":
            text = items.get(i).getAsJsonObject().get("text").getAsString();
            fill = items.get(i).getAsJsonObject().get("fill").getAsString();
            font = items.get(i).getAsJsonObject().get("font").getAsString();
            x = items.get(i).getAsJsonObject().get("x").getAsString();
            y = items.get(i).getAsJsonObject().get("y").getAsString();

            // Transformation
            if (items.get(i).getAsJsonObject().get("rotate") != null) {
                JsonObject temp = items.get(i).getAsJsonObject().get("rotate").getAsJsonObject();
                degrees = temp.get("degrees").getAsString();
            }

            fontSize = font.split(" ")[0];
            fontFamily = font.split(" ")[1];

            // Create the text.
            test = doc.createTextNode(text);
            element = doc.createElementNS(svgNS, "text");
            element.setAttributeNS(null, "text", text);
            element.setAttributeNS(null, "fill", fill);
            element.setAttributeNS(null, "font-family", fontFamily);
            element.setAttributeNS(null, "font-size", fontSize);
            element.setAttributeNS(null, "x", x);
            element.setAttributeNS(null, "y", y);
            if (!degrees.equals("")) {
                // element.setAttributeNS(null, "transform",
                // "rotate(270 "+500+","+80+")");
            }
            element.appendChild(test);
            break;

        default:
            break;
        }

        // Attach the rectangle to the root 'svg' element.
        svgRoot.appendChild(element);
    }

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    switch (exportType) {
    case "png":
        ret = createPNG(inputStream);
        break;

    case "jpg":
        ret = createJPG(inputStream);
        break;

    case "pdf":
        ret = createPDF(inputStream);
        break;

    default:
        break;
    }
    return ret;
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param commentsDom//from w  w  w .  java  2s  .c  o m
 * @param commentTemplate
 * @param messageText
 * @param commentId
 */
static void addCommentToComments(Document commentsDom, Element commentTemplate, String messageText,
        String commentId) {
    Element comment = (Element) commentsDom.importNode(commentTemplate, true);
    commentsDom.getDocumentElement().appendChild(comment);
    comment.setAttributeNS(wNs, "w:id", commentId);
    comment.setAttributeNS(wNs, "w:author", "XML Validator");
    comment.setAttributeNS(wNs, "w:initials", "XMLVal");
    comment.setAttributeNS(wNs, "w:date", timestampFormatter.format(Calendar.getInstance().getTime()));
    Element elem = DataUtil.getElementNS(comment, wNs, "p");
    NodeList nl = elem.getElementsByTagNameNS(wNs, "r");
    elem = (Element) nl.item(nl.getLength() - 1);
    Element text = DataUtil.getElementNS(elem, wNs, "t");
    text.setTextContent(messageText);
}