Example #1
0
 /**
  * 序列化。如果不生成文件,则直接返回生成的字符串。
  *
  * @param object 被序列化的对象
  * @param docWriter 文档输出器
  * @return 字符串
  */
 private static String serializing(Object object, DocWriter docWriter) {
   // 准备工作
   XBConfig xbConfig = XBConfigHelper.getXbConfig();
   docWriter.setXBConfig(xbConfig);
   PathTracker pathTracker = new PathTracker(xbConfig.isUseRelativePath());
   DocWriter realDocWriter = new PathTrackingWriter(docWriter, pathTracker);
   TransferInfo transferInfo = new TransferInfo(pathTracker, xbConfig, realDocWriter, null, null);
   try {
     // 开始序列化
     realDocWriter.writeStartDocument();
     Serializer.writeUnknow(object, transferInfo, null);
     realDocWriter.writeEndDocument();
   } catch (Exception e) {
     throw new RuntimeException("序列化失败。", e);
   } finally {
     if (null != realDocWriter) {
       try {
         realDocWriter.close();
       } catch (Exception e) {
         throw new RuntimeException("关闭输出流失败。", e);
       }
       if (null != realDocWriter.getWriter()) {
         try {
           realDocWriter.getWriter().close();
         } catch (Exception e) {
           throw new RuntimeException("关闭输出流失败。", e);
         }
       }
     }
   }
   return docWriter.getString();
 }
Example #2
0
 /**
  * 反序列化。
  *
  * @param DocReader 文档读取器
  * @param clz 参考类
  * @param object 参考对象
  * @return 对象
  */
 private static Object deserializing(DocReader docReader, Class<?> clz, Object object) {
   // 准备工作
   XBConfig xbConfig = XBConfigHelper.getXbConfig();
   PathTracker pathTracker = new PathTracker(xbConfig.isUseRelativePath());
   DocReader realDocReader = new PathTrackingReader(docReader, pathTracker);
   ObjectOperator objectOperator = ObjectOperatorFactory.createObjectOperator();
   TransferInfo transferInfo =
       new TransferInfo(pathTracker, xbConfig, null, realDocReader, objectOperator);
   Object result = null;
   try {
     realDocReader.moveDown();
     result = Deserializer.readUnknow(clz, object, null, transferInfo);
     // 处理引用失败的例子
     handleUnfin(transferInfo);
   } catch (Exception e) {
     throw new RuntimeException("反序列化失败。", e);
   } finally {
     if (null != realDocReader) {
       try {
         realDocReader.close();
       } catch (Exception e) {
         throw new RuntimeException("关闭输入流失败。", e);
       }
       if (null != realDocReader.getReader()) {
         try {
           realDocReader.getReader().close();
         } catch (Exception e) {
           throw new RuntimeException("关闭输入流失败。", e);
         }
       }
     }
   }
   return result;
 }