Search in sources :

Example 41 with XPathFactory

use of javax.xml.xpath.XPathFactory in project uPortal by Jasig.

the class SimpleLayout method getRootId.

@Override
public String getRootId() {
    String rootNode = null;
    try {
        String expression = "/layout/folder";
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        Element rootNodeE = (Element) xpath.evaluate(expression, layout, XPathConstants.NODE);
        rootNode = rootNodeE.getAttribute("ID");
    } catch (Exception e) {
        log.error("Error getting root id.", e);
    }
    return rootNode;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) Element(org.w3c.dom.Element) XPathExpressionException(javax.xml.xpath.XPathExpressionException) PortalException(org.apereo.portal.PortalException)

Example 42 with XPathFactory

use of javax.xml.xpath.XPathFactory in project uPortal by Jasig.

the class PositionManager method isNotReparentable.

/**
     * Return true if the passed in node or any downstream (higher index) siblings <strong>relative
     * to its destination location</strong> have moveAllowed="false".
     */
private static boolean isNotReparentable(NodeInfo ni, Element compViewParent, Element positionSet) {
    // This one is easy -- can't re-parent a node with dlm:moveAllowed=false
    if (ni.getNode().getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false")) {
        return true;
    }
    try {
        /*
             *  Annoying to do in Java, but we need to find our own placeholder
             *  element in the positionSet
             */
        final XPathFactory xpathFactory = XPathFactory.newInstance();
        final XPath xpath = xpathFactory.newXPath();
        final String findPlaceholderXpath = ".//*[local-name()='position' and @name='" + ni.getId() + "']";
        final XPathExpression findPlaceholder = xpath.compile(findPlaceholderXpath);
        final NodeList findPlaceholderList = (NodeList) findPlaceholder.evaluate(positionSet, XPathConstants.NODESET);
        switch(findPlaceholderList.getLength()) {
            case 0:
                LOG.warn("Node not found for XPathExpression=\"" + findPlaceholderXpath + "\" in positionSet=" + XmlUtilitiesImpl.toString(positionSet));
                return true;
            case 1:
                // This is healthy
                break;
            default:
                LOG.warn("More than one node found for XPathExpression=\"" + findPlaceholderXpath + "\" in positionSet=" + XmlUtilitiesImpl.toString(positionSet));
                return true;
        }
        // At last
        final Element placeholder = (Element) findPlaceholderList.item(0);
        for (Element nextPlaceholder = (Element) placeholder.getNextSibling(); // As long as we have a placeholder to look at
        nextPlaceholder != null; nextPlaceholder = (Element) nextPlaceholder.getNextSibling()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Considering whether node ''" + ni.getId() + "' is Reparentable;  subsequent sibling is:  " + nextPlaceholder.getAttribute("name"));
            }
            /*
                 * Next task:  we have to find the non-placeholder representation of
                 * the nextSiblingPlaceholder within the compViewParent
                 */
            final String unmaskPlaceholderXpath = ".//*[@ID='" + nextPlaceholder.getAttribute("name") + "']";
            final XPathExpression unmaskPlaceholder = xpath.compile(unmaskPlaceholderXpath);
            final NodeList unmaskPlaceholderList = (NodeList) unmaskPlaceholder.evaluate(compViewParent, XPathConstants.NODESET);
            switch(unmaskPlaceholderList.getLength()) {
                case 0:
                    // to a node that has been moved to this context (afaik)
                    continue;
                case 1:
                    final Element nextSibling = (Element) unmaskPlaceholderList.item(0);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Considering whether node ''" + ni.getId() + "' is Reparentable;  subsequent sibling '" + nextSibling.getAttribute("ID") + "' has dlm:moveAllowed=" + !nextSibling.getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false"));
                    }
                    // Need to perform some checks...
                    if (nextSibling.getAttribute(Constants.ATT_MOVE_ALLOWED).equals("false")) {
                        /*
                             *  The following check is a bit strange;  it seems to verify
                             *  that the current NodeInfo and the nextSibling come from the
                             *  same fragment.  If they don't, the re-parenting is allowable.
                             *  I believe this check could only be unsatisfied in the case
                             *  of tabs.
                             */
                        Precedence p = Precedence.newInstance(nextSibling.getAttribute(Constants.ATT_FRAGMENT));
                        if (ni.getPrecedence().isEqualTo(p)) {
                            return true;
                        }
                    }
                    break;
                default:
                    LOG.warn("More than one node found for XPathExpression=\"" + unmaskPlaceholderXpath + "\" in compViewParent");
                    return true;
            }
        }
    } catch (XPathExpressionException xpe) {
        throw new RuntimeException("Failed to evaluate XPATH", xpe);
    }
    // Re-parenting is "not disallowed" (double-negative less readable)
    return false;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 43 with XPathFactory

use of javax.xml.xpath.XPathFactory in project stanbol by apache.

the class HtmlExtractionRegistry method initialize.

