Example usage for org.w3c.dom Element getElementsByTagNameNS

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

Introduction

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

Prototype

public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Returns a NodeList of all the descendant Elements with a given local name and namespace URI in document order.

Usage

From source file:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasConceptTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    NodeList conceptElements = rootElement.getElementsByTagNameNS("*", "concepts");
    if (conceptElements.getLength() == 0)
        return false;
    else//from  www . ja v a 2s .  c  o m
        return true;
}

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

/**
 * @param pkg/* w w  w.j  a v  a 2  s. c o  m*/
 * @param bosOptions
 * @throws Exception 
 */
static void addCommentFileRelationship(ZipComponents zipComponents, BosConstructionOptions bosOptions)
        throws Exception {
    ZipComponent comp = zipComponents.getEntry(DocxConstants.DOCUMENT_XML_RELS_PATH);
    Document doc = zipComponents.getDomForZipComponent(bosOptions, comp);
    Element docElem = doc.getDocumentElement();
    NodeList nl = docElem.getElementsByTagNameNS(DocxConstants.RELS_NS, "Relationship");
    boolean foundCommentRel = false;
    for (int i = 0; i < nl.getLength(); i++) {
        Element elem = (Element) nl.item(i);
        String type = elem.getAttribute("Type");
        if (DocxConstants.COMMENT_REL_TYPE.equals(type)) {
            foundCommentRel = true;
            break;
        }
    }
    if (!foundCommentRel) {
        Element elem = doc.createElementNS(DocxConstants.RELS_NS, "Relationship");
        elem.setAttribute("Type", DocxConstants.COMMENT_REL_TYPE);
        elem.setAttribute("Id", "rId" + (nl.getLength() + 1));
        elem.setAttribute("Target", "comments.xml");
        docElem.appendChild(elem);
        // System.out.println(IOUtils.toString(DomUtil.serializeToInputStream(doc, "utf-8")));
        comp.setDom(doc);
    }

}

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

/**
 * @param commentsDom/* w  w  w  .  j a v  a2s  .  co  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);
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * Convenience method to update a template bean definition from overriding XML data.  
 * If <code>overrides</code> contains attribute <code>attribute</code> or a child element
 * with name <code>attribute</code>, transfer that
 * attribute as a bean property onto <code>template</code>, overwriting the default value.
 * @param reference if true, the value of the attribute is to be interpreted as a runtime bean name reference; otherwise it is interpreted as a literal value
 *///from  w  ww. ja v  a  2  s . c o m
public static boolean overrideProperty(String attribute, BeanDefinition template, Element overrides,
        boolean reference) {
    Object value = null;
    if (overrides.hasAttribute(attribute)) {
        value = overrides.getAttribute(attribute);
    } else {
        NodeList children = overrides.getElementsByTagNameNS("*", attribute);
        if (children.getLength() == 1) {
            Element child = (Element) children.item(0);
            value = child.getTextContent();
        }
    }

    if (value != null) {
        if (reference)
            value = new RuntimeBeanReference(value.toString());

        String propName = Conventions.attributeNameToPropertyName(attribute);
        MutablePropertyValues props = template.getPropertyValues();
        props.removePropertyValue(propName);
        props.addPropertyValue(propName, value);
        return true;
    }

    return false;
}

From source file:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasPrevQueryTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    // <query_master_id> indicates PrevQuery 
    NodeList pqElements = rootElement.getElementsByTagNameNS("*", "query_master");
    if (pqElements.getLength() == 0)
        return false;
    else/* w  ww. ja  v  a2 s .co  m*/
        return true;
}

From source file:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasGroupTemplateTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    // <query_master_id> indicates PrevQuery 
    NodeList gtElements = rootElement.getElementsByTagNameNS("*", "panel");
    if (gtElements.getLength() == 0)
        return false;
    else// ww  w.  j ava 2 s  .c  o m
        return true;
}

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

/**
 * @param zipComponents/*from  w  w  w. j a v  a 2s.  com*/
 * @param bosOptions
 * @throws Exception 
 */
public static void addCommentFileContentType(ZipComponents zipComponents, BosConstructionOptions bosOptions)
        throws Exception {

    /*
     *   <Override
    PartName="/word/comments.xml"
    ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>
            
     */

    ZipComponent comp = zipComponents.getEntry("[Content_Types].xml");
    Document doc = zipComponents.getDomForZipComponent(bosOptions, comp);
    Element docElem = doc.getDocumentElement();
    String contentTypesNs = "http://schemas.openxmlformats.org/package/2006/content-types";
    NodeList nl = docElem.getElementsByTagNameNS(contentTypesNs, "Override");
    boolean foundCommentType = false;
    for (int i = 0; i < nl.getLength(); i++) {
        Element elem = (Element) nl.item(i);
        String partName = elem.getAttribute("PartName");
        if (DocxConstants.COMMENTS_PARTNAME.equals(partName)) {
            foundCommentType = true;
            break;
        }
    }
    if (!foundCommentType) {
        Element elem = doc.createElementNS(contentTypesNs, "Override");
        elem.setAttribute("PartName", DocxConstants.COMMENTS_PARTNAME);
        elem.setAttribute("ContentType", DocxConstants.COMMENTS_CONTENT_TYPE);
        docElem.appendChild(elem);
        comp.setDom(doc);
    }
}

From source file:filehandling.FilePreprocessor.java

public static List<FileData> extractMDFilesFromXMLEmbedding(byte[] bytes, String basename, String extension) {

    List<FileData> returnList = new ArrayList<FileData>();
    try {/*  w  w w .j a  v  a2  s  . c  o  m*/
        FileUtils.writeByteArrayToFile(new File("/tmp/debugData/instreamBeforeExtraction.xml"), bytes);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbf.newDocumentBuilder();

        Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(bytes)));

        if (Master.DEBUG_LEVEL > Master.LOW) {
            System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
        }

        NodeList entryElements = doc.getElementsByTagNameNS("http://www.w3.org/2005/Atom", "entry");
        for (int i = 0; i < entryElements.getLength(); i++) {
            Element entryElement = (Element) entryElements.item(i);
            FileData fd = null;
            /*
             * First try to find a <link> element pointing to Metadata as
             * this should be included by default
             */
            if (fd == null) {
                NodeList linkElements = entryElement.getElementsByTagNameNS("http://www.w3.org/2005/Atom",
                        "link");
                String iso19139Link = null;
                String iso19139_2Link = null;
                for (int j = 0; j < linkElements.getLength(); j++) {
                    Element linkElement = (Element) linkElements.item(j);
                    String relAttrValue = linkElement.getAttribute("rel");
                    String typeAttributeValue = linkElement.getAttribute("type");
                    if (relAttrValue != null && relAttrValue.equals("alternate")
                            && typeAttributeValue != null) {
                        switch (typeAttributeValue) {
                        case "application/vnd.iso.19139+xml":
                            iso19139Link = linkElement.getAttribute("href");
                            break;
                        case "application/vnd.iso.19139-2+xml":
                            iso19139_2Link = linkElement.getAttribute("href");
                            break;
                        }
                    }
                }
                /* iso19139-2 gets priority */
                String url = iso19139_2Link != null ? iso19139_2Link : iso19139Link;
                if (url != null) {
                    try (InputStream is = FileFetcher.fetchFileFromUrl(url)) {
                        Document doc2 = dBuilder.parse(new InputSource(is));
                        fd = processMDList(doc2.getDocumentElement(), extension, url);
                    }
                }
            }
            /*
             * Fallback to finding Metadata embedded directly in <entry>.
             * There will be either MI_Metadata or MD_Metadata, not both
             */
            if (fd == null) {
                fd = processMDList(entryElement.getElementsByTagName("gmi:MI_Metadata").item(0), extension,
                        null);
            }
            if (fd == null) {
                fd = processMDList(entryElement.getElementsByTagName("gmd:MD_Metadata").item(0), extension,
                        null);
            }

            if (fd != null) {
                returnList.add(fd);
            }
        }
    } catch (Exception e) {
        // TODO: handle exception

        if (Master.DEBUG_LEVEL > Master.LOW)
            System.out.println(e.getLocalizedMessage());
        GUIrefs.displayAlert("Error in FilePreprocessor.extractMDFilesFromXMLEmbedding"
                + StringUtilities.escapeQuotes(e.getMessage()));
    }
    return returnList;
}

From source file:be.fedict.eid.dss.spi.utils.XAdESSignatureTimeStampValidation.java

