/** * Returns a Source for reading the XML value designated by this SQLXML * instance. <p> * * @param sourceClass The class of the source, or null. If null, then a * DOMSource is returned. * @return a Source for reading the XML value. * @throws SQLException if there is an error processing the XML value * or if the given <tt>sourceClass</tt> is not supported. */ protected <T extends Source>T getSourceImpl( Class<T> sourceClass) throws SQLException { if (JAXBSource.class.isAssignableFrom(sourceClass)) { // Must go first presently, since JAXBSource extends SAXSource // (purely as an implementation detail) and it's not possible // to instantiate a valid JAXBSource with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private marshaller and context object attributes) // FALL THROUGH... will throw an exception } else if (StreamSource.class.isAssignableFrom(sourceClass)) { return createStreamSource(sourceClass); } else if ((sourceClass == null) || DOMSource.class.isAssignableFrom(sourceClass)) { return createDOMSource(sourceClass); } else if (SAXSource.class.isAssignableFrom(sourceClass)) { return createSAXSource(sourceClass); } else if (StAXSource.class.isAssignableFrom(sourceClass)) { return createStAXSource(sourceClass); } throw JDBCUtil.invalidArgument("sourceClass: " + sourceClass); }
/** * Returns a Source for reading the XML value designated by this SQLXML * instance. <p> * * @param sourceClass The class of the source, or null. If null, then a * DOMSource is returned. * @return a Source for reading the XML value. * @throws SQLException if there is an error processing the XML value * or if the given <tt>sourceClass</tt> is not supported. */ protected <T extends Source>T getSourceImpl( Class<T> sourceClass) throws SQLException { if (JAXBSource.class.isAssignableFrom(sourceClass)) { // Must go first presently, since JAXBSource extends SAXSource // (purely as an implmentation detail) and it's not possible // to instantiate a valid JAXBSource with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private marshaller and context object attrbutes) // FALL THROUGH... will throw an exception } else if (StreamSource.class.isAssignableFrom(sourceClass)) { return createStreamSource(sourceClass); } else if ((sourceClass == null) || DOMSource.class.isAssignableFrom(sourceClass)) { return createDOMSource(sourceClass); } else if (SAXSource.class.isAssignableFrom(sourceClass)) { return createSAXSource(sourceClass); } else if (StAXSource.class.isAssignableFrom(sourceClass)) { return createStAXSource(sourceClass); } throw Util.invalidArgument("sourceClass: " + sourceClass); }
/** * Returns a Source for reading the XML value designated by this SQLXML * instance. <p> * * @param sourceClass The class of the source, or null. If null, then a * DOMSource is returned. * @return a Source for reading the XML value. * @throws SQLException if there is an error processing the XML value * or if the given <tt>sourceClass</tt> is not supported. */ protected <T extends Source>T getSourceImpl( Class<T> sourceClass) throws SQLException { if (JAXBSource.class.isAssignableFrom(sourceClass)) { // Must go first presently, since JAXBSource extends SAXSource // (purely as an implmentation detail) and it's not possible // to instantiate a valid JAXBSource with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private marshaller and context object attrbutes) // FALL THROUGH... will throw an exception } else if (StreamSource.class.isAssignableFrom(sourceClass)) { return createStreamSource(sourceClass); } else if ((sourceClass == null) || DOMSource.class.isAssignableFrom(sourceClass)) { return createDOMSource(sourceClass); } else if (SAXSource.class.isAssignableFrom(sourceClass)) { return createSAXSource(sourceClass); } else if (StAXSource.class.isAssignableFrom(sourceClass)) { return createStAXSource(sourceClass); } throw JDBCUtil.invalidArgument("sourceClass: " + sourceClass); }
@Override public Source resolve(String href, String base) throws TransformerException { String[] hrefParts = href.split(":"); String userID = hrefParts[1]; MCRUser user = null; try { if ("current".equals(userID)) { user = MCRUserManager.getCurrentUser(); } else if ("getOwnedUsers".equals(userID)) { return getOwnedUsers(hrefParts[2]); } else { user = MCRUserManager.getUser(userID); } if (user == null) { return null; } return new JAXBSource(MCRUserTransformer.JAXB_CONTEXT, user.getSafeCopy()); } catch (JAXBException e) { throw new TransformerException(e); } }
private static <T> T deepCopy(T object, Class<T> clazz, String packages) { try { JAXBContext jaxbContext = JAXBContext.newInstance(packages); // create marshaller which disable validation step Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setEventHandler(event -> true); // create unmarshaller which disable validation step Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setEventHandler(event -> true); JAXBElement<T> contentObject = new JAXBElement<>(new QName(clazz.getName()), clazz, object); return unmarshaller.unmarshal(new JAXBSource(marshaller, contentObject), clazz).getValue(); } catch (JAXBException e) { throw new ParseException("Time overlaps in <p> cannot be resolved.", e); } }
/** * Does TTML document transformation to another TTML document. * * @param tt source TTML document root element * @param transformer transformer * @return TTML document after transformation */ public static TtEltype transformTtmlDocument(TtEltype tt, Transformer transformer) { JAXBElement<TtEltype> ttJaxb = new ObjectFactory().createTt(tt); try { JAXBContext jaxbc = createTtmlJaxbContext(); JAXBSource source = new JAXBSource(jaxbc, ttJaxb); JAXBResult result = new JAXBResult(jaxbc); // transform transformer.transform(source, result); return (TtEltype) ((JAXBElement<TtEltype>) result.getResult()).getValue(); } catch (JAXBException | TransformerException e) { throw new ConvertException(e); } }
@Deprecated public void write(DigitalObjectHandler handler, ModsDefinition mods, String model, long timestamp, String message) throws DigitalObjectException { try { JAXBSource jaxbSource = new JAXBSource(ModsUtils.defaultMarshaller(false), new cz.cas.lib.proarc.mods.ObjectFactory().createMods(mods)); // DO NOT include schemaLocation. Fedora validator does not accept it. Transformer t = DcUtils.modsTransformer(model); EditorResult result = editor.createResult(); JAXBResult jaxbResult = new JAXBResult(DcUtils.defaultUnmarshaller()); t.transform(jaxbSource, jaxbResult); JAXBElement<OaiDcType> elm = (JAXBElement<OaiDcType>) jaxbResult.getResult(); OaiDcType dc = elm.getValue(); addDigitalObjectMetadata(handler, dc); DcUtils.marshal(result, dc, false); editor.write(result, timestamp, message); } catch (TransformerException | JAXBException ex) { throw new DigitalObjectException(object.getPid(), ex); } }
@Test public void testTransformation() throws Exception { ModsDefinition mods = ModsUtils.unmarshalModsType(new StreamSource(DcStreamEditorTest.class.getResource("periodical.xml").toExternalForm())); Transformer t = DcUtils.modsTransformer("model:periodical"); JAXBSource jaxbSource = new JAXBSource(ModsUtils.defaultMarshaller(false), new ObjectFactory().createMods(mods)); StringWriter dump = new StringWriter(); t.transform(jaxbSource, new StreamResult(dump)); String toXml = dump.toString(); System.out.println(toXml); HashMap<String, String> namespaces = new HashMap<String, String>(); namespaces.put("oai_dc", DcStreamEditor.DATASTREAM_FORMAT_URI); namespaces.put("dc", "http://purl.org/dc/elements/1.1/"); XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(namespaces)); XMLAssert.assertXpathExists("/oai_dc:dc/dc:title[text()='main']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:title[text()='key']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:title[text()='alternative']", toXml); XMLAssert.assertXpathEvaluatesTo("3", "count(/oai_dc:dc/dc:title)", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:creator[text()='Boleslav']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:description[text()='poznámka']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:identifier[text()='uuid:40d13cb2-811f-468c-a6d3-1ad6b01f06f7']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:identifier[text()='isbn:0eaa6730']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:identifier[text()='issn-l:idIssn-l']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:identifier[text()='idWitEmptyType']", toXml); XMLAssert.assertXpathExists("/oai_dc:dc/dc:identifier[text()='idWithoutType']", toXml); // XXX needs more test }
public Source invoke(Source request) { try { JAXBContext jc = JAXBContext.newInstance(new Class[] { UserType.class }); UserType user = (UserType)jc.createUnmarshaller().unmarshal(request); log.info("[string=" + user.getString() + ",qname=" + user.getQname() + "]"); return new JAXBSource(jc, user); } catch (RuntimeException rte) { throw rte; } catch (Exception e) { throw new WebServiceException(e); } }
/** * Render a jaxbElement with given xslt resource and * @param writer write result to this writer * @param jaxbModel date to render * @param stylesheet xslt stylesheet resource * @param parameters parameter for style sheet, like localizer, uriInfo (depends on stylesheet) * @throws IOException */ public void render(Writer writer, Object jaxbModel, String stylesheet, Map<String, Object> parameters) throws IOException { try { // Create Transformer Transformer transformer = transformerFactory.newTransformer(getStreamSource(stylesheet)); for(Entry<String, Object> entry : parameters.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } // Source JAXBContext jc = JAXBContext.newInstance(jaxbModel.getClass()); // Transform transformer.transform(new JAXBSource(jc, jaxbModel), new StreamResult(writer)); } catch(JAXBException | TransformerException e) { throw new IOException("Can't process stylesheet: " + stylesheet, e); } }
/** * Constructor called from factory * * @param busObject * @param qName * @param factory */ SourceBlockImpl(Source busObject, QName qName, BlockFactory factory) throws WebServiceException { super(busObject, null, qName, factory); // Check validity of Source if (busObject instanceof DOMSource || busObject instanceof SAXSource || busObject instanceof StreamSource || (busObject.getClass().equals(staxSource)) || busObject instanceof JAXBSource) { // Okay, these are supported Source objects if (log.isDebugEnabled()) { log.debug("data object is a " + busObject.getClass().getName()); } } else { throw ExceptionFactory.makeWebServiceException( Messages.getMessage("SourceNotSupported", busObject.getClass().getName())); } }
@Override public void writeTo( final Resource paramT, final Class<?> paramClass, final Type paramType, final Annotation[] paramArrayOfAnnotation, final MediaType paramMediaType, final MultivaluedMap<String, Object> paramMultivaluedMap, final OutputStream paramOutputStream) throws IOException, WebApplicationException { try { final Source input = new JAXBSource(newJAXBContext(), paramT); final Result result = new StreamResult(paramOutputStream); this.transformer.transform(input, result); } catch (JAXBException | TransformerException e) { LOG.error("Could not produce result", e); } }
@Override public Source build() { try { if (marshaller == null) { createDefaultMarshaller(); } final Object jaxbObject = getPreparedJaxbObject(); final JAXBSource jaxbSource = new JAXBSource(marshaller, jaxbObject); // the fake InputSource cannot be used (the Convert.java // will create a working one if it is null) jaxbSource.setInputSource(null); return jaxbSource; } catch (final JAXBException e) { throw new DataBindingException(e); } }
@Test public void export() throws JAXBException, TransformerException { JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model"); File iFile = new File("C:\\temp\\moduleTest1.xml"); //File iFile = new File("C:\\temp\\formExportTest.xml"); //File iFile = new File("C:\\temp\\section1.1.xml"); //File iFile = new File("C:\\temp\\complexSkip2.xml"); //File iFile = new File("C:\\temp\\section3.1.xml"); File oFile = new File("C:\\temp\\Book2.xml"); Unmarshaller m = jc.createUnmarshaller(); Cure xml = (Cure)m.unmarshal(iFile); StreamSource xslSource = new StreamSource("src//main//resources//xls.xsl"); //long formId = 9979; //Cure xml = dataExporter.constructFormXML(formId); JAXBSource xmlSource = new JAXBSource(jc, xml); Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource); transformer.transform(xmlSource,new StreamResult(oFile)); }
public static JAXBSource newJAXBSource(JAXBContext jaxbContext, Object object) { try { return new JAXBSource(jaxbContext, object); } catch (JAXBException ex) { throw new FluentXmlProcessingException(ex); } }
@Override public Source getPayload() { JAXBContext context = ctxt; if (context == null) { context = defaultJaxbContext.getJAXBContext(); } try { return new JAXBSource(context, o); } catch (JAXBException e) { throw new WebServiceException(e); } }
private Source getOwnedUsers(String userName) throws JAXBException { MCRUser owner = MCRUserManager.getUser(userName); List<MCRUser> listUsers = MCRUserManager.listUsers(owner); MCROwns mcrOwns = new MCROwns(); int userCount = listUsers.size(); mcrOwns.users = new MCRUser[userCount]; for (int i = 0; i < userCount; i++) { mcrOwns.users[i] = listUsers.get(i).getBasicCopy(); } return new JAXBSource(MCRUserTransformer.JAXB_CONTEXT, mcrOwns); }
@Override public Source getSource() throws IOException { try { Marshaller marshaller = getMarshaller(); return new JAXBSource(marshaller, jaxbObject); } catch (JAXBException e) { throw new IOException(e); } }
/** * Returns a JAXB SAX source. * * @return A JAXB SAX source. */ public JAXBSource getJaxbSource() throws IOException { try { return new JAXBSource(getContext(), getObject()); } catch (JAXBException e) { throw new IOException( "JAXBException while creating the JAXBSource: " + e.getMessage()); } }
/** * Transform metadata.xml into a set of parameter files for BMXLib tool. * The parameter files are created within the provided working directory. */ public void createBmxDppParameterFiles() { JAXBSource source = dppToJaxbSource(dpp); bmxDppParameters = new HashMap<>(); bmxDppParameters.put(DMFramework.UKDPP, createBmxFrameworkParameterFile(source, DMFramework.UKDPP, workingDir)); bmxDppParameters.put(DMFramework.AS11CORE, createBmxFrameworkParameterFile(source, DMFramework.AS11CORE, workingDir)); bmxDppParameters.put(DMFramework.AS11Segmentation, createBmxFrameworkParameterFile( source, DMFramework.AS11Segmentation, workingDir)); }
private JAXBSource dppToJaxbSource(DppType dpp) { try { JAXBElement<DppType> dppJaxb = new ObjectFactory().createDpp(dpp); return new JAXBSource(JAXBContext.newInstance(METADATA_PACKAGE), dppJaxb); } catch (JAXBException e) { throw new RuntimeException(e); } }
static public <T> T deepCopy(T object) { if (object == null) return null; try { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>)object.getClass(); JAXBContext context = JAXBContext.newInstance(clazz); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, object); JAXBSource source = new JAXBSource(context, contentObject); return context.createUnmarshaller().unmarshal(source, clazz).getValue(); } catch (JAXBException e) { throw new RuntimeException(e); } }
public Source invoke(final Source request) { try { JAXBContext jc = JAXBContext.newInstance(UserType.class); UserType user = (UserType)jc.createUnmarshaller().unmarshal(request); System.out.println("[string=" + user.getString() + ",qname=" + user.getQname() + "]"); return new JAXBSource(jc, user); } catch (JAXBException e) { throw new WebServiceException(e); } }
public void validar(ErrorHandler handler) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source[] schemas = new Source[] { new StreamSource(getClass().getResourceAsStream(XSD)), new StreamSource(getClass().getResourceAsStream(XSD_TFD)) }; Schema schema = sf.newSchema(schemas); Validator validator = schema.newValidator(); if (handler != null) { validator.setErrorHandler(handler); } validator.validate(new JAXBSource(context, document)); }
byte[] getOriginalBytes() throws Exception { JAXBSource in = new JAXBSource(context, document); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Result out = new StreamResult(baos); TransformerFactory factory = tf; if (factory == null) { factory = TransformerFactory.newInstance(); factory.setURIResolver(new URIResolverImpl()); } Transformer transformer = factory .newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT))); transformer.transform(in, out); return baos.toByteArray(); }
public void validar(ErrorHandler handler) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); sf.setResourceResolver(new ResourceResolver()); Source schemaFile = new StreamSource(getClass().getResourceAsStream("/xsd/v32/cfdv32.xsd")); Schema schemas = sf.newSchema(schemaFile); Validator validator = schemas.newValidator(); if (handler != null) { validator.setErrorHandler(handler); } validator.validate(new JAXBSource(context, document)); }
public void validar(ErrorHandler handler) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(getClass().getResource(XSD)); Validator validator = schema.newValidator(); if (handler != null) { validator.setErrorHandler(handler); } validator.validate(new JAXBSource(CONTEXT, tfd)); }
byte[] getOriginalBytes() throws Exception { JAXBSource in = new JAXBSource(CONTEXT, tfd); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Result out = new StreamResult(baos); TransformerFactory factory = tf; if (factory == null) { factory = TransformerFactory.newInstance(); } Transformer transformer = factory .newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT))); transformer.transform(in, out); return baos.toByteArray(); }