Search in sources :

Example 26 with XPathExpression

use of javax.xml.xpath.XPathExpression in project ORCID-Source by ORCID.

the class IdentityProviderManagerImpl method createEntityFromXml.

@Override
public IdentityProviderEntity createEntityFromXml(Element idpElement) {
    XPath xpath = createXPath();
    XPathExpression mainDisplayNameXpath = compileXPath(xpath, "string(.//md:IDPSSODescriptor//mdui:DisplayName[1])");
    XPathExpression displayNamesXpath = compileXPath(xpath, ".//md:IDPSSODescriptor//mdui:DisplayName");
    XPathExpression legacyMainDisplayNameXpath = compileXPath(xpath, "string(.//md:OrganizationDisplayName[1])");
    XPathExpression legacyDisplayNamesXpath = compileXPath(xpath, ".//md:OrganizationDisplayName");
    XPathExpression supportContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='support'])[1]/md:EmailAddress[1])");
    XPathExpression adminContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='administrative'])[1]/md:EmailAddress[1])");
    XPathExpression techContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='technical'])[1]/md:EmailAddress[1])");
    String entityId = idpElement.getAttribute("entityID");
    String mainDisplayName = evaluateXPathString(mainDisplayNameXpath, idpElement);
    if (StringUtils.isBlank(mainDisplayName)) {
        mainDisplayName = evaluateXPathString(legacyMainDisplayNameXpath, idpElement);
    }
    String supportEmail = tidyEmail(evaluateXPathString(supportContactXpath, idpElement));
    String adminEmail = tidyEmail(evaluateXPathString(adminContactXpath, idpElement));
    String techEmail = tidyEmail(evaluateXPathString(techContactXpath, idpElement));
    List<IdentityProviderNameEntity> nameEntities = createNameEntitiesFromXml(displayNamesXpath, idpElement);
    if (nameEntities == null || nameEntities.isEmpty()) {
        nameEntities = createNameEntitiesFromXml(legacyDisplayNamesXpath, idpElement);
    }
    IdentityProviderEntity identityProviderEntity = new IdentityProviderEntity();
    identityProviderEntity.setProviderid(entityId);
    identityProviderEntity.setDisplayName(mainDisplayName);
    identityProviderEntity.setNames(nameEntities);
    identityProviderEntity.setSupportEmail(supportEmail);
    identityProviderEntity.setAdminEmail(adminEmail);
    identityProviderEntity.setTechEmail(techEmail);
    return identityProviderEntity;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity)

Example 27 with XPathExpression

use of javax.xml.xpath.XPathExpression in project Tundra by Permafrost.

the class xpath method exists.

// ---( server methods )---
public static final void exists(IData pipeline) throws ServiceException {
    // --- <<IS-START(exists)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [o] field:0:required $exists?
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        IDataHelper.put(cursor, "$exists?", XPathHelper.exists(node, compiledExpression), String.class);
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset)

Example 28 with XPathExpression

use of javax.xml.xpath.XPathExpression in project Tundra by Permafrost.

the class xpath method get.

public static final void get(IData pipeline) throws ServiceException {
    // --- <<IS-START(get)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [i] field:0:optional $recurse? {"false","true"}
    // [o] record:1:optional $nodes
    // [o] - object:0:required node
    // [o] - field:0:required name.qualified
    // [o] - field:0:optional name.local
    // [o] - field:0:optional name.prefix
    // [o] - field:0:optional name.uri
    // [o] - field:0:required type
    // [o] - field:0:optional value
    // [o] - record:1:optional attributes
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] - record:1:optional elements
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] field:0:required $nodes.length
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        boolean recurse = IDataHelper.getOrDefault(cursor, "$recurse?", Boolean.class, false);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        Nodes nodes = XPathHelper.get(node, compiledExpression);
        if (nodes != null) {
            IDataHelper.put(cursor, "$nodes", nodes.reflect(namespace, recurse));
            IDataHelper.put(cursor, "$nodes.length", nodes.size(), String.class);
        } else {
            IDataHelper.put(cursor, "$nodes.length", "0");
        }
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset) Nodes(permafrost.tundra.xml.dom.Nodes)

Example 29 with XPathExpression

use of javax.xml.xpath.XPathExpression in project jangaroo-tools by CoreMedia.

the class ASDocScreenScraper method appendParamsOrReturnOrThrows.

