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

📄 castorhandler.java

📁 chatper4JdonCMS.rar
💻 JAVA
字号:
package com.jdon.cms.xml;


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

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

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: </p>
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author banq
 * @version 1.0
 */

public class CastorHandler {

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

  private static final String MAPFILE = "mapping.xml";
   private static PropsUtil propsUtil = PropsUtil.getInstance();

  /**
   * Load a mapping file
   */
  public static Mapping getMapping(){

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

      if (mapping == null){
       String pathMappingFile = propsUtil.getConfFile(MAPFILE);

        try {
          mapping = new Mapping();
          mapping.loadMapping(pathMappingFile);
          SimpleCachePool.put(MAPFILE, mapping);
        }catch (Exception e) {
          Debug.logError("get mapping error " + e, module);
        }
      }

      return mapping;

  }

  private static Unmarshaller getUnmarshaller(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());
        SimpleCachePool.put(className, un);

      }catch (Exception e) {
        Debug.logError(" getUnmarshaller error:  " + className + " " + e, module);
        throw new Exception(e);
      }
    }

    return un;

  }

  /**
   * read a object from xmlFile
   * @param classname
   * @param xmlfile
   * @return
   * @throws Exception
   */
  public static Object read(String className, String xmlfile) throws Exception{
     Object  object = null;
     try{
       Unmarshaller un = getUnmarshaller(className);

       FileReader in = new FileReader(xmlfile);
       object =  un.unmarshal(in);
       in.close();
     }catch(Exception e){
       Debug.logError(" read " + className + " form file:" + xmlfile + e, module);
       throw new Exception(e);
     }
     return object;

   }


   /**
    * write a object to xmlFile
    * @param outfile
    * @param object
    * @throws Exception
    */
   public static void write(Object object , String outfile) throws Exception{
      try{

        FileWriter out = new FileWriter(outfile);

        Marshaller ms = new Marshaller(out);
        ms.setMapping(getMapping());
        ms.setEncoding(propsUtil.ENCODING);

        ms.marshal(object);

        out.close();

      }catch(Exception e){
        Debug.logError("write object to file :" + outfile + " error! " + e, module);
        throw new Exception(e);
      }


    }


}

⌨️ 快捷键说明

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