本文整理了Java中org.exolab.castor.xml.Marshaller
类的一些代码示例,展示了Marshaller
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Marshaller
类的具体详情如下:
包路径:org.exolab.castor.xml.Marshaller
类名称:Marshaller
[英]A Marshaller that serializes Java Object's to XML Note: This class is not thread safe, and not intended to be, so please create a new Marshaller for each thread if it is to be used in a multithreaded environment.
[中]
代码示例来源:origin: org.codehaus.castor/castor-xml
/**
*
*
* @param out
* @throws org.exolab.castor.xml.MarshalException if object is
* null or if any SAXException is thrown during marshaling
* @throws org.exolab.castor.xml.ValidationException if this
* object is an invalid instance according to the schema
*/
public void marshal(
final java.io.Writer out)
throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
org.exolab.castor.xml.Marshaller.marshal(this, out);
}
代码示例来源:origin: org.codehaus.castor/castor-jdo
public void saveHistory() {
// write back the history to file
try {
FileWriter writer = new FileWriter("queryhistory.xml");
Marshaller marshaller = new Marshaller(writer);
marshaller.setMapping(_mapping);
marshaller.marshal(_qhistory);
} catch (Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: com.github.muff1nman.chameleon/core
/**
* Writes the specified object as an XML stream to an output writer.
* @param o the object to serialize.
* @param out the writer.
* @param asDocument if <code>true</code>, indicates to marshal as a complete XML document, which includes the XML declaration, and if necessary the DOCTYPE declaration.
* @throws MappingException an exception indicating an invalid mapping error.
* @throws org.exolab.castor.xml.MarshalException a marshalling exception.
* @throws org.exolab.castor.xml.ValidationException an XML validation error occurred.
* @throws NullPointerException if <code>o</code> is <code>null</code>.
* @throws NullPointerException if <code>out</code> is <code>null</code>.
* @see Mapping
*/
public void marshal(final Object o, final Writer out, final boolean asDocument) throws Exception
{
synchronized(_marshaller)
{
_marshaller.setWriter(out); // May throw IOException.
_marshaller.setMarshalAsDocument(asDocument);
_marshaller.setEncoding("ISO-8859-1");
// Do not use marshal(Object object, Writer out): IT DOESN'T WORK !!!
_marshaller.marshal(o); // May throw MarshalException, ValidationException.
}
}
代码示例来源:origin: org.codehaus.castor/castor-xml
/**
* Marshals the given Object as XML using the given DocumentHandler to send events to.
*
* @param object The Object to marshal.
* @param handler The DocumentHandler to marshal to.
* @exception org.exolab.castor.xml.MarshalException
* @exception org.exolab.castor.xml.ValidationException
*/
public static void marshal(Object object, DocumentHandler handler)
throws MarshalException, ValidationException {
staticMarshal(object, new Marshaller(handler));
} // -- marshal
代码示例来源:origin: org.codehaus.castor/com.springsource.org.castor
/**
* Creates a new Marshaller with the given writer.
* @param out the Writer to serialize to
**/
public Marshaller( Writer out )
throws IOException
{
initialize();
setWriter(out);
} //-- Marshaller
代码示例来源:origin: axis/axis
try {
AxisContentHandler hand = new AxisContentHandler(context);
Marshaller marshaller = new Marshaller(hand);
marshaller.setMarshalAsDocument(false);
String localPart = name.getLocalPart();
int arrayDims = localPart.indexOf('[');
localPart = localPart.substring(0, arrayDims);
marshaller.setRootElement(localPart);
marshaller.marshal(value);
} catch (MarshalException me) {
log.error(Messages.getMessage("castorMarshalException00"), me);
代码示例来源:origin: org.codehaus.castor/castor-xml
public static void main(String[] args) {
// TODO: add CommandLineOptions
// options needed
// 1. filename/path of CHANGELOG
// 2. mapping file for customization
// 3. output file name
XMLContext xmlContext = new XMLContext();
ChangeLog2XML parser = xmlContext.createChangeLog2XML();
try {
File file = new File(DEFAULT_FILE);
Changelog changelog = parser.parse(file);
file = new File(DEFAULT_OUTPUT);
FileWriter writer = new FileWriter(file);
xmlContext.setProperty(XMLProperties.USE_INDENTATION, true);
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setWriter(writer);
marshaller.setRootElement("changelog");
marshaller.setSuppressXSIType(true);
marshaller.marshal(changelog);
} catch (Exception ex) {
ex.printStackTrace();
}
}
代码示例来源:origin: org.apache.camel/camel-castor
public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
Writer writer = new OutputStreamWriter(outputStream, encoding);
Marshaller marshaller = createMarshaller(exchange);
marshaller.setWriter(writer);
marshaller.marshal(body);
if (contentTypeHeader) {
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/xml");
} else {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
}
}
}
代码示例来源:origin: org.codehaus.castor/castor-xml
/**
* Serializes the mapping to the given writer.
*
* @param writer the Writer to serialize the mapping to
* @throws MappingException if writing the mapping information fails
*/
public void write(final Writer writer) throws MappingException {
Marshaller marshal;
MappingRoot mapping;
Enumeration enumeration;
try {
mapping = new MappingRoot();
mapping.setDescription("Castor generated mapping file");
enumeration = _mappings.elements();
while (enumeration.hasMoreElements()) {
mapping.addClassMapping((ClassMapping) enumeration.nextElement());
}
marshal = new Marshaller(writer);
marshal.setNamespaceMapping(null, "http://castor.exolab.org/");
marshal.setNamespaceMapping("cst", "http://castor.exolab.org/");
marshal.marshal(mapping);
} catch (Exception except) {
throw new MappingException(except);
}
} // -- write
代码示例来源:origin: org.codehaus.xfire/xfire-castor
marshaller = new Marshaller(new SafeContentHandler(new ContentHandlerToXMLStreamWriter(
sWriter)));
marshaller.setRootElement(part.getName().getLocalPart());
if (mapping != null)
marshaller.setMapping(mapping);
marshaller.marshal(object);
代码示例来源:origin: OpenClinica/OpenClinica
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;
}
代码示例来源:origin: org.codehaus.castor/castor-codegen
/**
* Generates the mapping file.
*
* @param packageName Package name to be generated
* @param sInfo Source Generator current state
* @throws IOException if this Exception occurs while generating the mapping file
*/
private void generateMappingFile(final String packageName, final SGStateInfo sInfo)
throws IOException {
String pkg = (packageName != null) ? packageName : "";
MappingRoot mapping = sInfo.getMapping(pkg);
if (mapping == null) {
return;
}
FileWriter writer = new FileWriter(_mappingFilename);
try {
Marshaller marshaller = new Marshaller(writer);
marshaller.setSuppressNamespaces(true);
marshaller.marshal(mapping);
} catch (Exception ex) {
throw new NestedIOException(ex);
} finally {
writer.flush();
writer.close();
}
}
代码示例来源:origin: org.apache.portals.jetspeed-2/jetspeed-page-manager
final ContentHandler handler = xmlWriter;
Marshaller marshaller = new Marshaller(new ContentHandler()
marshaller.setResolver((XMLClassDescriptorResolver) classDescriptorResolver);
marshaller.setValidation(false); // results in better performance
marshaller.marshal(document);
代码示例来源:origin: apache/servicemix-bundles
/**
* Template method that allows for customizing of the given Castor {@link Marshaller}.
*/
protected void customizeMarshaller(Marshaller marshaller) {
marshaller.setValidation(this.validating);
marshaller.setSuppressNamespaces(this.suppressNamespaces);
marshaller.setSuppressXSIType(this.suppressXsiType);
marshaller.setMarshalAsDocument(this.marshalAsDocument);
marshaller.setMarshalExtendedType(this.marshalExtendedType);
marshaller.setRootElement(this.rootElement);
marshaller.setNoNamespaceSchemaLocation(this.noNamespaceSchemaLocation);
marshaller.setSchemaLocation(this.schemaLocation);
marshaller.setUseXSITypeAtRoot(this.useXSITypeAtRoot);
if (this.doctypes != null) {
this.doctypes.forEach(marshaller::setDoctype);
}
if (this.processingInstructions != null) {
this.processingInstructions.forEach(marshaller::addProcessingInstruction);
}
if (this.namespaceMappings != null) {
this.namespaceMappings.forEach(marshaller::setNamespaceMapping);
}
}
代码示例来源:origin: com.github.muff1nman.chameleon/playlist-atom
@Override
public void writeTo(final OutputStream out, final String encoding) throws Exception
{
// Marshal the document.
final StringWriter writer = new StringWriter();
final XmlSerializer serializer = XmlSerializer.getMapping("chameleon/atom"); // May throw Exception.
// Specifies whether XML documents (as generated at marshalling) should use indentation or not. Default is false.
serializer.getMarshaller().setProperty("org.exolab.castor.indent", "true");
//serializer.getMarshaller().setNamespaceMapping("", "http://www.w3.org/2005/Atom");
serializer.marshal(_feed, writer, false); // May throw Exception.
String enc = encoding;
if (enc == null)
{
enc = "UTF-8";
}
final byte[] bytes = writer.toString().getBytes(enc); // May throw UnsupportedEncodingException.
out.write(bytes); // Throws NullPointerException if out is null. May throw IOException.
out.flush(); // May throw IOException.
}
代码示例来源:origin: org.springframework.ws/spring-oxm
/**
* Template method that allows for customizing of the given Castor {@link Marshaller}.
* <p/>
* Default implementation invokes {@link Marshaller#setValidation(boolean)} with the property set on this
* marshaller, and calls {@link Marshaller#setNamespaceMapping(String, String)} with the {@linkplain
* #setNamespaceMappings(java.util.Properties) namespace mappings}.
*/
protected void customizeMarshaller(Marshaller marshaller) {
marshaller.setValidation(isValidating());
marshaller.setSuppressNamespaces(isSuppressNamespaces());
marshaller.setSuppressXSIType(isSuppressXsiType());
Properties namespaceMappings = getNamespaceMappings();
if (namespaceMappings != null) {
for (Iterator iterator = namespaceMappings.keySet().iterator(); iterator.hasNext();) {
String prefix = (String) iterator.next();
String uri = namespaceMappings.getProperty(prefix);
marshaller.setNamespaceMapping(prefix, uri);
}
}
}
代码示例来源:origin: com.github.muff1nman.chameleon/core
/**
* Initializes the Castor mapping instance.
* @param mapping a Castor mapping. Shall not be <code>null</code>.
* @throws NullPointerException if <code>mapping</code> is <code>null</code>.
* @throws MappingException an exception indicating an invalid mapping error.
*/
private void setMapping(final Mapping mapping) throws MappingException
{
_unmarshaller = new Unmarshaller(mapping); // May throw MappingException.
_unmarshaller.setValidation(false);
_unmarshaller.setIgnoreExtraElements(true);
_marshaller = new Marshaller();
_marshaller.setMapping(mapping); // May throw MappingException.
_marshaller.setValidation(false);
//_marshaller.setDebug(true);
// Specifies whether to support XML namespaces by default. Default is false.
//_marshaller.setProperty("org.exolab.castor.parser.namespaces", "true");
// Specifies whether XML documents (as generated at marshalling) should use indentation or not. Default is false.
//_marshaller.setProperty("org.exolab.castor.indent", "true");
}
}
代码示例来源:origin: apache/servicemix-bundles
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
Assert.state(this.xmlContext != null, "CastorMarshaller not initialized");
Marshaller marshaller = this.xmlContext.createMarshaller();
marshaller.setWriter(writer);
doMarshal(graph, marshaller);
}
代码示例来源:origin: com.github.muff1nman.chameleon/playlist-rss
@Override
public void writeTo(final OutputStream out, final String encoding) throws Exception
{
// Marshal the RSS document.
final StringWriter writer = new StringWriter();
final XmlSerializer serializer = XmlSerializer.getMapping("chameleon/rss"); // May throw Exception.
// Specifies whether XML documents (as generated at marshalling) should use indentation or not. Default is false.
serializer.getMarshaller().setProperty("org.exolab.castor.indent", "true");
//serializer.getMarshaller().setNamespaceMapping("", "http://purl.org/rss/1.0/modules/content/");
serializer.getMarshaller().setNamespaceMapping("media", "http://search.yahoo.com/mrss/");
serializer.marshal(_rss, writer, false); // May throw Exception.
String enc = encoding;
if (enc == null)
{
enc = "UTF-8";
}
final byte[] bytes = writer.toString().getBytes(enc); // May throw UnsupportedEncodingException.
out.write(bytes); // Throws NullPointerException if out is null. May throw IOException.
out.flush(); // May throw IOException.
}
代码示例来源:origin: org.codehaus.castor/castor-testsuite-xml-framework
private Marshaller createMarshaler(final File marshalOutput) throws Exception {
getXMLContext().getInternalContext().getXMLClassDescriptorResolver().cleanDescriptorCache();
Marshaller marshaller = getXMLContext().createMarshaller();
marshaller.setWriter(new FileWriter(marshalOutput));
// -- Configuration for marshaler? Use config from unit test case if available
Configuration config = _unitTest.getConfiguration();
if (config == null) {
config = _configuration;
}
if (config != null) {
ConfigurationType marshal = config.getMarshal();
List returnValues = invokeEnumeratedMethods(marshaller, marshal);
returnValues.clear(); // We don't care about the return values
} // -- config != null
if (_mapping != null) {
marshaller.setMapping(_mapping);
}
if (_listener != null && _listener instanceof MarshalListener
&& _listenerType != TypeType.UNMARSHAL) {
marshaller.setMarshalListener((MarshalListener) _listener);
}
return marshaller;
}
内容来源于网络,如有侵权,请联系作者删除!