Search in sources :

Example 1 with FieldDictionary

use of com.thoughtworks.xstream.converters.reflection.FieldDictionary in project camel by apache.

the class DefaultCompositeApiClient method configureXStream.

static XStream configureXStream() {
    final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
    final XppDriver hierarchicalStreamDriver = new XppDriver(new NoNameCoder()) {

        @Override
        public HierarchicalStreamWriter createWriter(final Writer out) {
            return new CompactWriter(out, getNameCoder());
        }
    };
    final XStream xStream = new XStream(reflectionProvider, hierarchicalStreamDriver);
    xStream.aliasSystemAttribute(null, "class");
    xStream.ignoreUnknownElements();
    XStreamUtils.addDefaultPermissions(xStream);
    xStream.registerConverter(new DateTimeConverter());
    xStream.setMarshallingStrategy(new TreeMarshallingStrategy());
    xStream.processAnnotations(ADDITIONAL_TYPES);
    return xStream;
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) TreeMarshallingStrategy(com.thoughtworks.xstream.core.TreeMarshallingStrategy) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with FieldDictionary

use of com.thoughtworks.xstream.converters.reflection.FieldDictionary in project camel by apache.

the class SObjectBatchTest method shouldSerializeToXml.

@Test
public void shouldSerializeToXml() {
    final String xml = //
    "<batch>\n" + //
    "  <batchRequests>\n" + //
    "    <batchRequest>\n" + //
    "      <method>POST</method>\n" + //
    "      <url>v37.0/sobjects/Account/</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewAccountName</Name>\n" + //
    "          <Industry>Environmental</Industry>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>DELETE</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ?fields=Name,BillingPostalCode</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ/CreatedBy?fields=Name</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/limits/</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "          <AccountNumber>AC12345</AccountNumber>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/some/url</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/query/?q=SELECT Name FROM Account</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/queryAll/?q=SELECT Name FROM Account</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/search/?q=FIND {joe}</url>\n" + //
    "    </batchRequest>\n" + //
    "  </batchRequests>\n" + "</batch>";
    final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
    final XStream xStream = new XStream(reflectionProvider);
    xStream.aliasSystemAttribute(null, "class");
    xStream.processAnnotations(SObjectBatch.class);
    xStream.processAnnotations(batch.objectTypes());
    final String serialized = xStream.toXML(batch);
    assertEquals("Should serialize as expected by Salesforce", xml, serialized);
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider) Test(org.junit.Test)

Example 3 with FieldDictionary

use of com.thoughtworks.xstream.converters.reflection.FieldDictionary in project gate-core by GateNLP.

the class PersistenceManager method saveObjectToFile.

/**
 * Save the given object to the given file.
 *
 * @param obj The object to persist.
 * @param file The file where to persist to
 * @param usegatehome if true (recommended) use $gatehome$ and $resourceshome$ instead of
 * $relpath$ in any saved path URLs if the location of that URL is inside GATE home or
 * inside the resources home directory (if set).
 * @param warnaboutgatehome if true, issue a warning message when a saved URL uses $gatehome$
 * or $resourceshome$.
 * @throws PersistenceException
 * @throws IOException
 */
public static void saveObjectToFile(Object obj, File file, boolean usegatehome, boolean warnaboutgatehome) throws PersistenceException, IOException {
    ProgressListener pListener = (ProgressListener) Gate.getListeners().get("gate.event.ProgressListener");
    StatusListener sListener = (gate.event.StatusListener) Gate.getListeners().get("gate.event.StatusListener");
    long startTime = System.currentTimeMillis();
    if (pListener != null)
        pListener.progressChanged(0);
    // The object output stream is used for native serialization,
    // but the xstream and filewriter are used for XML serialization.
    ObjectOutputStream oos = null;
    com.thoughtworks.xstream.XStream xstream = null;
    HierarchicalStreamWriter writer = null;
    warnAboutGateHome.get().addFirst(warnaboutgatehome);
    useGateHome.get().addFirst(usegatehome);
    startPersistingTo(file);
    try {
        if (Gate.getUseXMLSerialization()) {
            // Just create the xstream and the filewriter that will later be
            // used to serialize objects.
            xstream = new XStream(new SunUnsafeReflectionProvider(new FieldDictionary(new XStream12FieldKeySorter())), new StaxDriver(new XStream11NameCoder())) {

                @Override
                protected boolean useXStream11XmlFriendlyMapper() {
                    return true;
                }
            };
            FileWriter fileWriter = new FileWriter(file);
            writer = new PrettyPrintWriter(fileWriter, new XmlFriendlyNameCoder("-", "_"));
        } else {
            oos = new ObjectOutputStream(new FileOutputStream(file));
        }
        Object persistentList = getPersistentRepresentation(Gate.getCreoleRegister().getPlugins());
        Object persistentObject = getPersistentRepresentation(obj);
        if (Gate.getUseXMLSerialization()) {
            // We need to put the urls and the application itself together
            // as xstreams can only hold one object.
            GateApplication gateApplication = new GateApplication();
            // gateApplication.workspace = new File("cache");
            gateApplication.urlList = persistentList;
            gateApplication.application = persistentObject;
            // Then do the actual serialization.
            xstream.marshal(gateApplication, writer);
        } else {
            // This is for native serialization.
            oos.writeObject(persistentList);
            // now write the object
            oos.writeObject(persistentObject);
        }
    } finally {
        finishedPersisting();
        if (oos != null) {
            oos.flush();
            oos.close();
        }
        if (writer != null) {
            // Just make sure that all the xml is written, and the file
            // closed.
            writer.flush();
            writer.close();
        }
        long endTime = System.currentTimeMillis();
        if (sListener != null)
            sListener.statusChanged("Storing completed in " + NumberFormat.getInstance().format((double) (endTime - startTime) / 1000) + " seconds");
        if (pListener != null)
            pListener.processFinished();
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) XStream12FieldKeySorter(com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter) FileWriter(java.io.FileWriter) XmlFriendlyNameCoder(com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder) ObjectOutputStream(java.io.ObjectOutputStream) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) ProgressListener(gate.event.ProgressListener) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) XStream11NameCoder(com.thoughtworks.xstream.io.xml.XStream11NameCoder) FileOutputStream(java.io.FileOutputStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) StatusListener(gate.event.StatusListener) SunUnsafeReflectionProvider(com.thoughtworks.xstream.converters.reflection.SunUnsafeReflectionProvider)

Aggregations

XStream (com.thoughtworks.xstream.XStream)3 FieldDictionary (com.thoughtworks.xstream.converters.reflection.FieldDictionary)3 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)2 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)2 AnnotationFieldKeySorter (org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 SunUnsafeReflectionProvider (com.thoughtworks.xstream.converters.reflection.SunUnsafeReflectionProvider)1 XStream12FieldKeySorter (com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter)1 TreeMarshallingStrategy (com.thoughtworks.xstream.core.TreeMarshallingStrategy)1 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)1 CompactWriter (com.thoughtworks.xstream.io.xml.CompactWriter)1 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)1 XStream11NameCoder (com.thoughtworks.xstream.io.xml.XStream11NameCoder)1 XmlFriendlyNameCoder (com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder)1 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)1 ProgressListener (gate.event.ProgressListener)1 StatusListener (gate.event.StatusListener)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1