/** * Specifies that the transformation is done via XQuery * */ public TransmogrifierAdapterDefinition xquery() { transmogrifier = new XqjTransmogrifier<>(String.class); return (TransmogrifierAdapterDefinition) input(bodyAs(StreamSource.class)); }
private void doZap(Source source, Result result, Object... params) { XQResultSequence seq = null; try { XQPreparedExpression expression = resource(params); expression.bindDocument(XQConstants.CONTEXT_ITEM, source, null); bindExpressionContext(expression, staticParams); bindExpressionContext(expression, resourceParameters(params)); seq = expression.executeQuery(); seq.writeSequenceToResult(result); } catch (Exception e) { throw new RuntimeException("XQuery processing failed", e); } finally { if (seq != null && !seq.isClosed()) { try { seq.close(); } catch (XQException e) { LOG.trace("XQLTransmogrifier didn't return a value.", e); } } } }
/** * Converts a Source into a typed result using a XQuery processor. * <p> * The XQ resource location is mandatory in the first extra parameter. Other * parameters may be passed as a Map in the second parameter. * * @see Transmogrifier#zap(java.lang.Object, java.lang.Object[]) */ @Override public T zap(Source source, Object... params) { ResultHolder<T> accessor = ResultHolderFactory.create(outputFormat); if (accessor == null) throw new IllegalArgumentException("Format " + outputFormat.getClass() + " is not supported"); Result result = accessor.createResult(); doZap(source, result, params); return accessor.getResult(); }
@Override public XQPreparedExpression createResource(Object... params) { String resourceLocation = resourceLocation(params); LOG.debug("Create new template for {}", resourceLocation); try { InputStream stream = resourceContent(params).getInputStream(); return getConnection().prepareExpression(stream); } catch (Exception e) { throw new IllegalArgumentException("The resource " + resourceLocation + " is not valid", e); } }
@Test public void zapParametrisedConstructor() throws IOException, SAXException, XQException { Map<String, Object> configParams = new HashMap<>(); configParams.put(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE); XqjTransmogrifier<String> localTransformer = new XqjTransmogrifier<>(String.class, configParams); String zapResult = localTransformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq" ); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }
@Test(expected = RuntimeException.class) public void zapMissingParameter() throws IOException, SAXException { transformer.zap(source("/xquery/string.xml"), "/xquery/string-q2.xq"); }
/** * This method had to be overridden because {@link XQPreparedExpression} objects * are not thread-safe, thus an additional replication step is necessary. */ @Override protected XQPreparedExpression resource(Object... params) throws Exception { XQPreparedExpression expression = super.resource(params); return getConnection().copyPreparedExpression(expression); }
@Test public void zapParametrisedConstructorNoParams() throws IOException, SAXException, XQException { XqjTransmogrifier<String> localTransformer = new XqjTransmogrifier<>(String.class); String zapResult = localTransformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }
@Test public void zapRCase() throws IOException, SAXException { String zapResult = transformer.zap(new StreamSource(new StringReader("<empty/>")), "/xquery/r-q1.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/r-q1.xml"), zapResult).similar()); }
/** * Specifies that the transformation is done via XQuery * * @param clazz * the resulting type of the message body after the * transformation */ public <T> TransmogrifierAdapterDefinition xquery(Class<T> clazz) { transmogrifier = new XqjTransmogrifier<>(clazz); return (TransmogrifierAdapterDefinition) input(bodyAs(StreamSource.class)); }
@Test public void zapLocalFunction() throws IOException, SAXException { Map<String, Object> dynamicEvalParams = new HashMap<>(); dynamicEvalParams.put("language", "Bulgarian"); dynamicEvalParams.put("author_name", "John"); Object[] params = new Object[] { "/xquery/string-q3.xq", dynamicEvalParams }; String zapResult = transformer.zap(source("/xquery/string.xml"), params); assertTrue(XMLUnit.compareXML(result("/xquery/string-q3.xml"), zapResult).similar()); }
@Before public void setUp() throws Exception { transformer = new XqjTransmogrifier<>(String.class); }
@Test public void zapStringParameter() throws IOException, SAXException { Map<String, Object> dynamicParams = new HashMap<>(); dynamicParams.put("language", "English"); Object[] params = new Object[] { "/xquery/string-q2.xq", dynamicParams }; String zapResult1 = transformer.zap(source("/xquery/string.xml"), params); assertTrue(XMLUnit.compareXML(result("/xquery/string-q2.xml"), zapResult1).similar()); dynamicParams.put("language", "German"); String zapResult2 = transformer.zap(source("/xquery/string.xml"), params); assertTrue(XMLUnit.compareXML(result("/xquery/string-q2g.xml"), zapResult2).similar()); }
@Test public void zapClasspathResolver() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }
@Test public void zapWithNamespaces() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/ns.xml"), "/xquery/ns-q1.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/ns-q1.xml"), zapResult).similar()); }
@Test public void zapSimple() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q1.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string-q1.xml"), zapResult).similar()); }
@Test public void zapMainFunction() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q4.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }