/** * Loads the mapping from the specified URL with type defaults to 'CastorXmlMapping'. * * @param url The URL of the mapping file. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final URL url) throws IOException, MappingException { loadMapping(url, DEFAULT_SOURCE_TYPE); }
/** * Loads the mapping from the specified URL with type defaults to * 'CastorXmlMapping'. * * @param url The URL of the mapping file. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final URL url) throws IOException, MappingException { loadMapping(url, DEFAULT_SOURCE_TYPE); }
/** * Loads the mapping from the specified URL with type defaults to 'CastorXmlMapping'. If an entity * resolver was specified, will use the entity resolver to resolve the URL. This method is also * used to load mappings referenced from another mapping or configuration file. * * @param url The URL of the mapping file. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final String url) throws IOException, MappingException { loadMapping(url, DEFAULT_SOURCE_TYPE); }
/** * Loads the mapping from the specified input source with type defaults to 'CastorXmlMapping'. * * @param source The input source. */ public void loadMapping(final InputSource source) { loadMapping(source, DEFAULT_SOURCE_TYPE); }
/** * Loads the mapping from the specified input source with type defaults to * 'CastorXmlMapping'. * * @param source The input source. */ public void loadMapping(final InputSource source) { loadMapping(source, DEFAULT_SOURCE_TYPE); }
/** * Loads the mapping from the specified URL with type defaults to * 'CastorXmlMapping'. If an entity resolver was specified, will use * the entity resolver to resolve the URL. This method is also used * to load mappings referenced from another mapping or configuration * file. * * @param url The URL of the mapping file. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final String url) throws IOException, MappingException { loadMapping(url, DEFAULT_SOURCE_TYPE); }
private void loadHistory() { try { Unmarshaller unmarshaller = new Unmarshaller( Class.forName("org.exolab.castor.gui.QueryHistory")); ClassLoader cl = ClassLoader.getSystemClassLoader(); _mapping.loadMapping(cl.getResource("org/exolab/castor/gui/Queryanlyser.xml")); unmarshaller.setMapping(_mapping); FileReader reader = new FileReader("queryhistory.xml"); _qhistory = (QueryHistory) unmarshaller.unmarshal(reader); } catch (Exception e) { e.printStackTrace(); // if there is no file, it's ok also // then we have an empty History } }
Marshaller marshaller = new Marshaller(); Mapping mapping = new Mapping(); mapping.loadMapping("test-mapping.xml"); marshaller.setMapping(mapping); TestObj obj = new TestObj(); StringWriter writer = new StringWriter(); obj.setParam1("1"); obj.setParam2("2"); marshaller.setWriter(writer); marshaller.marshal(obj); System.out.println("output:"+writer.toString());
/** * Loads the mapping from the specified URL. * * @param url The URL of the mapping file. * @param type The source type. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final URL url, final String type) throws IOException, MappingException { try { if (_resolver.getBaseURL() == null) { _resolver.setBaseURL(url); } InputSource source = _resolver.resolveEntity(null, url.toExternalForm()); if (source == null) { source = new InputSource(url.toExternalForm()); source.setByteStream(url.openStream()); } else source.setSystemId(url.toExternalForm()); LOG.info(Messages.format("mapping.loadingFrom", url.toExternalForm())); loadMapping(source, type); } catch (SAXException ex) { throw new MappingException(ex); } }
/** * Loads the mapping from the specified URL. * * @param url The URL of the mapping file. * @param type The source type. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final URL url, final String type) throws IOException, MappingException { try { if (_resolver.getBaseURL() == null) { _resolver.setBaseURL(url); } InputSource source = _resolver.resolveEntity(null, url.toExternalForm()); if (source == null) { source = new InputSource(url.toExternalForm()); source.setByteStream(url.openStream()); } else source.setSystemId(url.toExternalForm()); LOG.info(Messages.format("mapping.loadingFrom", url.toExternalForm())); loadMapping(source, type); } catch (SAXException ex) { throw new MappingException(ex); } }
protected void createCastorClassDescriptorResolver(String mappingFile) throws MappingException { Mapping mapping=null; try { InputStream stream = getClass().getResourceAsStream(mappingFile); if (log.isDebugEnabled()) { log.debug("Loading psml mapping file " + mappingFile); } mapping = new Mapping(); InputSource is = new InputSource(stream); is.setSystemId(mappingFile); mapping.loadMapping(is); } catch (Exception e) { IllegalStateException ise = new IllegalStateException("Error in psml mapping creation"); ise.initCause(e); throw ise; } this.classDescriptorResolver = ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML); MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller(); MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML); classDescriptorResolver.setMappingLoader(mappingLoader); }
/** * Loads a package mapping file for the given package name using the * provided ClassLoader. * * @param packageName The name of the package to load the mapping file for. * @return The loaded Mapping or <code>null</code> if no mapping file * is available for the given package. * @throws MappingException */ private Mapping loadMapping(final String packageName, final ClassLoader classLoader) throws MappingException { URL url = classLoader.getResource(ResolveHelpers.getQualifiedFileName( XMLConstants.PKG_MAPPING_FILE, packageName)); if (url == null) { return null; } try { Mapping mapping = new Mapping(classLoader); mapping.loadMapping(url); return mapping; } catch (java.io.IOException ioex) { throw new MappingException(ioex); } }
/** * Loads a package mapping file for the given package name using the provided ClassLoader. * * @param packageName The name of the package to load the mapping file for. * @return The loaded Mapping or <code>null</code> if no mapping file is available for the given * package. * @throws MappingException */ private Mapping loadMapping(final String packageName, final ClassLoader classLoader) throws MappingException { URL url = classLoader.getResource( ResolveHelpers.getQualifiedFileName(XMLConstants.PKG_MAPPING_FILE, packageName)); if (url == null) { return null; } try { Mapping mapping = new Mapping(classLoader); mapping.loadMapping(url); return mapping; } catch (java.io.IOException ioex) { throw new MappingException(ioex); } }
/** * Loads the mapping from the specified URL. If an entity resolver * was specified, will use the entity resolver to resolve the URL. * This method is also used to load mappings referenced from another * mapping or configuration file. * * @param url The URL of the mapping file. * @param type The source type. * @throws IOException An error occured when reading the mapping file. * @throws MappingException The mapping file is invalid. */ public void loadMapping(final String url, final String type) throws IOException, MappingException { String location = url; if (_resolver.getBaseURL() == null) { setBaseURL(location); location = URIUtils.getRelativeURI(location); } try { InputSource source = _resolver.resolveEntity(null, location); if (source == null) { source = new InputSource(location); } if (source.getSystemId() == null) { source.setSystemId(location); } LOG.info(Messages.format("mapping.loadingFrom", location)); loadMapping(source, type); } catch (SAXException ex) { throw new MappingException(ex); } }
loadMapping(source, type); } catch (SAXException ex) { throw new MappingException(ex);
public Html unMarshall(String xml) throws Exception { xmlContext = new XMLContext(); Mapping mapping = xmlContext.createMapping(); mapping.loadMapping(coreResources.getURL("xformMapping.xml")); xmlContext.addMapping(mapping); // XML to Object Reader reader = new StringReader(xml); Unmarshaller unmarshaller = xmlContext.createUnmarshaller(); unmarshaller.setClass(Html.class); unmarshaller.setWhitespacePreserve(false); Html html = (Html) unmarshaller.unmarshal(reader); reader.close(); return html; }
/** * Creates the Castor <code>XMLContext</code>. Subclasses can override this to create a custom context. * <p/> * The default implementation loads mapping files if defined, and the target class if not defined. * * @return the created resolver * @throws MappingException when the mapping file cannot be loaded * @throws IOException in case of I/O errors * @see XMLContext#addMapping(org.exolab.castor.mapping.Mapping) * @see XMLContext#addClass(Class) */ protected XMLContext createXMLContext(Resource[] mappingLocations, Class targetClass) throws MappingException, IOException, ResolverException { XMLContext context = new XMLContext(); if (!ObjectUtils.isEmpty(mappingLocations)) { Mapping mapping = new Mapping(); for (int i = 0; i < mappingLocations.length; i++) { mapping.loadMapping(SaxUtils.createInputSource(mappingLocations[i])); } context.addMapping(mapping); } if (targetClass != null) { context.addClass(targetClass); } return context; }
@Override public List<StatisticDescriptor> getStatisticDescriptors(String filePath) throws ParserException { try { Mapping mapping = new Mapping(); mapping.loadMapping(FileUtils.findFileAsResource("StatisticDescriptorMapping.xml")); XMLContext context = new XMLContext(); context.addMapping(mapping); InputSource source = new InputSource(FileUtils.findFileAsStream(filePath)); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setClass(StatisticList.class); StatisticList list = (StatisticList)unmarshaller.unmarshal(source); validate(list.getDescriptors()); return list.getDescriptors(); } catch (Exception e) { ParserException parserException = new ParserException("Exception parsing statistic descriptor files", e); Logger.getLogger(this.getClass()).error(parserException.getMessage(), parserException); throw parserException; } }
public String marshall(Html html) throws Exception { StringWriter writer = new StringWriter(); xmlContext = new XMLContext(); Mapping mapping = xmlContext.createMapping(); mapping.loadMapping(coreResources.getURL("xformMapping.xml")); xmlContext.addMapping(mapping); Marshaller marshaller = xmlContext.createMarshaller(); marshaller.setNamespaceMapping("h", "http://www.w3.org/1999/xhtml"); marshaller.setNamespaceMapping("jr", "http://openrosa.org/javarosa"); marshaller.setNamespaceMapping("xsd", "http://www.w3.org/2001/XMLSchema"); marshaller.setNamespaceMapping("ev", "http://www.w3.org/2001/xml-events"); marshaller.setNamespaceMapping("", "http://www.w3.org/2002/xforms"); marshaller.setNamespaceMapping("enk", "http://enketo.org/xforms"); marshaller.setNamespaceMapping("oc", "http://openclinica.org/xforms"); marshaller.setWriter(writer); marshaller.marshal(html); String xform = writer.toString(); return xform; }
protected XMLContext createXMLContext(ClassResolver resolver, ClassLoader contextClassLoader) throws Exception { XMLContext xmlContext = new XMLContext(); if (ObjectHelper.isNotEmpty(getMappingFile())) { Mapping xmlMap; if (contextClassLoader != null) { xmlMap = new Mapping(contextClassLoader); } else { xmlMap = new Mapping(); } xmlMap.loadMapping(resolver.loadResourceAsURL(getMappingFile())); xmlContext.addMapping(xmlMap); } if (getPackages() != null) { xmlContext.addPackages(getPackages()); } if (getClassNames() != null) { for (String name : getClassNames()) { Class<?> clazz = resolver.resolveClass(name); xmlContext.addClass(clazz); } } return xmlContext; }