/**
   * @param writer
   * @param includeHistory
   * @param session
   * @throws DataAccessException
   * @throws HibernateException
   */
  private void writeObjects(
      final Writer writer,
      final boolean includeHistory,
      final Session session,
      final boolean preserveIds)
      throws DataAccessException, HibernateException {
    // Container für die Objekte
    final List<Object> all = new ArrayList<Object>();
    final XStream stream = initXStream(session, true);
    final XStream defaultXStream = initXStream(session, false);

    session.flush();
    // Alles laden
    List<?> list = session.createQuery("select o from java.lang.Object o").setReadOnly(true).list();
    list = (List<?>) CollectionUtils.select(list, PredicateUtils.uniquePredicate());
    final int size = list.size();
    log.info("Writing " + size + " objects");
    for (final Iterator<?> it = list.iterator(); it.hasNext(); ) {
      final Object obj = it.next();
      if (log.isDebugEnabled()) {
        log.debug("loaded object " + obj);
      }
      if ((obj instanceof HistoryEntry || obj instanceof PropertyDelta)
          && includeHistory == false) {
        continue;
      }
      Hibernate.initialize(obj);
      final Class<?> targetClass = HibernateProxyHelper.getClassWithoutInitializingProxy(obj);
      final ClassMetadata classMetadata = session.getSessionFactory().getClassMetadata(targetClass);
      if (classMetadata == null) {
        log.fatal("Can't init " + obj + " of type " + targetClass);
        continue;
      }
      // initalisierung des Objekts...
      defaultXStream.marshal(obj, new CompactWriter(new NullWriter()));

      if (preserveIds == false) {
        // Nun kann die ID gelöscht werden
        classMetadata.setIdentifier(obj, null, EntityMode.POJO);
      }
      if (log.isDebugEnabled()) {
        log.debug("loading evicted object " + obj);
      }
      if (this.ignoreFromTopLevelListing.contains(targetClass) == false) {
        all.add(obj);
      }
    }
    // und schreiben
    try {
      writer.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
    } catch (final IOException ex) {
      // ignore, will fail on stream.marshal()
    }
    log.info("Wrote " + all.size() + " objects");
    final MarshallingStrategy marshallingStrategy = new ProxyIdRefMarshallingStrategy();
    stream.setMarshallingStrategy(marshallingStrategy);
    stream.marshal(all, new PrettyPrintWriter(writer));
  }
Esempio n. 2
0
  public static void marshal(APIRequest request, Object responseRoot, Writer writer)
      throws FatalException, IOException {

    // get the javascript callback, if present
    String jsCallback = null;
    if (request != null) {
      try {
        jsCallback = request.getParameter(Constants.Request.JSCALLBACK, false);
      } catch (InvalidParameterException e) {
        // it's okay to not have one
      }
    }

    // get an xstream using a json dirver
    XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
    xstream.setMode(XStream.NO_REFERENCES);

    // alias top-level types
    xstream.alias("response", ResponseType.class);

    // first write out the jscallback method name
    if (!StringUtil.isNullOrEmpty(jsCallback)) {
      writer.write(jsCallback + "(");
    }

    // write the json to our writer
    // NOTE: all element names will be camel case and not cased as in JAXB
    xstream.marshal(responseRoot, new JsonWriter(writer));

    // close off the method if jscallback was given
    if (!StringUtil.isNullOrEmpty(jsCallback)) {
      writer.write(")");
    }
  }
  @Override
  public void encode(Object object, ContentHandler handler) throws Exception {
    // prepare xml encoding
    XStream xstream = buildXStream();

    // bind with the content handler
    SaxWriter writer = new SaxWriter(nameCoder);
    writer.setContentHandler(handler);

    // write out xml
    xstream.marshal(object, writer);
  }
  protected String marshallAdvisory(final DataStructure ds, String transformation) {

    StringWriter buffer = new StringWriter();
    HierarchicalStreamWriter out;
    if (transformation.toLowerCase(Locale.ENGLISH).endsWith("json")) {
      out = new JettisonMappedXmlDriver().createWriter(buffer);
    } else {
      out = new PrettyPrintWriter(buffer);
    }

    XStream xstream = getXStream();
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.aliasPackage("", "org.apache.activemq.command");
    xstream.marshal(ds, out);
    return buffer.toString();
  }
 /**
  * Converts FIX message to FIXML.
  *
  * @param message - FIX message to convert
  * @return XML representation of FIX message as String
  */
 public String toFIXML(Message message) {
   if (!initialized) {
     initialize();
   }
   StringWriter sw = new StringWriter();
   try {
     if (pretty) {
       xstream.toXML(new FIXMLRoot(message), sw);
     } else {
       xstream.marshal(new FIXMLRoot(message), new CompactWriter(sw));
     }
     return sw.toString();
   } finally {
     try {
       sw.close();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 }
 public String writeXml(Object output) {
   StringWriter sw = new StringWriter();
   xstream.marshal(output, new CompactWriter(sw));
   return sw.toString();
 }