コード例 #1
0
  public Object deserialize(InputStream is, Hashtable extendedContext) throws Exception {

    // getting deserializer
    Class targetCls = (Class) extendedContext.get("targetClass");
    Object objectOfTargetCls = targetCls.newInstance();
    TypeDesc desc =
        (TypeDesc)
            objectOfTargetCls
                .getClass()
                .getMethod("getTypeDesc", new Class[] {})
                .invoke(objectOfTargetCls, new Object[] {});
    final QName xmlType; // = desc.getXmlType();
    xmlType =
        new QName(
            "http://" + objectOfTargetCls.getClass().getName(),
            org.uengine.util.UEngineUtil.getClassNameOnly(objectOfTargetCls.getClass()));
    Deserializer dser =
        (Deserializer)
            objectOfTargetCls
                .getClass()
                .getMethod("getDeserializer", new Class[] {String.class, Class.class, QName.class})
                .invoke(
                    objectOfTargetCls, new Object[] {"", objectOfTargetCls.getClass(), xmlType});
    // end
    System.out.println("dser:" + dser);

    DeserializationContext context =
        new DeserializationContextImpl(
            new org.xml.sax.InputSource(is),
            /*			new MessageContext(null){
            	public String getEncodingStyle(){
            		return xmlType.getNamespaceURI();
            	}
            },*/
            new MessageContext(new AxisClient()),
            // Message.RESPONSE
            "PurchaseOrder");

    boolean oldVal = context.isDoneParsing();
    ((DeserializationContextImpl) context).deserializing(true);
    context.pushElementHandler(new EnvelopeHandler((SOAPHandler) dser));

    //	        context.getRecorder().replay(0, -1, (org.xml.sax.ContentHandler)context);
    context.getRecorder().replay(0, -1, (org.xml.sax.ContentHandler) context);

    ((DeserializationContextImpl) context).deserializing(oldVal);

    context.parse();

    return dser.getValue();
  }
コード例 #2
0
  public void serialize(Object sourceObj, OutputStream os, Hashtable extendedContext)
      throws Exception {

    try {
      final ClassLoader urlClassLoader = GlobalContext.getComponentClassLoader();
      /* this will replace the default system class loader with the new custom classloader, so that XMLEncoder will use the new custom classloader to lookup a class */
      Thread.currentThread().setContextClassLoader(urlClassLoader);
    } catch (Exception e) {
      System.out.println("can't replace classloader");
    }

    try {
      TypeDesc desc =
          (TypeDesc)
              sourceObj
                  .getClass()
                  .getMethod("getTypeDesc", new Class[] {})
                  .invoke(sourceObj, new Object[] {});
      QName xmlType = desc.getXmlType();
      //
      // xmlType = (QName)extendedContext.get("qName");
      //
      xmlType =
          new QName(
              "http://" + sourceObj.getClass().getName(),
              org.uengine.util.UEngineUtil.getClassNameOnly(sourceObj.getClass()));
      //			xmlType = new QName(Constants.URI_SOAP11_ENV, Constants.ELEM_ENVELOPE );

      org.apache.axis.encoding.Serializer ser =
          (org.apache.axis.encoding.Serializer)
              sourceObj
                  .getClass()
                  .getMethod("getSerializer", new Class[] {String.class, Class.class, QName.class})
                  .invoke(sourceObj, new Object[] {"", sourceObj.getClass(), xmlType});

      // Writer w = new OutputStreamWriter(os);
      StringWriter w = new StringWriter();
      SerializationContext context =
          new SerializationContextImpl(w) {
            public MessageContext getMessageContext() {
              return new MessageContext(null) {
                public String getEncodingStyle() {
                  return "";
                }
              };
            }
          };
      ser.serialize(xmlType, (org.xml.sax.Attributes) null, sourceObj, context);

      // System.out.println(w);
      // w.write("SdfasdfsadfA");

      BufferedOutputStream bao = new BufferedOutputStream(os);

      bao.write(w.toString().getBytes());

      bao.flush();
      bao.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }