⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 castorhandler.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.jdon.util;

import java.io.*;
import java.util.*;
import java.net.*;

import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;
import org.xml.sax.InputSource;

import org.exolab.castor.xml.Marshaller;

import com.jdon.util.Debug;

import com.jdon.util.SimpleCachePool;
import com.jdon.util.PropsUtil;

/**
 *
 * <p>Title: </p>
 * <p>Description:
 * CastorHanlder操作类
 * 需要映射文件,映射文件名可以自己任意取名。
 * 映射文件可以和EJB的ejb-jar.xml一起,放在META-INF/目录下
 * 也可以包含在EJB的jar中。
 *
 * </p>
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public class CastorHandler {

  public final static String module = CastorHandler.class.getName();

  private static PropsUtil propsUtil = PropsUtil.getInstance();

  /**
   * 获得对象和XML之间映射关系
   */
  public static Mapping getMapping(String mappingFile) {

    Mapping mapping = (Mapping) SimpleCachePool.get(mappingFile);

    if (mapping == null) {
      try {

        mapping = new Mapping();
        String pathmappingFile = propsUtil.getConfFile(mappingFile);
        InputStream mappingIn = null;

        if (pathmappingFile == null) {
          Debug.logVerbose("get mapping from stream " + mappingFile, module);
          mappingIn = propsUtil.getConfStream(mappingFile);
          mapping.loadMapping(new InputSource(mappingIn));
        } else
          mapping.loadMapping(pathmappingFile);

        SimpleCachePool.put(mappingFile, mapping);
      } catch (Exception e) {
        Debug.logError("get mapping: " + mappingFile + " error " + e, module);
      }
    }
    return mapping;
  }

  /**
   * 获得反序列化的对象
   */
  private static Unmarshaller getUnmarshaller(String mappingFile,
                                              String className) throws
      Exception {

    Unmarshaller un = (Unmarshaller) SimpleCachePool.get(className);
    if (un == null) {
      try {
        Class c = Class.forName(className);
        un = new Unmarshaller(c);
        un.setMapping(getMapping(mappingFile));
        SimpleCachePool.put(className, un);
      } catch (Exception e) {
        Debug.logError(" getUnmarshaller error:  " + className + " " + e,
                       module);
        throw new Exception(e);
      }
    }

    return un;

  }

  /**
   * 反序列化 读取对象从Reader中
   * @param mappingFile
   * @param className
   * @param in
   * @return
   */
  private static Object readImp(String mappingFile, String className, Reader in) throws
      Exception {
    Object object = null;
    try {
      Unmarshaller un = getUnmarshaller(mappingFile, className);
      object = un.unmarshal(in);
      in.close();
    } catch (Exception e) {
      Debug.logError(" read " + className + e, module);
      throw new Exception(e);
    }
    return object;

  }

  /**
   * 对象序列化 将对象写入Writer中
   * @param mappingFile
   * @param object
   * @param out
   * @throws java.lang.Exception
   */
  public static void writeImp(String mappingFile, Object object, Writer out) throws
      Exception {
    Debug.logVerbose("", module);
    try {
      Marshaller ms = new Marshaller(out);
      ms.setMapping(getMapping(mappingFile));
      ms.setEncoding(propsUtil.ENCODING);
      ms.marshal(object);
      out.close();
    } catch (Exception e) {
      Debug.logError("write object to file :" + e, module);
      throw new Exception(e);
    }

  }

  /**
   * 从XML文件中读取对象
   */
  public static Object read(String mappingFile, String className,
                            String xmlfile) throws Exception {
    FileReader in = new FileReader(xmlfile);
    return readImp(mappingFile, className, in);

  }

  public static Object readFromString(String mappingFile, String className,
                                      String s) throws Exception {
    Reader in = new StringReader(s);
    return readImp(mappingFile, className, in);
  }

  /**
   * 将对象序列化到XML文件中
   */
  public static void write(String mappingFile, Object object, String outfile) throws
      Exception {

    FileWriter out = new FileWriter(outfile);
    writeImp(mappingFile, object, out);
  }

  /**
   * 将对象序列化到字符串文本中
   */
  public static String writeToString(String mappingFile, Object object) throws
      Exception {
    String s = null;
    Writer out = new StringWriter();
    writeImp(mappingFile, object, out);
    if (out != null)
      s = out.toString();
    return s;

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -