java类javax.xml.xpath.XPathFactory的实例源码

XPathContentWorkerSelector.java 文件源码 项目:alfresco-repository 阅读 19 收藏 0 点赞 0 评论 0
public XPathContentWorkerSelector()
{
    try
    {
        documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        xpathFactory = XPathFactory.newInstance();
    }
    catch (Throwable e)
    {
        throw new AlfrescoRuntimeException("Failed to initialize XPathContentWorkerSelector", e);
    }
    supportedMimetypes = new HashSet<String>();
    supportedMimetypes.add(MimetypeMap.MIMETYPE_XML);
}
MetaDataMerger.java 文件源码 项目:neoscada 阅读 20 收藏 0 点赞 0 评论 0
public MetaDataMerger ( final String label, final boolean compressed ) throws Exception
{
    this.doc = createDocument ();

    final XPathFactory xpf = XPathFactory.newInstance ();
    final XPath xp = xpf.newXPath ();
    this.isCategoryPathExpression = xp.compile ( "properties/property[@name='org.eclipse.equinox.p2.type.category']/@value" );

    final ProcessingInstruction pi = this.doc.createProcessingInstruction ( "metadataRepository", "" );
    pi.setData ( "version=\"1.1.0\"" );
    this.doc.appendChild ( pi );

    this.r = this.doc.createElement ( "repository" );
    this.doc.appendChild ( this.r );
    this.r.setAttribute ( "name", label );
    this.r.setAttribute ( "type", "org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository" );
    this.r.setAttribute ( "version", "1" );

    this.p = this.doc.createElement ( "properties" );
    this.r.appendChild ( this.p );

    addProperty ( this.p, "p2.compressed", "" + compressed );
    addProperty ( this.p, "p2.timestamp", "" + System.currentTimeMillis () );

    this.u = this.doc.createElement ( "units" );
    this.r.appendChild ( this.u );
}
ResXmlPatcher.java 文件源码 项目:AndroidApktool 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Any @string reference in a <provider> value in AndroidManifest.xml will break on
 * build, thus preventing the application from installing. This is from a bug/error
 * in AOSP where public resources cannot be part of an authorities attribute within
 * a <provider> tag.
 *
 * This finds any reference and replaces it with the literal value found in the
 * res/values/strings.xml file.
 *
 * @param file File for AndroidManifest.xml
 * @throws AndrolibException
 */
public static void fixingPublicAttrsInProviderAttributes(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expression = xPath.compile("/manifest/application/provider");

            Object result = expression.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;

            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                NamedNodeMap attrs = node.getAttributes();

                if (attrs != null) {
                    Node provider = attrs.getNamedItem("android:authorities");

                    if (provider != null) {
                        String reference = provider.getNodeValue();
                        String replacement = pullValueFromStrings(file.getParentFile(), reference);

                        if (replacement != null) {
                            provider.setNodeValue(replacement);
                            saveDocument(file, doc);
                        }
                    }
                }
            }

        }  catch (SAXException | ParserConfigurationException | IOException |
                XPathExpressionException | TransformerException ignored) {
        }
    }
}
ResXmlPatcher.java 文件源码 项目:AndroidApktool 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Finds key in strings.xml file and returns text value
 *
 * @param directory Root directory of apk
 * @param key String reference (ie @string/foo)
 * @return String|null
 * @throws AndrolibException
 */
