use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class StatHashMap method getMarshaller.
/**
* Return a properly configured marshaller for
* writing in the file.
*
* @return The marshaller
* @throws IOException If the witer is invalid for the marshaller
*/
private Marshaller getMarshaller(Writer writer) throws IOException {
if (writer == null) {
throw new IllegalArgumentException("Invalid null writer");
}
Marshaller marshaller = new Marshaller(writer);
marshaller.setEncoding("ISO-8859-1");
marshaller.setSuppressNamespaces(true);
marshaller.setSupressXMLDeclaration(true);
marshaller.setSuppressXSIType(true);
return marshaller;
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class StatHashMap method openOutputFile.
/**
* Open and create the output stream for writing statistics on file.
* <P>
* The file is opened for each writing and closed immediately.
* A new file is created, whenever the size of the actual file is
* greater then {@link #maxFileSize}.
*
* @return The stream for writing into the file
* @throws IOException If can't open/create the file for writing
* @throws ValidationException In case of error validating the Statistics element (should never happen)
* @throws MarshalException Error writing Statistics header in the file
*/
private BufferedWriter openOutputFile() throws IOException, MarshalException, ValidationException {
String actualFileName = fileNamePrefix + fileNumber + ".xml";
String folderName = folder.getAbsolutePath();
if (!folderName.endsWith("" + File.separator)) {
folderName = folderName + File.separator;
}
// Check the size of the file if it exists
File f = new File(folderName + actualFileName);
if (f.exists() && f.length() > maxFileSize) {
fileNumber++;
return openOutputFile();
}
if (f.length() == 0) {
// New file: write the header
Statistics statistics = new Statistics();
BufferedWriter outF = new BufferedWriter(new FileWriter(f, true));
Marshaller marshaller = getMarshaller(outF);
marshaller.setSupressXMLDeclaration(false);
marshaller.marshal(statistics);
outF.flush();
outF.close();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(raf.length() - 3);
raf.seek(raf.length());
raf.writeBytes(">\n");
raf.close();
} else {
// Remove the closing tag (closeXMLTag) before adding a new record
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(raf.length() - closeXMLTag.length() - 1);
raf.seek(raf.length());
raf.writeBytes("\n");
raf.close();
}
return new BufferedWriter(new FileWriter(f, true));
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class XmlInOut method marshalObsProposalToFile.
public String marshalObsProposalToFile(ObsProposal prop, String originalXmlFilename) throws MarshalException, ValidationException, IOException {
String filename = getRemarshaledFilename(originalXmlFilename);
File xmlFile = new File(module_dir, pathToXmlSamples + filename);
Marshaller m = new Marshaller(new FileWriter(xmlFile));
m.setNamespaceMapping("tprj", "AlmaTest/ObsProject");
m.marshal(prop);
return filename;
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class CastorMarshalMapper method translate.
/**
* Converts a Castor generated entity object to the marshalled representation as
* an <code>XmlEntityStruct</code>.
*
* @see alma.acs.component.dynwrapper.TypeMapper#translate(java.lang.Object, java.lang.Object, java.lang.Class,
* alma.acs.component.dynwrapper.ComponentInvocationHandler)
*/
@SuppressWarnings("unchecked")
public Object translate(Object oldObject, Object newObjectTemplate, Class newObjectClass, ComponentInvocationHandler invHandler) throws DynWrapperException {
if (oldObject == null) {
return null;
}
EntityT entityMeta;
try {
entityMeta = m_entityTFinder.extractEntityT(oldObject);
} catch (EntityException e) {
String extractionErrMsg = "failed to extract EntityT object from " + oldObject.getClass().getName();
throw new DynWrapperException(extractionErrMsg, e);
}
if (entityMeta == null) {
String extractionNullErrMsg = "entity object of type " + oldObject.getClass().getName() + " must have a non-null child assignable to " + EntityT.class.getName() + " to be serialized.";
throw new DynWrapperException(extractionNullErrMsg);
}
XmlEntityStruct entStruct = null;
// dealt with using a Holder class. Just confused right now...
if (newObjectTemplate != null) {
if (!XmlEntityStruct.class.isInstance(newObjectTemplate)) {
throw new DynWrapperException("invalid template for XmlEntityStruct object");
}
entStruct = (XmlEntityStruct) newObjectTemplate;
} else {
entStruct = new XmlEntityStruct();
}
entStruct.entityId = entityMeta.getEntityId();
entStruct.entityTypeName = entityMeta.getEntityTypeName();
entStruct.schemaVersion = (entityMeta.getSchemaVersion() != null ? entityMeta.getSchemaVersion() : "");
try {
StringWriter wr = new StringWriter();
Marshaller marsh = new Marshaller(wr);
marsh.setValidation(false);
marsh.marshal(oldObject);
entStruct.xmlString = wr.toString();
} catch (Exception e) {
String msg = "failed to marshal entity object with id='" + entityMeta.getEntityId() + "' using the Castor Marshaller without validation.";
throw new DynWrapperException(msg);
}
if (m_verbose) {
m_logger.finer("successfully translated from '" + oldObject.getClass().getName() + "' to '" + entStruct.getClass().getName() + "'.");
}
return entStruct;
}
use of org.exolab.castor.xml.Marshaller in project camel by apache.
the class AbstractCastorDataFormat method createMarshaller.
public Marshaller createMarshaller(Exchange exchange) throws Exception {
// need to create new marshaller as we may have concurrent processing
Marshaller answer = xmlContext.createMarshaller();
answer.setValidation(isValidation());
return answer;
}
Aggregations