Example usage for org.w3c.dom Element getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:com.enonic.esl.xml.XMLTool.java

public static Element renameElement(Element elem, String newName) {
    Document doc = elem.getOwnerDocument();

    // Create an element with the new name
    Element elem2 = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        elem2.getAttributes().setNamedItem(attr2);
    }/*  w ww  .  j  a v  a 2s. co m*/

    // Move all the children
    while (elem.hasChildNodes()) {
        elem2.appendChild(elem.getFirstChild());
    }

    // Replace the old node with the new node
    elem.getParentNode().replaceChild(elem2, elem);

    return elem2;
}

From source file:MSUmpire.SearchResultParser.PepXMLParseHandler.java

private void ParseSearchSummary(Element node)
        throws XmlPullParserException, XmlPullParserException, IOException {
    if (node.getAttributes().getNamedItem("search_engine") != null) {
        singleLCMSID.SearchEngine = node.getAttributes().getNamedItem("search_engine").getNodeValue();
    }//from ww w. j a  va  2  s .com
    if (node.getAttributes().getNamedItem("msDetector") != null) {
        singleLCMSID.msDetector = node.getAttributes().getNamedItem("msDetector").getNodeValue();
    }
    if (node.getAttributes().getNamedItem("msIonization") != null) {
        singleLCMSID.msIonization = node.getAttributes().getNamedItem("msIonization").getNodeValue();
    }
    if (node.getAttributes().getNamedItem("msManufacturer") != null) {
        singleLCMSID.msManufacturer = node.getAttributes().getNamedItem("msManufacturer").getNodeValue();
    }
    if (node.getAttributes().getNamedItem("msMassAnalyzer") != null) {
        singleLCMSID.msMassAnalyzer = node.getAttributes().getNamedItem("msMassAnalyzer").getNodeValue();
    }
    if (node.getAttributes().getNamedItem("msModel") != null) {
        singleLCMSID.msModel = node.getAttributes().getNamedItem("msModel").getNodeValue();
    }
    for (int k = 0; k < node.getChildNodes().getLength(); k++) {
        if ("search_database".equals(node.getChildNodes().item(k).getNodeName())) {
            singleLCMSID.DataBase = node.getChildNodes().item(k).getAttributes().getNamedItem("local_path")
                    .getNodeValue();
        }
        if ("aminoacid_modification".equals(node.getChildNodes().item(k).getNodeName())) {
            if (node.getChildNodes().item(k).getAttributes().getNamedItem("aminoacid") != null) {
                String site = node.getChildNodes().item(k).getAttributes().getNamedItem("aminoacid")
                        .getNodeValue();
                float mass = (float) Math.round(Float.parseFloat(
                        node.getChildNodes().item(k).getAttributes().getNamedItem("mass").getNodeValue())
                        * 1000) / 1000;
                float massdiff2 = Float.parseFloat(
                        node.getChildNodes().item(k).getAttributes().getNamedItem("massdiff").getNodeValue());
                AminoAcid aa = AminoAcid.getAminoAcid(site.charAt(0));
                float massdiff = mass - (float) aa.monoisotopicMass;

                if (massdiff != 0f && Math.abs(massdiff - massdiff2) < 0.1f) {
                    PTM ptm = PTMManager.GetInstance().GetPTM(site, massdiff);
                    if (ptm == null) {
                        Logger.getRootLogger().warn("Warning! modification in pepxml : amino acid " + site
                                + "(mass diff:" + massdiff + ") doesn't exist in the library.");
                    } else {
                        singleLCMSID.AddModification(ptm, site);
                    }
                } else {
                    if (Math.abs(massdiff2 + 17.0265) < 0.01 && Math.abs(mass - 143.0041f) < 0.001
                            && "C".equals(site)) {
                        PTM ptm = PTMManager.GetInstance().GetPTM(site, massdiff);
                        if (ptm == null) {
                            Logger.getRootLogger().warn("Warning! modification in pepxml : amino acid " + site
                                    + "(mass diff:" + massdiff + ") doesn't exist in the library.");
                        } else {
                            singleLCMSID.AddModification(ptm, site);
                        }
                    } else {
                        Logger.getRootLogger().warn("Warning! modification in pepxml : amino acid " + site
                                + "(mass: " + mass + ", massdiff:" + massdiff2 + ") ignored.");
                    }
                }
            }
        }
        if ("terminal_modification".equals(node.getChildNodes().item(k).getNodeName())) {

            if (node.getChildNodes().item(k).getAttributes().getNamedItem("terminus") != null) {
                String site = "";
                if ("c".equals(node.getChildNodes().item(k).getAttributes().getNamedItem("terminus")
                        .getNodeValue().toLowerCase())) {
                    site = "C-term";
                }
                if ("n".equals(node.getChildNodes().item(k).getAttributes().getNamedItem("terminus")
                        .getNodeValue().toLowerCase())) {
                    site = "N-term";
                }
                float massdiff = Float.parseFloat(
                        node.getChildNodes().item(k).getAttributes().getNamedItem("massdiff").getNodeValue());
                PTM ptm = PTMManager.GetInstance().GetPTM(site, massdiff);
                if (ptm == null) {
                    Logger.getRootLogger().warn("Warning! term-modification:" + site + "(" + massdiff
                            + ") doesn't exist in the library.\n");
                } else {
                    singleLCMSID.AddModification(ptm, site);
                }
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.impl.AbstractODataBinder.java

@Override
public ODataProperty getProperty(final Element property) {
    final ODataProperty res;

    final Node nullNode = property.getAttributes().getNamedItem(ODataConstants.ATTR_NULL);

    if (nullNode == null) {
        final EdmType edmType = StringUtils.isBlank(property.getAttribute(ODataConstants.ATTR_M_TYPE)) ? null
                : newEdmType(property.getAttribute(ODataConstants.ATTR_M_TYPE));

        final PropertyType propType = edmType == null ? guessPropertyType(property)
                : edmType.isCollection() ? PropertyType.COLLECTION
                        : edmType.isSimpleType() ? PropertyType.PRIMITIVE : PropertyType.COMPLEX;

        switch (propType) {
        case COLLECTION:
            res = fromCollectionPropertyElement(property, edmType);
            break;

        case COMPLEX:
            res = fromComplexPropertyElement(property, edmType);
            break;

        case PRIMITIVE:
            res = fromPrimitivePropertyElement(property, edmType);
            break;

        case EMPTY:
        default:/*from   w  w  w .  ja v a 2 s  . c o m*/
            res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
        }
    } else {
        res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
    }

    return res;
}

From source file:com.msopentech.odatajclient.engine.data.AbstractODataBinder.java

@Override
public ODataProperty getProperty(final Element property) {
    final ODataProperty res;

    final Node nullNode = property.getAttributes().getNamedItem(ODataConstants.ATTR_NULL);

    if (nullNode == null) {
        final EdmType edmType = StringUtils.isBlank(property.getAttribute(ODataConstants.ATTR_M_TYPE)) ? null
                : newEdmType(property.getAttribute(ODataConstants.ATTR_M_TYPE));

        final PropertyType propType = edmType == null ? guessPropertyType(property)
                : edmType.isCollection() ? PropertyType.COLLECTION
                        : edmType.isSimpleType() ? PropertyType.PRIMITIVE : PropertyType.COMPLEX;

        switch (propType) {
        case COLLECTION:
            res = fromCollectionPropertyElement(property, edmType);
            break;

        case COMPLEX:
            res = fromComplexPropertyElement(property, edmType);
            break;

        case PRIMITIVE:
            res = fromPrimitivePropertyElement(property, edmType);
            break;

        case EMPTY:
        default:/*from   www.j  a v  a2 s.  c o  m*/
            res = ODataObjectFactory.newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
        }
    } else {
        res = ODataObjectFactory.newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
    }

    return res;
}

From source file:com.msopentech.odatajclient.engine.data.impl.v3.AtomSerializer.java

private Element entry(final AtomEntry entry) throws ParserConfigurationException {
    final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element entryElem = doc.createElement(ODataConstants.ATOM_ELEM_ENTRY);
    entryElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    entryElem.setAttribute(ODataConstants.XMLNS_METADATA,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA));
    entryElem.setAttribute(ODataConstants.XMLNS_DATASERVICES,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES));
    entryElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    entryElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (entry.getBaseURI() != null) {
        entryElem.setAttribute(ODataConstants.ATTR_XMLBASE, entry.getBaseURI().toASCIIString());
    }//from  w  ww  .  ja v  a 2 s.c  om
    doc.appendChild(entryElem);

    final Element category = doc.createElement(ODataConstants.ATOM_ELEM_CATEGORY);
    category.setAttribute(ODataConstants.ATOM_ATTR_TERM, entry.getType());
    category.setAttribute(ODataConstants.ATOM_ATTR_SCHEME,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_SCHEME));
    entryElem.appendChild(category);

    if (StringUtils.isNotBlank(entry.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(entry.getTitle()));
        entryElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(entry.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(entry.getSummary()));
        entryElem.appendChild(summary);
    }

    setLinks(entryElem, entry.getAssociationLinks());
    setLinks(entryElem, entry.getNavigationLinks());
    setLinks(entryElem, entry.getMediaEditLinks());

    final Element content = doc.createElement(ODataConstants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            content.setAttribute(ODataConstants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            content.setAttribute(ODataConstants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        if (content.getAttributes().getLength() > 0) {
            entryElem.appendChild(content);
        }

        if (entry.getMediaEntryProperties() != null) {
            entryElem.appendChild(doc.importNode(entry.getMediaEntryProperties(), true));
        }
    } else {
        content.setAttribute(ODataConstants.ATTR_TYPE, ContentType.APPLICATION_XML.getMimeType());
        if (entry.getContent() != null) {
            content.appendChild(doc.importNode(entry.getContent(), true));
        }
        entryElem.appendChild(content);
    }

    return entryElem;
}

From source file:jp.igapyon.selecrawler.SeleCrawlerWebContentTrimmer.java

public void processElement(final Element element) throws IOException {
    final NodeList nodeList = element.getChildNodes();
    for (int index = nodeList.getLength() - 1; index >= 0; index--) {
        final Node node = nodeList.item(index);
        if (node instanceof Element) {
            final Element lookup = (Element) node;

            if ("script".equals(lookup.getTagName())) {
                // REMOVE script tag.
                element.removeChild(node);
                continue;
            }/*ww  w. j av  a  2s .  c  o m*/

            if ("noscript".equals(lookup.getTagName())) {
                // REMOVE noscript tag.
                element.removeChild(node);
                continue;
            }

            if ("iframe".equals(lookup.getTagName())) {
                final NamedNodeMap nnm = lookup.getAttributes();
                for (int indexNnm = 0; indexNnm < nnm.getLength(); indexNnm++) {
                    final Attr attr = (Attr) nnm.item(indexNnm);

                    // System.out.println(" " + attr.getName() + " [" +
                    // attr.getValue() + "]");
                    if ("style".equals(attr.getName())) {
                        final String value = attr.getValue().replaceAll(" ", "");
                        if (value.indexOf("display:none") >= 0) {
                            // REMOVE iframe tag which is display:none
                            // style..
                            element.removeChild(node);
                            continue;
                        }
                    }
                }
            }

            processElement(lookup);
        }
    }
}

From source file:com.espertech.esper.client.ConfigurationParser.java

private static void handleMap(String name, Configuration configuration, Element eventTypeElement) {
    ConfigurationEventTypeMap config;/*from w w w.  j a  v a2 s  .  co  m*/
    String startTimestampProp = getOptionalAttribute(eventTypeElement, "start-timestamp-property-name");
    String endTimestampProp = getOptionalAttribute(eventTypeElement, "end-timestamp-property-name");
    Node superTypesList = eventTypeElement.getAttributes().getNamedItem("supertype-names");
    if (superTypesList != null || startTimestampProp != null || endTimestampProp != null) {
        config = new ConfigurationEventTypeMap();
        if (superTypesList != null) {
            String value = superTypesList.getTextContent();
            String[] names = value.split(",");
            for (String superTypeName : names) {
                config.getSuperTypes().add(superTypeName.trim());
            }
        }
        config.setEndTimestampPropertyName(endTimestampProp);
        config.setStartTimestampPropertyName(startTimestampProp);
        configuration.addMapConfiguration(name, config);
    }

    Properties propertyTypeNames = new Properties();
    NodeList propertyList = eventTypeElement.getElementsByTagName("map-property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        String nameProperty = getRequiredAttribute(propertyList.item(i), "name");
        String clazz = getRequiredAttribute(propertyList.item(i), "class");
        propertyTypeNames.put(nameProperty, clazz);
    }
    configuration.addEventType(name, propertyTypeNames);
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static boolean compareElement(Element a, Element b, boolean considerNamespacePrefixes) {
    if (a == b) {
        return true;
    }/*from  w w  w . j a v  a 2s  .c om*/
    if (a == null && b == null) {
        return true;
    }
    if (a == null || b == null) {
        return false;
    }
    if (!getQName(a).equals(getQName(b))) {
        return false;
    }
    if (!compareAttributes(a.getAttributes(), b.getAttributes(), considerNamespacePrefixes)) {
        return false;
    }
    if (!compareNodeList(a.getChildNodes(), b.getChildNodes(), considerNamespacePrefixes)) {
        return false;
    }
    return true;
}

From source file:com.nridge.core.base.io.xml.DataTableXML.java

/**
 * Parses an XML DOM element and loads it into a bag/table.
 *
 * @param anElement DOM element.//w  w w . j ava  2 s.c om
 * @throws java.io.IOException I/O related exception.
 */
@Override
public void load(Element anElement) throws IOException {
    Node nodeItem;
    Attr nodeAttr;
    Element nodeElement;
    String nodeName, nodeValue, attrValue;

    attrValue = anElement.getAttribute("name");
    if (StringUtils.isNotEmpty(attrValue))
        mDataTable.setName(attrValue);

    NamedNodeMap namedNodeMap = anElement.getAttributes();
    int attrCount = namedNodeMap.getLength();
    for (int attrOffset = 0; attrOffset < attrCount; attrOffset++) {
        nodeAttr = (Attr) namedNodeMap.item(attrOffset);
        nodeName = nodeAttr.getNodeName();
        nodeValue = nodeAttr.getNodeValue();

        if (StringUtils.isNotEmpty(nodeValue)) {
            if (!StringUtils.equalsIgnoreCase(nodeName, "name"))
                mDataTable.addFeature(nodeName, nodeValue);
        }
    }

    NodeList nodeList = anElement.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        nodeItem = nodeList.item(i);

        if (nodeItem.getNodeType() != Node.ELEMENT_NODE)
            continue;

        nodeName = nodeItem.getNodeName();
        if (nodeName.equalsIgnoreCase("Context")) {
            nodeElement = (Element) nodeItem;
            attrValue = nodeElement.getAttribute("start");
            if (StringUtils.isNumeric(attrValue))
                mContextStart = Integer.parseInt(attrValue);
            attrValue = nodeElement.getAttribute("limit");
            if (StringUtils.isNumeric(attrValue))
                mContextLimit = Integer.parseInt(attrValue);
            attrValue = nodeElement.getAttribute("total");
            if (StringUtils.isNumeric(attrValue))
                mContextTotal = Integer.parseInt(attrValue);
        } else if (nodeName.equalsIgnoreCase("Columns")) {
            nodeElement = (Element) nodeItem;
            DataBagXML dataBagXML = new DataBagXML();
            dataBagXML.load(nodeElement);
            DataBag dataBag = dataBagXML.getBag();
            dataBag.setName(mDataTable.getName());
            mDataTable = new DataTable(dataBag);
        } else if (nodeName.equalsIgnoreCase("Rows")) {
            nodeElement = (Element) nodeItem;
            loadRows(nodeElement);
        }
    }
}

From source file:com.espertech.esper.client.ConfigurationParser.java

private static void handleObjectArray(String name, Configuration configuration, Element eventTypeElement) {
    ConfigurationEventTypeObjectArray config;
    String startTimestampProp = getOptionalAttribute(eventTypeElement, "start-timestamp-property-name");
    String endTimestampProp = getOptionalAttribute(eventTypeElement, "end-timestamp-property-name");
    Node superTypesList = eventTypeElement.getAttributes().getNamedItem("supertype-names");
    if (superTypesList != null || startTimestampProp != null || endTimestampProp != null) {
        config = new ConfigurationEventTypeObjectArray();
        if (superTypesList != null) {
            String value = superTypesList.getTextContent();
            String[] names = value.split(",");
            for (String superTypeName : names) {
                config.getSuperTypes().add(superTypeName.trim());
            }/*w  w  w . ja v a2 s  . c o  m*/
        }
        config.setEndTimestampPropertyName(endTimestampProp);
        config.setStartTimestampPropertyName(startTimestampProp);
        configuration.addObjectArrayConfiguration(name, config);
    }

    List<String> propertyNames = new ArrayList<String>();
    List<Object> propertyTypes = new ArrayList<Object>();
    NodeList propertyList = eventTypeElement.getElementsByTagName("objectarray-property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        String nameProperty = getRequiredAttribute(propertyList.item(i), "name");
        String clazz = getRequiredAttribute(propertyList.item(i), "class");
        propertyNames.add(nameProperty);
        propertyTypes.add(clazz);
    }
    configuration.addEventType(name, propertyNames.toArray(new String[propertyNames.size()]),
            propertyTypes.toArray());
}