private void appendParamsOrReturnOrThrows(String atMode, StringBuilder writer, Node node) throws XPathExpressionException {
    XPathExpression paramNameExpression = xPathFactory.newXPath().compile(".//*[name()='span'][@class='label']");
    XPathExpression errorTypeExpression = xPathFactory.newXPath().compile(".//*[name()='a']");
    NodeList paramRowNodes = node.getChildNodes();
    for (int i = 0; i < paramRowNodes.getLength(); i++) {
        Node paramRowNode = paramRowNodes.item(i);
        String firstTdClass = getAttributeValueIfMatches(paramRowNode.getFirstChild(), "td", "class");
        if ("paramSpacer".equals(firstTdClass)) {
            continue;
        }
        String key = "@param".equals(atMode) ? ((String) paramNameExpression.evaluate(paramRowNode, XPathConstants.STRING)).trim() : "@throws".equals(atMode) ? toType((Node) errorTypeExpression.evaluate(paramRowNode, XPathConstants.NODE)) : "";
        // unparse second td (first is only a spacer)
        String text = unparse(paramRowNode.getChildNodes().item(1), true);
        int mdashIndex = text.indexOf("� ");
        if (mdashIndex != -1) {
            text = text.substring(mdashIndex + "� ".length());
        }
        writer.append("\n * ").append(atMode).append(" ");
        if (key.length() > 0) {
            writer.append(key).append(' ');
        }
        writer.append(text);
    }
    writer.append("\n * ");
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 30 with XPathExpression

use of javax.xml.xpath.XPathExpression in project jangaroo-tools by CoreMedia.

the class ASDocScreenScraper method getASDoc.

private String getASDoc(NodeList classDocNodes, Node eventNode) throws XPathExpressionException {
    if (classDocNodes.getLength() > 0) {
        StringBuilder writer = new StringBuilder();
        writer.append("/**\n");
        writer.append(" * ");
        boolean first = true;
        String atMode = null;
        XPathExpression atModeExpression = xPathFactory.newXPath().compile("*[name()='span'][@class='label' or @class='classHeaderTableLabel']");
        XPathExpression runtimeOrLanguageVersionExpression = xPathFactory.newXPath().compile(".//*[name()='td']/*[name()='b'][contains(text(),'Language Version')] | .//*[name()='td']/*[name()='b'][contains(text(), 'Runtime Version')]");
        for (int i = 0; i < classDocNodes.getLength(); i++) {
            Node node = classDocNodes.item(i);
            if (first) {
                // skip initial <code> element:
                if ("code".equals(node.getNodeName())) {
                    // also skip following text node:
                    if (node.getNextSibling() != null && "#text".equals(node.getNextSibling().getNodeName())) {
                        ++i;
                    }
                    continue;
                } else if (isP(node) && node.getFirstChild() == null) {
                    // skip all initial empty <p>s:
                    continue;
                }
            }
            if (IGNORE_UNTIL_BR.equals(atMode)) {
                if ("br".equals(node.getNodeName())) {
                    atMode = null;
                }
            } else if ("@see".equals(atMode)) {
                appendSeeAlsos(writer, node);
                atMode = null;
            } else if ("@example".equals(atMode)) {
                writer.append("\n * @example ").append(unparse(node, true));
                atMode = null;
            } else if ("@param".equals(atMode) || "@return".equals(atMode) || "@throws".equals(atMode)) {
                if ("table".equals(node.getNodeName())) {
                    appendParamsOrReturnOrThrows(atMode, writer, node);
                    atMode = null;
                }
            } else {
                if ("classHeaderTable".equals(getAttributeValueIfMatches(node, "table", "class"))) {
                    continue;
                }
                Node runtimeOrLanguageVersionNode = (Node) runtimeOrLanguageVersionExpression.evaluate(node, XPathConstants.NODE);
                if (runtimeOrLanguageVersionNode != null) {
                    atMode = null;
                    // so far, skip runtime and language version information.
                    continue;
                }
                Node atModeNode = (Node) atModeExpression.evaluate(node, XPathConstants.NODE);
                String newAtMode = determineAtMode(atModeNode == null ? node : atModeNode);
                if (newAtMode != null) {
                    atMode = newAtMode;
                } else if (!(atMode != null && atMode.startsWith(IGNORE_UNTIL))) {
                    if (eventNode != null && !first && "#text".equals(node.getNodeName())) {
                        // detected copied ASDoc from event type constant, see http://livedocs.adobe.com/flex/3/html/help.html?content=asdoc_4.html "documenting ... events":
                        break;
                    }
                    String nodeValue = unparse(node, first && isP(node));
                    if (nodeValue.length() > 0) {
                        first = false;
                        writer.append(nodeValue);
                    }
                }
            }
        }
        if (eventNode != null) {
            writer.append("\n * @eventType ").append(eventNode.getNodeValue());
        }
        writer.append("\n */\n");
        return writer.toString();
    }
    return "";
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) Node(org.w3c.dom.Node)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9