public void initialize(InputStream configFileStream) throws InitializationException {
    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = parser.parse(new InputSource(configFileStream));
        Node node;
        NodeList nodes = (NodeList) xPath.evaluate("/htmlextractors/extractor", document, XPathConstants.NODESET);
        if (nodes != null) {
            TransformerFactory transFac = TransformerFactory.newInstance();
            transFac.setURIResolver(new BundleURIResolver());
            for (int j = 0, iCnt = nodes.getLength(); j < iCnt; j++) {
                Node nd = nodes.item(j);
                node = (Node) xPath.evaluate("@id", nd, XPathConstants.NODE);
                String id = node.getNodeValue();
                Node srcNode = (Node) xPath.evaluate("source", nd, XPathConstants.NODE);
                if (srcNode != null) {
                    node = (Node) xPath.evaluate("@type", srcNode, XPathConstants.NODE);
                    String srcType = node.getNodeValue();
                    if (srcType.equals("xslt")) {
                        String rdfFormat = "rdfxml";
                        Syntax rdfSyntax = Syntax.RdfXml;
                        node = (Node) xPath.evaluate("@syntax", srcNode, XPathConstants.NODE);
                        if (node != null) {
                            rdfFormat = node.getNodeValue();
                            if (rdfFormat.equalsIgnoreCase("turtle")) {
                                rdfSyntax = Syntax.Turtle;
                            } else if (rdfFormat.equalsIgnoreCase("ntriple")) {
                                rdfSyntax = Syntax.Ntriples;
                            } else if (rdfFormat.equalsIgnoreCase("n3")) {
                                rdfSyntax = XsltExtractor.N3;
                            } else if (!rdfFormat.equalsIgnoreCase("rdfxml")) {
                                throw new InitializationException("Unknown RDF Syntax: " + rdfFormat + " for " + id + " extractor");
                            }
                        }
                        // TODO: do something about disjunctions of
                        // Extractors? Assume, only RDFa or Microformats are
                        // used?
                        String fileName = DOMUtils.getText(srcNode);
                        XsltExtractor xsltExtractor = new XsltExtractor(id, fileName, transFac);
                        xsltExtractor.setSyntax(rdfSyntax);
                        // name of URI/URL parameter of the script (default
                        // "uri")
                        node = (Node) xPath.evaluate("@uri", srcNode, XPathConstants.NODE);
                        if (node != null) {
                            xsltExtractor.setUriParameter(node.getNodeValue());
                        }
                        registry.put(id, xsltExtractor);
                        activeExtractors.add(id);
                    } else if (srcType.equals("java")) {
                        String clsName = srcNode.getNodeValue();
                        Object extractor = Class.forName(clsName).newInstance();
                        if (extractor instanceof HtmlExtractionComponent) {
                            registry.put(id, (HtmlExtractionComponent) extractor);
                            activeExtractors.add(id);
                        } else {
                            throw new InitializationException("clsName is not an HtmlExtractionComponent");
                        }
                    } else {
                        LOG.warn("No valid type for extractor found: " + id);
                    }
                    LOG.info("Extractor for: " + id);
                }
            }
        }
    } catch (FileNotFoundException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (XPathExpressionException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (DOMException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (IllegalAccessException e) {
        throw new InitializationException(e.getMessage(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DOMException(org.w3c.dom.DOMException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XPath(javax.xml.xpath.XPath) TransformerFactory(javax.xml.transform.TransformerFactory) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Syntax(org.ontoware.rdf2go.model.Syntax)

Example 44 with XPathFactory

use of javax.xml.xpath.XPathFactory in project tomee by apache.

the class MavenResolver method m2Home.

private static String m2Home() {
    final Properties properties;
    if (SystemInstance.isInitialized()) {
        properties = SystemInstance.get().getProperties();
    } else {
        properties = System.getProperties();
    }
    String home = "";
    File f = new File(properties.getProperty("openejb.m2.home", System.getProperty("user.home") + "/.m2/repository/"));
    if (f.exists()) {
        home = f.getAbsolutePath();
    } else {
        f = new File(properties.getProperty("openejb.m2.settings", System.getProperty("user.home") + "/.m2/settings.xml"));
        if (f.exists()) {
            try {
                final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(f);
                final XPathFactory xpf = XPathFactory.newInstance();
                final XPath xp = xpf.newXPath();
                home = xp.evaluate("//settings/localRepository/text()", document.getDocumentElement());
            } catch (final Exception ignore) {
            //no-op
            }
        }
    }
    return (home.endsWith("/") ? home : home + "/");
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Properties(java.util.Properties) Document(org.w3c.dom.Document) File(java.io.File) MalformedURLException(java.net.MalformedURLException) SAXException(org.xml.sax.SAXException)

Example 45 with XPathFactory

use of javax.xml.xpath.XPathFactory in project ceylon-compiler by ceylon.

the class AntBasedTests method rewriteBuildFile.

@Before
public void rewriteBuildFile() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document document = parser.parse(this.originalBuildfile);
    if (OSUtil.isWindows()) {
        /*
             * On windows you can't just use the <exec> task on a .bat file
             * you have to exec "cmd -c foo.bat". To avoid a maintainance 
             * nightmare in the test build files, we just rewrite the build 
             * files on the fly.
             */
        XPathFactory xpfactory = XPathFactory.newInstance();
        NodeList execTasks = (NodeList) xpfactory.newXPath().evaluate("//exec", document, XPathConstants.NODESET);
        for (int ii = 0; ii < execTasks.getLength(); ii++) {
            Element exec = (Element) execTasks.item(ii);
            String executable = exec.getAttribute("executable");
            exec.setAttribute("executable", "cmd");
            Element argExecutable = document.createElement("arg");
            argExecutable.setAttribute("value", executable);
            exec.insertBefore(argExecutable, exec.getFirstChild());
            Element argOptiopnC = document.createElement("arg");
            argOptiopnC.setAttribute("value", "/c");
            exec.insertBefore(argOptiopnC, exec.getFirstChild());
        }
    }
    actualBuildFile = File.createTempFile("ceylon-ant-test-", "-build.xml");
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(actualBuildFile));
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Before(org.junit.Before)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 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 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7