Search in sources :

Example 1 with Marshaller

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;
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) StringWriter(java.io.StringWriter) EntityT(alma.entities.commonentity.EntityT) EntityException(alma.acs.entityutil.EntityException) XmlEntityStruct(alma.xmlentity.XmlEntityStruct) EntityException(alma.acs.entityutil.EntityException)

Example 2 with Marshaller

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;
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller)

Example 3 with 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));
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) RandomAccessFile(java.io.RandomAccessFile) FileWriter(java.io.FileWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Statistics(alma.alarmsystem.statistics.generated.Statistics) BufferedWriter(java.io.BufferedWriter)

Example 4 with Marshaller

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;
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) FileWriter(java.io.FileWriter) File(java.io.File)

Example 5 with Marshaller

use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.

the class XMLMessageHelper method marshal.

/**
   * DOCUMENT ME!
   *
   * @param asiMessage DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   *
   * @throws Exception DOCUMENT ME!
   */
public static String marshal(ASIMessage asiMessage) throws Exception {
    StringWriter writer = new StringWriter();
    Marshaller marshaller = new Marshaller(writer);
    marshaller.setSuppressNamespaces(true);
    marshaller.marshal(asiMessage);
    return writer.toString();
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) StringWriter(java.io.StringWriter)

Aggregations

Marshaller (org.exolab.castor.xml.Marshaller)25 StringWriter (java.io.StringWriter)14 IOException (java.io.IOException)11 MarshalException (org.exolab.castor.xml.MarshalException)7 ValidationException (org.exolab.castor.xml.ValidationException)7 Mapping (org.exolab.castor.mapping.Mapping)6 XMLContext (org.exolab.castor.xml.XMLContext)6 LaserObjectNotFoundException (cern.laser.business.LaserObjectNotFoundException)4 File (java.io.File)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 MalformedURLException (java.net.MalformedURLException)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 FaultFamily (alma.acs.alarmsystem.generated.FaultFamily)2 CDBRecordDoesNotExistEx (alma.cdbErrType.CDBRecordDoesNotExistEx)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2