/**
 * Checks the time-stamp tokens' digital signatures.
 * //from w w  w  .jav  a 2 s . c  o m
 * @param signatureTimeStamp
 * @param signatureElement
 * @return
 * @throws XAdESValidationException
 */
public static List<TimeStampToken> verify(XAdESTimeStampType signatureTimeStamp, Element signatureElement)
        throws XAdESValidationException {

    LOG.debug("validate SignatureTimeStamp...");

    List<TimeStampToken> timeStampTokens = XAdESUtils.getTimeStampTokens(signatureTimeStamp);
    if (timeStampTokens.isEmpty()) {
        LOG.error("No timestamp tokens present in SignatureTimeStamp");
        throw new XAdESValidationException("No timestamp tokens present in SignatureTimeStamp");
    }

    // 2. take ds:SignatureValue element
    NodeList signatureValueNodeList = signatureElement.getElementsByTagNameNS(XMLSignature.XMLNS,
            "SignatureValue");
    if (0 == signatureValueNodeList.getLength()) {
        LOG.error("no XML signature valuefound");
        throw new XAdESValidationException("no XML signature valuefound");
    }

    // 3. canonicalize using CanonicalizationMethod if any, else take dsig's
    TimeStampDigestInput digestInput = new TimeStampDigestInput(
            signatureTimeStamp.getCanonicalizationMethod().getAlgorithm());
    digestInput.addNode(signatureValueNodeList.item(0));

    for (TimeStampToken timeStampToken : timeStampTokens) {

        // 1. verify signature in timestamp token
        XAdESUtils.verifyTimeStampTokenSignature(timeStampToken);

        // 4. for-each timestamp token, compute digest and compare
        XAdESUtils.verifyTimeStampTokenDigest(timeStampToken, digestInput);
    }

    return timeStampTokens;
}

From source file:edu.harvard.i2b2.eclipse.plugins.workplace.util.XmlUtil.java

public static String getName(XmlValueType xml) {
    String name = null;//w  w w.  j  a  v a 2 s.c o  m
    Element rootElement = xml.getAny().get(0);
    NodeList nameElements = rootElement.getElementsByTagName("name");
    // Group templates dont have tag 'name'
    if (nameElements.getLength() == 0) {
        nameElements = rootElement.getElementsByTagNameNS("*", "panel");
        if (nameElements.getLength() == 0) {
            nameElements = rootElement.getElementsByTagName("query_name");
            if (nameElements.getLength() == 0) {
                // if we get to here and no name has been found then its a PDO.
                // return generically -- change to obs or event etc one level up.
                return "PDO";
            } else {
                name = nameElements.item(0).getTextContent();
            }
        } else {
            name = nameElements.item(0).getAttributes().getNamedItem("name").getNodeValue();
        }
        if (name != null)
            return name;
        // Default to ABC if we cant find a name at all.
        else
            return "ABC" + MessageUtil.getInstance().getTimestamp();
    }
    // append result_instance_id to PATIENT_COUNT_XML <name> to create unique name

    else if ((nameElements.item(0).getTextContent().equals("PATIENT_COUNT_XML"))) {
        NodeList resultElements = rootElement.getElementsByTagName("result_instance_id");
        if (resultElements.getLength() > 0) {
            String resultInstanceId = resultElements.item(0).getTextContent();
            return nameElements.item(0).getTextContent() + "_" + resultInstanceId;
        }
    }

    else if ((nameElements.item(0).getTextContent().equals("PATIENTSET"))
            || (nameElements.item(0).getTextContent().equals("PATIENT_ENCOUNTER_SET"))
            || (nameElements.item(0).getTextContent().equals("PATIENT_GENDER_COUNT_XML"))
            || (nameElements.item(0).getTextContent().equals("PATIENT_AGE_COUNT_XML"))
            || (nameElements.item(0).getTextContent().equals("PATIENT_VITALSTATUS_COUNT_XML"))
            || (nameElements.item(0).getTextContent().equals("PATIENT_RACE_COUNT_XML"))) {
        NodeList resultElements = rootElement.getElementsByTagName("description");
        if (resultElements.getLength() > 0) {
            return resultElements.item(0).getTextContent();
        }
    }

    return nameElements.item(0).getTextContent();
}