Example usage for org.w3c.dom Element setAttribute

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

Introduction

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

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:fr.aliasource.webmail.proxy.impl.ResponderImpl.java

private void addRecipients(Element recipients, List<org.minig.imap.Address> recip) {
    if (recip != null) {
        for (org.minig.imap.Address a : recip) {
            Element p = DOMUtils.createElement(recipients, "r");
            p.setAttribute("displayName", a.getDisplayName());
            String ma = a.getMail().replace("<", "").replace(">", "");
            p.setAttribute("addr", ma);
        }//from  www.  j  ava 2 s .  co  m
    }
}

From source file:fr.aliasource.webmail.proxy.impl.ResponderImpl.java

public void sendConversationIds(String[] convIds) {
    try {//from www.  j  a  va2  s. c o m
        Document doc = DOMUtils.createDoc("http://obm.aliasource.fr/xsd/conv_ids", "convIds");
        Element root = doc.getDocumentElement();
        for (String cid : convIds) {
            Element comp = DOMUtils.createElement(root, "c");
            comp.setAttribute("id", cid);
        }
        sendDom(doc);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:fr.aliasource.webmail.proxy.impl.ResponderImpl.java

public void sendCompletions(List<Completion> possibleCompletions) {
    try {/*from  ww w.  j a v a 2s . c o m*/
        Document doc = DOMUtils.createDoc("http://obm.aliasource.fr/xsd/completion", "completions");
        Element root = doc.getDocumentElement();
        for (Completion c : possibleCompletions) {
            Element comp = DOMUtils.createElement(root, "c");
            comp.setAttribute("d", c.getDisplayName());
            comp.setAttribute("v", c.getValue());
        }
        sendDom(doc);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:ca.mcgill.music.ddmal.mei.MeiXmlWriter.java

/**
 * Turn an MeiElement into a DOM node.//from   w  w w .  j  a  va 2  s  . c o m
 */
Node meiElementToNode(MeiElement e) {
    MeiNamespace ns = e.getNamespace();
    Element element;
    // Return a comment early, since it has no children or attributes
    if (e.getName().equals("#comment")) {
        return document.createComment(e.getValue());
    } else {
        element = document.createElementNS(ns.getHref(), e.getName());
    }
    element.setAttribute("xml:id", e.getId());
    // Attributes with or without namespaces
    // TODO: Currently namespaces prefixes are ns0, etc.
    for (MeiAttribute attr : e.getAttributes()) {
        MeiNamespace attrNs = attr.getNamespace();
        if (attrNs != null) {
            element.setAttributeNS(attrNs.getHref(), attr.getName(), attr.getValue());
        } else {
            element.setAttribute(attr.getName(), attr.getValue());
        }
    }

    if (e.getValue() != null) {
        element.appendChild(document.createTextNode(e.getValue()));
    }
    // Add each child. Tail text is represented in XML as a text node
    // after the child, so add that if it exists.
    for (MeiElement ch : e.getChildren()) {
        element.appendChild(meiElementToNode(ch));
        if (ch.getTail() != null) {
            element.appendChild(document.createTextNode(ch.getTail()));
        }
    }
    return element;
}

From source file:gool.generator.xml.XmlCodePrinter.java

/**
 * Translate GOOL node to XML Element./*from  w w  w. j a  va  2s  .  co m*/
 * 
 * @param node
 *            GOOL node
 * @param document
 *            XML Document
 * @return XML Element
 */
private Element NodeToElement(Object node, Document document) {
    // element create for return
    Element newElement = null;

    // check if the parameter does not cause trouble
    if (node == null || node.getClass().getName().equals("gool.generator.xml.XmlPlatform"))
        return null;
    if (node.getClass().isAssignableFrom(gool.ast.core.Node.class)) {
        return null;
    }
    if (node.getClass().getName().equals("gool.ast.core.ClassDef")) {
        if (classdefok)
            classdefok = false;
        else
            return null;
    }

    // Create the new Element
    newElement = document.createElement(node.getClass().getName().substring(9));

    // find every method to find every child node
    Method[] meths = node.getClass().getMethods();
    for (Method meth : meths) {
        Class<?> laCl = meth.getReturnType();
        // check if the method return type is a node.
        if (gool.ast.core.Node.class.isAssignableFrom(laCl)
                && (meth.getParameterTypes().length == 0) /* && nbNode<1000 */) {
            // Debug for recursion
            // nbNode++;
            // Log.d(laCl.getName() + "\n" + nbNode);
            try {
                gool.ast.core.Node newNode = (gool.ast.core.Node) meth.invoke(node);
                // detect recursion risk.
                boolean recursionRisk = (newNode == node) ? true : false;
                if (methexclude.containsKey(node.getClass().getName())) {
                    for (String exmeth : methexclude.get(node.getClass().getName())) {
                        if (newNode == null || meth.getName().equals(exmeth))
                            recursionRisk = true;
                    }
                }
                if (recursionRisk) {
                    Element newElement2 = document.createElement(node.getClass().getName().substring(9));
                    newElement2.setTextContent("recursion risk detected!");
                    newElement.appendChild(newElement2);
                } else {
                    Element el = NodeToElement(newNode, document);
                    if (el != null) {
                        el.setAttribute("getterName", meth.getName());
                        newElement.appendChild(el);
                    }
                }
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
        // if the method return node list.
        else if (java.util.List.class.isAssignableFrom(laCl)) {
            try {
                java.util.List<Object> listObj = (java.util.List<Object>) meth.invoke(node);
                for (Object o : listObj) {
                    Element el = NodeToElement(o, document);
                    if (el != null) {
                        el.setAttribute("getterName", meth.getName());
                        newElement.appendChild(el);
                    }
                }
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }

        }
        // generate XML attribute for getter
        else if (meth.getName().startsWith("get") && !meth.getName().equals("getCode")
                && (meth.getParameterTypes().length == 0)) {
            try {
                if (!attrexclude.contains(meth.getName()))
                    newElement.setAttribute(meth.getName().substring(3),
                            meth.invoke(node) == null ? "null" : meth.invoke(node).toString());
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
        // generate XML attribute for iser
        else if (meth.getName().startsWith("is") && (meth.getParameterTypes().length == 0)) {
            try {
                if (!attrexclude.contains(meth.getName()))
                    newElement.setAttribute(meth.getName().substring(2),
                            meth.invoke(node) == null ? "null" : meth.invoke(node).toString());
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
    }
    return newElement;
}

From source file:fr.aliasource.webmail.proxy.impl.ResponderImpl.java

public void sendFolderList(List<IFolder> folders) {
    try {/*from   www . ja  v a 2  s .  co m*/
        Document doc = DOMUtils.createDoc("http://obm.aliasource.fr/xsd/folder_list", "folderList");
        Element root = doc.getDocumentElement();
        for (IFolder f : folders) {
            Element fe = DOMUtils.createElement(root, "folder");
            fe.setAttribute("displayName", f.getDisplayName());
            fe.setAttribute("name", f.getName());
            fe.setAttribute("subscribed", String.valueOf(f.isSubscribed()));
            fe.setAttribute("shared", String.valueOf(f.isShared()));
        }
        sendDom(doc);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:fr.aliasource.webmail.proxy.impl.ResponderImpl.java

public void sendSummary(SortedMap<IFolder, Integer> summary) {
    try {/*from w  w  w .  java 2  s  . c o  m*/
        Document doc = DOMUtils.createDoc("http://obm.aliasource.fr/xsd/folder_list", "folderList");
        Element root = doc.getDocumentElement();
        for (IFolder f : summary.keySet()) {
            Element fe = DOMUtils.createElement(root, "folder");
            fe.setAttribute("displayName", f.getDisplayName());
            fe.setAttribute("name", f.getName());
            fe.setAttribute("unread", summary.get(f).toString());
        }

        sendDom(doc);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:net.sf.jabref.logic.msbib.MSBibDatabase.java

public Document getDOM() {
    Document document = null;//from   w w  w  .j  a  v a2 s.c om
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        document = documentBuilder.newDocument();

        Element rootNode = document.createElementNS(NAMESPACE, PREFIX + "Sources");
        rootNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", NAMESPACE);
        rootNode.setAttributeNS("http://www.w3.org/2000/xmlns/",
                "xmlns:" + PREFIX.substring(0, PREFIX.length() - 1), NAMESPACE);
        rootNode.setAttribute("SelectedStyle", "");

        for (MSBibEntry entry : entries) {
            Node node = entry.getDOM(document);
            rootNode.appendChild(node);
        }

        document.appendChild(rootNode);
    } catch (ParserConfigurationException e) {
        LOGGER.warn("Could not build XML document", e);
    }

    return document;
}

From source file:es.bsc.servicess.ide.actions.DeployAction.java

private void addLocalhostToResourcesFile(IJavaProject pr, String coreLocation)
        throws ParserConfigurationException, ConfigurationException, SAXException, IOException, CoreException,
        TransformerFactoryConfigurationError, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(getResourcesFileLocation(pr));

    Element resources = doc.getDocumentElement();
    Element worker = doc.createElement("Resource");
    worker.setAttribute("Name", "localhost");
    resources.appendChild(worker);//from   ww w .j av a  2  s . c o m

    Source source = new DOMSource(doc);

    IFolder output = pr.getProject().getFolder("output");

    if (output != null && output.exists()) {
        IFolder war_folder = output.getFolder(pr.getProject().getName());
        // Prepare the output file
        File file = (war_folder.getFile("resources.xml").getLocation().toFile());
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }

}

From source file:net.sf.jabref.logic.mods.MODSEntry.java

public Element getDOMrepresentation(Document d) {
    try {/*  www  .j ava 2  s  . c  o m*/
        Element mods = d.createElement(entryType);
        mods.setAttribute("version", "3.0");
        // mods.setAttribute("xmlns:xlink:", "http://www.w3.org/1999/xlink");
        // title
        if (title != null) {
            Element titleInfo = d.createElement("titleInfo");
            Element mainTitle = d.createElement("title");
            mainTitle.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(title)));
            titleInfo.appendChild(mainTitle);
            mods.appendChild(titleInfo);
        }
        if (authors != null) {
            for (PersonName name : authors) {
                Element modsName = d.createElement("name");
                modsName.setAttribute("type", "personal");
                if (name.getSurname() != null) {
                    Element namePart = d.createElement("namePart");
                    namePart.setAttribute("type", "family");
                    namePart.appendChild(
                            d.createTextNode(StringUtil.stripNonValidXMLCharacters(name.getSurname())));
                    modsName.appendChild(namePart);
                }
                if (name.getGivenNames() != null) {
                    Element namePart = d.createElement("namePart");
                    namePart.setAttribute("type", "given");
                    namePart.appendChild(
                            d.createTextNode(StringUtil.stripNonValidXMLCharacters(name.getGivenNames())));
                    modsName.appendChild(namePart);
                }
                Element role = d.createElement("role");
                Element roleTerm = d.createElement("roleTerm");
                roleTerm.setAttribute("type", "text");
                roleTerm.appendChild(d.createTextNode("author"));
                role.appendChild(roleTerm);
                modsName.appendChild(role);
                mods.appendChild(modsName);
            }
        }
        //publisher
        Element originInfo = d.createElement("originInfo");
        mods.appendChild(originInfo);
        if (this.publisher != null) {
            Element publisher = d.createElement(FieldName.PUBLISHER);
            publisher.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(this.publisher)));
            originInfo.appendChild(publisher);
        }
        if (date != null) {
            Element dateIssued = d.createElement("dateIssued");
            dateIssued.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(date)));
            originInfo.appendChild(dateIssued);
        }
        Element issuance = d.createElement("issuance");
        issuance.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(this.issuance)));
        originInfo.appendChild(issuance);

        if (id != null) {
            Element idref = d.createElement("identifier");
            idref.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(id)));
            mods.appendChild(idref);
            mods.setAttribute("ID", id);

        }
        Element typeOfResource = d.createElement("typeOfResource");
        String type = "text";
        typeOfResource.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(type)));
        mods.appendChild(typeOfResource);

        if (genre != null) {
            Element genreElement = d.createElement("genre");
            genreElement.setAttribute("authority", "marc");
            genreElement.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(genre)));
            mods.appendChild(genreElement);
        }

        if (host != null) {
            Element relatedItem = host.getDOMrepresentation(d);
            relatedItem.setAttribute("type", "host");
            mods.appendChild(relatedItem);
        }
        if (pages != null) {
            mods.appendChild(pages.getDOMrepresentation(d));
        }

        /* now generate extension fields for unhandled data */
        for (Map.Entry<String, String> theEntry : extensionFields.entrySet()) {
            Element extension = d.createElement("extension");
            String field = theEntry.getKey();
            String value = theEntry.getValue();
            if (handledExtensions.contains(field)) {
                continue;
            }
            Element theData = d.createElement(field);
            theData.appendChild(d.createTextNode(StringUtil.stripNonValidXMLCharacters(value)));
            extension.appendChild(theData);
            mods.appendChild(extension);
        }
        return mods;
    } catch (Exception e) {
        LOGGER.warn("Exception caught...", e);
        throw new Error(e);
    }
    // return result;
}