org.apache.xpath.XPathAPI Java Examples

The following examples show how to use org.apache.xpath.XPathAPI. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XPathApacheXPathApi.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Document doc = XmlUtils.loadDoc("/testcode/xpath/data.xml");

    String input = args.length != 0 ? args[1] : "guess' or '1'='1";
    String query = "//groups/group[@id='" + input + "']/writeAccess/text()";

    //selectNodeIterator
    NodeIterator iterator = XPathAPI.selectNodeIterator(doc, query);
    XmlUtils.printNodeIterator(iterator);

    //selectNodeList
    NodeList nodeList = XPathAPI.selectNodeList(doc, query);
    XmlUtils.printNodeList(nodeList);

    //selectSingleNode
    Node node = XPathAPI.selectSingleNode(doc, query);
    XmlUtils.printNode(node);

    //Static string (safe)
    Node node2 = XPathAPI.selectSingleNode(doc, "//groups/group[@id='guess']/writeAccess/text()".toLowerCase());
    XmlUtils.printNode(node2);
}
 
Example #2
Source File: SignedInfo.java    From ebics-java-client with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Canonizes and signs a given input with the authentication private key.
 * of the EBICS user.
 * 
 * <p>The given input to be signed is first Canonized using the 
 * http://www.w3.org/TR/2001/REC-xml-c14n-20010315 algorithm.
 * 
 * <p>The element to be canonized is only the SignedInfo element that should be
 * contained in the request to be signed. Otherwise, a {@link TransformationException}
 * is thrown.
 * 
 * <p> The namespace of the SignedInfo element should be named <b>ds</b> as specified in
 * the EBICS specification for common namespaces nomination.
 * 
 * <p> The signature is ensured using the user X002 private key. This step is done in
 * {@link EbicsUser#authenticate(byte[]) authenticate}.
 * 
 * @param toSign the input to sign
 * @return the signed input
 * @throws EbicsException signature fails.
 */
public byte[] sign(byte[] toSign) throws EbicsException {
  try {
    DocumentBuilderFactory 		factory;
    DocumentBuilder			builder;
    Document				document;
    Node 				node;
    Canonicalizer 			canonicalizer;

    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new IgnoreAllErrorHandler());
    document = builder.parse(new ByteArrayInputStream(toSign));
    node = XPathAPI.selectSingleNode(document, "//ds:SignedInfo");
    canonicalizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    return user.authenticate(canonicalizer.canonicalizeSubtree(node));
  } catch(Exception e) {
    throw new EbicsException(e.getMessage());
  }
}
 
Example #3
Source File: SignedInfo.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Canonizes and signs a given input with the authentication private key. of the EBICS user.
 *
 * <p>The given input to be signed is first Canonized using the
 * http://www.w3.org/TR/2001/REC-xml-c14n-20010315 algorithm.
 *
 * <p>The element to be canonized is only the SignedInfo element that should be contained in the
 * request to be signed. Otherwise, a {@link TransformationException} is thrown.
 *
 * <p>The namespace of the SignedInfo element should be named <b>ds</b> as specified in the EBICS
 * specification for common namespaces nomination.
 *
 * <p>The signature is ensured using the user X002 private key. This step is done in {@link
 * EbicsUser#authenticate(byte[]) authenticate}.
 *
 * @param toSign the input to sign
 * @return the signed input
 * @throws EbicsException signature fails.
 */
public byte[] sign(byte[] toSign) throws AxelorException {
  try {
    DocumentBuilderFactory factory;
    DocumentBuilder builder;
    Document document;
    Node node;
    Canonicalizer canonicalizer;

    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new IgnoreAllErrorHandler());
    document = builder.parse(new ByteArrayInputStream(toSign));
    node = XPathAPI.selectSingleNode(document, "//ds:SignedInfo");
    canonicalizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    return Beans.get(EbicsUserService.class)
        .authenticate(user, canonicalizer.canonicalizeSubtree(node));
  } catch (Exception e) {
    e.printStackTrace();
    throw new AxelorException(e, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR);
  }
}
 
Example #4
Source File: Utils.java    From ebics-java-client with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Canonizes an input with inclusive c14n without comments algorithm.
 * 
 * <p>EBICS Specification 2.4.2 - 5.5.1.1.1 EBICS messages in transaction initialization:
 * 
 * <p>The identification and authentication signature includes all XML elements of the
 * EBICS request whose attribute value for @authenticate is equal to “true”. The
 * definition of the XML schema “ebics_request.xsd“ guarantees that the value of the
 * attribute @authenticate is equal to “true” for precisely those elements that also
 * need to be signed.
 * 
 * <p>Thus, All the Elements with the attribute authenticate = true and their 
 * sub elements are considered for the canonization process. This is performed 
 * via the {@link XPathAPI#selectNodeIterator(Node, String) selectNodeIterator(Node, String)}.
 * 
 * @param input the byte array XML input.
 * @return the canonized form of the given XML
 * @throws EbicsException
 */
public static byte[] canonize(byte[] input) throws EbicsException {
  DocumentBuilderFactory 		factory;
  DocumentBuilder			builder;
  Document				document;
  NodeIterator			iter;
  ByteArrayOutputStream		output;
  Node 				node;

  try {
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new IgnoreAllErrorHandler());
    document = builder.parse(new ByteArrayInputStream(input));
    iter = XPathAPI.selectNodeIterator(document, "//*[@authenticate='true']");
    output = new ByteArrayOutputStream();
    while ((node = iter.nextNode()) != null) {
      Canonicalizer 		canonicalizer;

      canonicalizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
      output.write(canonicalizer.canonicalizeSubtree(node));
    }

    return output.toByteArray();
  } catch (Exception e) {
    throw new EbicsException(e.getMessage());
  }
}
 
