/** * Returns properly configured (e.g. security features) factory * - securityProcessing == is set based on security processing property, default is true */ public static XPathFactory createXPathFactory(boolean disableSecureProcessing) throws IllegalStateException { try { XPathFactory factory = XPathFactory.newInstance(); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "XPathFactory instance: {0}", factory); } factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing)); return factory; } catch (XPathFactoryConfigurationException ex) { LOGGER.log(Level.SEVERE, null, ex); throw new IllegalStateException( ex); } catch (AbstractMethodError er) { LOGGER.log(Level.SEVERE, null, er); throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er); } }
/** * Security is enabled, extension function not allowed */ public void testExtFuncNotAllowed() { Policy p = new SimplePolicy(new AllPermission()); Policy.setPolicy(p); System.setSecurityManager(new SecurityManager()); try { evaluate(false); } catch (XPathFactoryConfigurationException e) { fail(e.getMessage()); } catch (XPathExpressionException ex) { //expected since extension function is disallowed System.out.println("testExtFuncNotAllowed: OK"); } finally { System.setSecurityManager(null); } }
public TrpXPathProcessor(final String docBuilderFactoryImpl, final String xPathFactoryImpl) throws XPathFactoryConfigurationException, ParserConfigurationException { if(docBuilderFactoryImpl == null || xPathFactoryImpl == null) { throw new IllegalArgumentException("Arguments must not be null!"); } classLoader = this.getClass().getClassLoader(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( docBuilderFactoryImpl, classLoader); builder = factory.newDocumentBuilder(); xPathFactory = XPathFactory.newInstance( javax.xml.xpath.XPathFactory.DEFAULT_OBJECT_MODEL_URI, xPathFactoryImpl, classLoader ); xPath = xPathFactory.newXPath(); }
protected static XPathFactory createDefaultXPathFactory() throws XPathFactoryConfigurationException { XPathFactory factory = null; // read system property and see if there is a factory set Properties properties = System.getProperties(); for (Map.Entry<Object, Object> prop : properties.entrySet()) { String key = (String) prop.getKey(); if (key.startsWith(XPathFactory.DEFAULT_PROPERTY_NAME)) { String uri = ObjectHelper.after(key, ":"); if (uri != null) { factory = XPathFactory.newInstance(uri); LOG.info("Using system property {} with value {} when created default XPathFactory {}", new Object[]{key, uri, factory}); } } } if (factory == null) { factory = XPathFactory.newInstance(); LOG.info("Created default XPathFactory {}", factory); } return factory; }
@Autowired public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException, XPathFactoryConfigurationException, XPathExpressionException { this.humanResourceService = humanResourceService; Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI); XPathFactory xPathFactory = XPathFactory.instance(); this.startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace); this.endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace); this.nameExpression = xPathFactory.compile( "concat(//hr:FirstName,' ',//hr:LastName)", Filters.fstring(), null, namespace); }
private WMTSLayerInfo parseCapabilities() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, XPathFactoryConfigurationException { String uri = capabilitiesUrl; WMTSCapabilitiesDocument capabilities = new WMTSCapabilitiesDocument(uri); capabilities.scanTileMatrixSet(tileMatrixSet, new TileMatrixSetScanner() { @Override public void onTileMatrix(String identifier, double scaleDenom, double ulx, double uly, int tileWidth, int tileHeight, int matrixWidth, int matrixHeight) { tileMatrices.add( new WMTSTileMatrix(identifier, scaleDenom, ulx, uly, tileWidth, tileHeight, matrixWidth, matrixHeight)); } }); return capabilities.getLayerInfo(layerName); }
private boolean isContained(TestAssertion assertion, String fileLocation) throws XPathExpressionException, XPathFactoryConfigurationException, XPathException { XPathFactory xpathFactory = XPathFactory .newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); XPathEvaluator xpath = (XPathEvaluator) xpathFactory.newXPath(); xpath.getConfiguration().setLineNumbering(true); xpath.setNamespaceContext(new BpelNamespaceContext()); XPathExpression expr = xpath.compile(assertion.getTarget()); InputSource inputSource = new InputSource( new File(fileLocation).toString()); SAXSource saxSource = new SAXSource(inputSource); NodeInfo doc = xpath.getConfiguration().buildDocument(saxSource); @SuppressWarnings("unchecked") List<NodeInfo> matchedLines = (List<NodeInfo>) expr.evaluate(doc, XPathConstants.NODESET); if (matchedLines.size() > 0) { return true; } return false; }
public static XPathFactory newXPathFactory(boolean secureXmlProcessingEnabled) { XPathFactory factory = XPathFactory.newInstance(); try { factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); } catch (XPathFactoryConfigurationException e) { LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); } return factory; }
private static XPathFactory getXPathFactory() throws XPathFactoryConfigurationException { if (_xPathFactory==null) { String magicValue=System.getProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom"); System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom","net.sf.saxon.xpath.XPathFactoryImpl"); // System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom","org.apache.xpath.jaxp.XPathFactoryImpl"); // System.setProperty("jaxp.debug","yes"); _xPathFactory=XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); if (magicValue==null) System.clearProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom"); else System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom",magicValue); } return _xPathFactory; }
void evaluate(boolean enableExt) throws XPathFactoryConfigurationException, XPathExpressionException { Document document = getDocument(); XPathFactory xPathFactory = XPathFactory.newInstance(); /** * Use of the extension function 'http://exslt.org/strings:tokenize' is * not allowed when the secure processing feature is set to true. * Attempt to use the new property to enable extension function */ if (enableExt) { boolean isExtensionSupported = enableExtensionFunction(xPathFactory); } xPathFactory.setXPathFunctionResolver(new MyXPathFunctionResolver()); if (System.getSecurityManager() == null) { xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false); } XPath xPath = xPathFactory.newXPath(); xPath.setNamespaceContext(new MyNamespaceContext()); String xPathResult = xPath.evaluate(XPATH_EXPRESSION, document); System.out.println( "XPath result (enableExtensionFunction == " + enableExt + ") = \"" + xPathResult + "\""); }
boolean enableExtensionFunction(XPathFactory factory) { boolean isSupported = true; try { factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true); } catch (XPathFactoryConfigurationException ex) { isSupported = false; } return isSupported; }
public static XPathFactory newXPathFactory(boolean disableSecurity) { XPathFactory factory = XPathFactory.newInstance(); try { factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !xmlSecurityDisabled(disableSecurity)); } catch (XPathFactoryConfigurationException e) { LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); } return factory; }
private XPath createXPath() throws XPathFactoryConfigurationException { XPathFactory xpathFactory = XPathFactory.newInstance(); Assert.assertNotNull(xpathFactory); XPath xpath = xpathFactory.newXPath(); Assert.assertNotNull(xpath); return xpath; }
public static PageXmlProcessor newInstance(DocBuilderFactoryImpl docBuilderFactoryImpl, XPathFactoryImpl xPathFactoryImpl) throws XPathFactoryConfigurationException, ParserConfigurationException { File store = new File(STORE_LOCATION); if(store.isDirectory() && store.canRead()) { logger.debug("Returning Instance with netShare access."); return buildNetShareInstance(docBuilderFactoryImpl, xPathFactoryImpl); } else { logger.debug("Returning Instance with HTTPS access."); return buildHttpInstance(docBuilderFactoryImpl, xPathFactoryImpl); } }
private static PageXmlProcessor buildHttpInstance(DocBuilderFactoryImpl dbImpl, XPathFactoryImpl xpImpl) throws XPathFactoryConfigurationException, ParserConfigurationException { return new PageXmlProcessor(dbImpl, xpImpl) { @Override protected Document loadDocument(final String xmlKey) throws MalformedURLException, IllegalArgumentException, SAXException, IOException { return super.parse(uriBuilder.getFileUri(xmlKey).toURL()); } }; }
private static PageXmlProcessor buildNetShareInstance(DocBuilderFactoryImpl dbImpl, XPathFactoryImpl xpImpl) throws XPathFactoryConfigurationException, ParserConfigurationException { return new PageXmlProcessor(dbImpl, xpImpl) { @Override protected Document loadDocument(final String xmlKey) throws MalformedURLException, IllegalArgumentException, SAXException, IOException { File file = LocalFimagestoreClient.findFile(STORE_LOCATION, xmlKey); return super.parse(file); } }; }
public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, XPathFactoryConfigurationException{ ClassLoader cl = XPathTest.class.getClassLoader(); // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", cl); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // domFactory.setNamespaceAware(true); // XPathFactory xpathFactory = XPathFactory.newInstance(javax.xml.xpath.XPathFactory.DEFAULT_OBJECT_MODEL_URI, "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", cl); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); xpath.getNamespaceContext(); DocumentBuilder builder = domFactory.newDocumentBuilder(); File f = new File("/mnt/dea_scratch/TRP/test/Klassikerausgaben_test/page/bsb00087391_00009.xml"); Document catalog = builder.parse(f); final XPathExpression expr = xpath.compile("//*[contains(name(), 'Page')]/@imageWidth"); // Object result = expr.evaluate(catalog, XPathConstants.NUMBER); // Double res = (Double)result; // System.out.println(res); Object result = expr.evaluate(catalog, XPathConstants.STRING); String res = (String)result; System.out.println(res); // Object result = expr.evaluate(catalog, XPathConstants.NODESET); // NodeList nodes = (NodeList) result; // if (nodes.getLength() > 0) { // String[] parents = new String[nodes.getLength()]; // for (int i = 0; i < nodes.getLength(); i++) { // parents[i] = nodes.item(i).getNodeValue(); // System.out.println(parents[i]); // } // } }