public static String pullValueFromStrings(File directory, String key) throws AndrolibException {
    if (! key.contains("@")) {
        return null;
    }

    File file = new File(directory, "/res/values/strings.xml");
    key = key.replace("@string/", "");

    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expression = xPath.compile("/resources/string[@name=" + '"' + key + "\"]/text()");

            Object result = expression.evaluate(doc, XPathConstants.STRING);

            if (result != null) {
                return (String) result;
            }

        }  catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) {
        }
    }

    return null;
}
ResolveParentVersionMojo.java 文件源码 项目:resolve-parent-version-maven-plugin 阅读 15 收藏 0 点赞 0 评论 0
private Element getParentVersionElement(Document doc) throws MojoExecutionException {
    XPath xPath = XPathFactory.newInstance().newXPath();

    try {
        NodeList nodes = ((NodeList) xPath.evaluate("/project/parent/version", doc.getDocumentElement(), XPathConstants.NODESET));
        if (nodes.getLength() == 0) {
            return null;
        }

        return (Element) nodes.item(0);

    } catch (XPathExpressionException e) {
        throw new MojoExecutionException("Failed to evaluate xpath expression", e);
    }
}
Main.java 文件源码 项目:JATS2LaTeX 阅读 17 收藏 0 点赞 0 评论 0
private static void writerToFile(String inputFile, String outputLatexStandard, String outputBib) throws IOException,
ParserConfigurationException, SAXException, XPathExpressionException, DOMException, NumberFormatException {


    // writing LaTeX in World Standard
    Path latexStandard = Paths.get(outputLatexStandard);
    BufferedWriter wrlatex = Files.newBufferedWriter(latexStandard, StandardCharsets.UTF_8);

    // writing bibtex
    Path bibtexStandard = Paths.get(outputBib);
    BufferedWriter bib = Files.newBufferedWriter(bibtexStandard, StandardCharsets.UTF_8);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(inputFile);
    XPath xPath =  XPathFactory.newInstance().newXPath();

    /* parsing JATS XML */
    LaTeX latex = jatsParser(document, xPath);

    /* creating reference to a bib with regex */
    String referenceLink = outputBib.trim().replaceAll(".bib$", "");
    if (referenceLink.contains("\\") || referenceLink.contains("/")) {
        Pattern p = Pattern.compile("(\\w+)$");
        Matcher m = p.matcher(referenceLink);
        if (m.find()) {
            referenceLink = m.group();
        }
    } 

    /* writing to LaTeX (standard, bib) */
    latexStandardWriter(wrlatex, latex, referenceLink);
    bibWriter(bib, latex);
    }
XMLConverter.java 文件源码 项目:oscm 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Returns the node in the given document at the specified XPath.
 * 
 * @param node
 *            The document to be checked.
 * @param xpathString
 *            The xpath to look at.
 * @return The node at the given xpath.
 * @throws XPathExpressionException
 */
public static Node getNodeByXPath(Node node, String xpathString)
        throws XPathExpressionException {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new XmlNamespaceResolver(
            getOwningDocument(node)));
    final XPathExpression expr = xpath.compile(xpathString);
    return (Node) expr.evaluate(node, XPathConstants.NODE);
}
Pom.java 文件源码 项目:nbreleaseplugin 阅读 22 收藏 0 点赞 0 评论 0
private List<ElementValue> findElementValues(ElementValue pev, String path) {
    List<ElementValue> result = new ArrayList<>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    try {
        NodeList list = (NodeList) xpath.evaluate(path, pev.getElement(), XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            result.add(new ElementValue((Element) list.item(i), Type.OK));
        }
    } catch (XPathExpressionException ex) {
    }
    return result;
}
XMLConverter.java 文件源码 项目:oscm 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Count nodes which are given via xpath expression
 */
public static Double countNodes(Node node, String nodePath)
        throws XPathExpressionException {
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    final XPathExpression expr = xpath.compile("count(" + nodePath + ')');
    return (Double) expr.evaluate(node, XPathConstants.NUMBER);
}
WsdlHandleTaskTest.java 文件源码 项目:oscm 阅读 24 收藏 0 点赞 0 评论 0
@Test
public void execute() throws Exception {
    // when
    task.execute();
    // then
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(TEST_FILE));
    XPathFactory xpfactory = XPathFactory.newInstance();
    XPath path = xpfactory.newXPath();
    Element node = (Element) path.evaluate("/definitions/documentation",
            doc, XPathConstants.NODE);
    assertEquals("v1.7", node.getTextContent());
}


问题


面经


文章

微信
公众号

扫码关注公众号