Example #5
Source File: XmlUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
public static Node selectNode(Element documentElement, String xpath)
{
	Node res = null;
	try {
		Node result = XPathAPI.selectSingleNode(documentElement, xpath);
		if (result!=null)
			res = result;
	} catch (TransformerException e) {
		e.printStackTrace();
	}
	return res;
}
 
Example #6
Source File: CreateBlockTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws SAXException, IOException, TransformerException, InvalidFormatException {
	
	/* Here we want to test white space handling in 
	 * 	protected static DocumentFragment createBlock(WordprocessingMLPackage wmlPackage, RunFontSelector runFontSelector, NodeIterator childResults,
		boolean sdt, PPr pPrDirect, PPr pPr, RPr rPr, RPr rPrParagraphMark) 
		
		
	 */
	
	// We need NodeIterator, simulating the results from XSLT
	File f = new File("src/test/java/org/docx4j/convert/out/fo/nodes.fo");
	DocumentBuilder db = XmlUtils.getNewDocumentBuilder();
	Document doc = db.parse(f);
	NodeIterator childResults = XPathAPI.selectNodeIterator(doc, "/*");

	//System.out.println(childResults.nextNode().getNodeName());
	
	// Other params don't matter
	FOConversionContext context = null; 
	String pStyleVal = "styleX";
	boolean sdt = false;
	PPr pPrDirect = new PPr();
	PPr pPr = new PPr(); 
	RPr rPr = new RPr();
	RPr rPrParagraphMark  = new RPr();
	
	WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage();
	RunFontSelector runFontSelector = FOConversionContext.createRunFontSelector(wmlPackage);
	
	DocumentFragment df = XsltFOFunctions.createBlock( wmlPackage, runFontSelector,  pStyleVal,  childResults,
			 sdt,  pPrDirect,  pPr,  rPr,  rPrParagraphMark);
	
	System.out.println(XmlUtils.w3CDomNodeToString(df));
	
}
 
Example #7
Source File: EbicsUtils.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Canonizes an input with inclusive c14n without comments algorithm.
 *
 * <p>EBICS Specification 2.4.2 - 5.5.1.1.1 EBICS messages in transaction initialization:
 *
 * <p>The identification and authentication signature includes all XML elements of the EBICS
 * request whose attribute value for @authenticate is equal to “true”. The definition of the XML
 * schema “ebics_request.xsd“ guarantees that the value of the attribute @authenticate is equal to
 * “true” for precisely those elements that also need to be signed.
 *
 * <p>Thus, All the Elements with the attribute authenticate = true and their sub elements are
 * considered for the canonization process. This is performed via the {@link
 * XPathAPI#selectNodeIterator(Node, String) selectNodeIterator(Node, String)}.
 *
 * @param input the byte array XML input.
 * @return the canonized form of the given XML
 * @throws EbicsException
 */
public static byte[] canonize(byte[] input) throws AxelorException {
  DocumentBuilderFactory factory;
  DocumentBuilder builder;
  Document document;
  NodeIterator iter;
  ByteArrayOutputStream output;
  Node node;

  try {
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new IgnoreAllErrorHandler());
    document = builder.parse(new ByteArrayInputStream(input));
    iter = XPathAPI.selectNodeIterator(document, "//*[@authenticate='true']");
    output = new ByteArrayOutputStream();
    while ((node = iter.nextNode()) != null) {
      Canonicalizer canonicalizer;

      canonicalizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
      output.write(canonicalizer.canonicalizeSubtree(node));
    }

    return output.toByteArray();
  } catch (Exception e) {
    throw new AxelorException(
        e.getCause(), TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, e.getMessage());
  }
}
 
Example #8
Source File: AbstractHtmlParseFilter.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
protected static NodeList selectNodeList(Node node, String xpath) {
    try {
        return XPathAPI.selectNodeList(node, xpath);
    } catch (TransformerException e) {
        LOG.warn("Bad 'xpath' expression [{}]", xpath);
    }
    return null;
}
 
Example #9
Source File: AbstractHtmlParseFilter.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
protected Node selectSingleNode(Node contextNode, String xpath) {
    try {
        return XPathAPI.selectSingleNode(contextNode, xpath);
    } catch (TransformerException e) {
        LOG.warn("Bad 'xpath' expression [{}]", xpath);
    }
    return null;
}