Example usage for java.beans XMLEncoder XMLEncoder

List of usage examples for java.beans XMLEncoder XMLEncoder

Introduction

In this page you can find the example usage for java.beans XMLEncoder XMLEncoder.

Prototype

public XMLEncoder(OutputStream out) 

Source Link

Document

Creates a new XML encoder to write out JavaBeans to the stream out using an XML encoding.

Usage

From source file:Main.java

/**
 * Creates from the given Object an xml string.
 * /*  w w w  .j av  a2  s .c o m*/
 * @param <T>
 *            the generic type of the return type
 * @param obj
 *            the obj to transform to an xml string.
 * @return the xml string
 */
public static <T> String toXmlWithXMLEncoder(final T obj) {
    XMLEncoder enc = null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        enc.close();
        enc = null;
    } finally {
        if (enc != null) {
            enc.close();
        }
    }
    return baos.toString();
}

From source file:fi.vm.sade.log.client.LoggerJms.java

/**
 * Encode messages to string (xml)./*from w w  w .j  av  a2s . c  o  m*/
 *
 * @param event
 * @return
 */
public static String encode(LogEvent event) {
    if (event == null) {
        return null;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder xmlEncoder = new XMLEncoder(baos);
    xmlEncoder.writeObject(event);
    xmlEncoder.close();

    return baos.toString();
}

From source file:org.cesecore.util.HashMapTest.java

@SuppressWarnings("rawtypes")
@Test//w w w  .j a v  a  2s  .  c  om
public void testHashMapNormal() throws Exception {
    HashMap<String, Comparable> a = new HashMap<String, Comparable>();
    a.put("foo0", Boolean.FALSE);
    a.put("foo1", "fooString");
    a.put("foo2", Integer.valueOf(2));
    a.put("foo3", Boolean.TRUE);

    // Write to XML
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.writeObject(a);
    encoder.close();
    String data = baos.toString("UTF8");
    //log.error(data);

    XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(data.getBytes("UTF8")));
    HashMap<?, ?> b = (HashMap<?, ?>) decoder.readObject();
    decoder.close();
    assertEquals(((Boolean) b.get("foo0")).booleanValue(), false);
    assertEquals(((Boolean) b.get("foo3")).booleanValue(), true);
    assertEquals(((String) b.get("foo1")), "fooString");
    assertEquals(((Integer) b.get("foo2")).intValue(), 2);

}

From source file:opdracht2.FileTabellen.java

public static void createBigXMLArtikelTabel(String filename, int entries) {

    try (XMLEncoder encoder = new XMLEncoder(new FileOutputStream(filename))) {

        HashMap<Integer, ArtikelPOJO> catalogus = new HashMap<>();
        ArtikelPOJO artikel;//w ww. j  ava  2s  .  c o  m
        for (int i = 0; i < entries; i++) {
            artikel = new ArtikelPOJO();
            Random rng = new Random();
            artikel.setArtikelID(i);
            artikel.setArtikelNaam(FillBatchDatabase.generateString(rng, "MooieNamenString", 10));
            artikel.setArtikelPrijs(rng.nextInt(100));
            catalogus.put(i, artikel);
        }
        encoder.writeObject(catalogus);
    } catch (IOException ex) {
        LOGGER.error("kan XMLcatalogus niet vullen " + ex);
    }
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.DefaultSettingsProvider.java

@Override
public void save(Settings settings) {
    synchronized (getClass()) {
        File file = getSettingsFile();
        FileOutputStream os = null;
        try {/* w  ww . ja v a 2 s . com*/
            os = new FileOutputStream(file, false);
            XMLEncoder encoder = new XMLEncoder(os);
            encoder.writeObject(settings);
            encoder.close();
            this.settings = settings;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            IOUtils.closeQuietly(os);
        }
    }
}

From source file:com.jaspersoft.jasperserver.export.modules.repository.OlapUnitViewOptionsDataProvider.java

public InputStream getData(ExporterModuleContext exportContext, Resource resource) {

    OlapUnit unit = (OlapUnit) resource;
    InputStream dataStream;/*from   w  w w .  j  a va  2  s  .c  om*/
    Object viewOptions = unit.getOlapViewOptions();
    if (viewOptions == null) {
        dataStream = null;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(baos));
        e.writeObject(unit.getOlapViewOptions());
        e.flush();
        e.close();
        dataStream = new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray()));
    }
    return dataStream;
}

From source file:Roster.Rosters.java

private JSONObject toXML() {
    String xml = null;/* www.  jav  a  2s.  com*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(out);
        encoder.writeObject(students); // serialize to XML
        encoder.close();
        xml = out.toString(); // stringify
    } catch (Exception e) {
    }
    //System.out.println(xml.trim());

    JSONObject jobt = XML.toJSONObject(xml);
    //System.out.println(jobt);

    return jobt;
}

From source file:com.npower.dl.FirmwareMimeTypeHelper.java

public static void save(File file) throws IOException {
    FileOutputStream out = new FileOutputStream(file);
    XMLEncoder xmlEncoder = new XMLEncoder(out);
    xmlEncoder.writeObject(mimeTypes);/*from   ww  w.  j a  v a 2 s .  co m*/
    xmlEncoder.flush();
    xmlEncoder.close();
    out.close();
}

From source file:PersistentFrameTest.java

 public void save()
{
   if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
   {/*from ww w  . j a v a2  s  .c  o m*/
      try
      {
         File file = chooser.getSelectedFile();
         XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
         encoder.writeObject(frame);
         encoder.close();
      }
      catch (IOException e)
      {
         JOptionPane.showMessageDialog(null, e);
      }
   }
}

From source file:org.sakaiproject.tool.gradebook.GradebookArchive.java

/**
 * Serializes this gradebook archive into an xml document
 *///from w  w  w  .  j  av a 2s.  c  o  m
public String archive() {
    if (log.isDebugEnabled())
        log.debug("GradebookArchive.archive() called");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(baos));
    encoder.writeObject(this);
    encoder.flush();
    String xml = baos.toString();
    if (log.isDebugEnabled())
        log.debug("GradebookArchive.archive() finished");
    return xml;
}