View Javadoc
1 //============================================================================== 2 // file : JibxUtil.java 3 // project: Java Common Utility 4 // 5 // last change: date: $Date: 2003/09/10 09:22:14 $ 6 // by: $Author: bitiboy $ 7 // revision: $Revision: 1.1 $ 8 //------------------------------------------------------------------------------ 9 // copyright: GNU GPL Software License (see class documentation) 10 //============================================================================== 11 package com.justhis.jibx; 12 13 14 /* 15 * $Id: JibxUtil.java,v 1.1 2003/09/10 09:22:14 bitiboy Exp $ 16 * 17 * Copyright 2003 Acai Software All Rights Reserved. 18 * 19 * This file JibxUtil.java is part of the Java Common Utility. 20 21 * The Java Common Utility is free software; you can redistribute it and/or modify 22 * it under the terms of the GNU General Public License as published by 23 * the Free Software Foundation; either version 2 of the License, or 24 * (at your option) any later version. 25 26 * Java Common Utility is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 31 * You should have received a copy of the GNU General Public License 32 * along with the Java Common Utility; if not, write to the Free Software 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34 35 * http://www.justhis.com http://ejb.cn 36 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com 37 */ 38 import org.jibx.runtime.BindingDirectory; 39 import org.jibx.runtime.IBindingFactory; 40 import org.jibx.runtime.IMarshallingContext; 41 import org.jibx.runtime.IUnmarshallingContext; 42 import org.jibx.runtime.JiBXException; 43 44 import java.io.FileInputStream; 45 import java.io.FileNotFoundException; 46 import java.io.FileOutputStream; 47 import java.io.InputStream; 48 import java.io.OutputStream; 49 50 51 /*** 52 * TODO 53 * 54 * @author <a href="http://blog.ejb.cn">acai</a> 55 * @version $Revision: 1.1 $ 56 */ 57 public class JibxUtil { 58 //~ Constructors ----------------------------------------------------------- 59 60 /*** 61 * Creates a new JibxUtil object. 62 */ 63 public JibxUtil() { 64 super(); 65 } 66 67 //~ Methods ---------------------------------------------------------------- 68 69 /*** 70 * marshall document by a jibx object enhanced. 71 * 72 * @param obj a jibx object 73 * @param xslt xslt file name 74 * @param indent tidy document indent 75 * @param encoding document's encoding 76 * @param out stream of print document 77 * 78 * @throws FileNotFoundException if file not found. 79 * @throws JiBXException if use jibx to mashall document. 80 */ 81 public static void marshallDocument(Object obj, String xslt, int indent, 82 String encoding, OutputStream out 83 ) 84 throws FileNotFoundException, JiBXException { 85 IBindingFactory bfact = BindingDirectory.getFactory(obj.getClass()); 86 IMarshallingContext mctx = bfact.createMarshallingContext(); 87 88 if (xslt != null) { 89 mctx.setXSLT(xslt); 90 } 91 92 mctx.setIndent(indent); 93 mctx.marshalDocument(obj, encoding, null, out); 94 } 95 96 /*** 97 * marshall jibx object to print xml document. 98 * 99 * @param obj marshalled object 100 * @param xslt xslt file name 101 * @param out a stream of print xml doucment. 102 * 103 * @throws FileNotFoundException if file not found 104 * @throws JiBXException if deal with jibx object ocurr any error. 105 */ 106 public static void marshallDocument(Object obj, String xslt, 107 OutputStream out 108 ) 109 throws FileNotFoundException, JiBXException { 110 marshallDocument(obj, xslt, 4, "UTF-8", out); 111 } 112 113 /*** 114 * marshall jibx object to print xml document file. 115 * 116 * @param obj jibx object 117 * @param xslt xslt file name or path 118 * @param fileName a file printed xml document 119 * 120 * @throws FileNotFoundException if can't find any file 121 * @throws JiBXException if any jibx error occurs 122 */ 123 public static void marshallDocument(Object obj, String xslt, String fileName) 124 throws FileNotFoundException, JiBXException { 125 marshallDocument(obj, xslt, 4, "UTF-8", new FileOutputStream(fileName)); 126 } 127 128 /*** 129 * get jibx object from inputstream 130 * 131 * @param cs class name 132 * @param ins input stream 133 * 134 * @return a jibx object 135 * 136 * @throws JiBXException if any jibx error occurs 137 */ 138 public static Object unmarshallDocument(Class cs, InputStream ins) 139 throws JiBXException { 140 IBindingFactory bfact = BindingDirectory.getFactory(cs); 141 IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); 142 143 return uctx.unmarshalDocument(ins, null); 144 } 145 146 /*** 147 * get object from xml file 148 * 149 * @param cs class name 150 * @param fileName xml file name 151 * 152 * @return jibx object 153 * 154 * @throws JiBXException if any jibx exception occurs 155 */ 156 public static Object unmarshallDocument(Class cs, String fileName) 157 throws JiBXException { 158 try { 159 return unmarshallDocument(cs, new FileInputStream(fileName)); 160 } catch (FileNotFoundException e) { 161 throw new JiBXException(e.getMessage(), e.getCause()); 162 } 163 } 164 } 165 166 167 /* 168 * $Log: JibxUtil.java,v $ 169 * Revision 1.1 2003/09/10 09:22:14 bitiboy 170 * *** empty log message *** 171 * 172 * 173 */

This page was automatically generated by Maven