package com.c2kernel.utils; //Java import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.util.HashSet; import java.util.StringTokenizer; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.ValidationException; import org.exolab.castor.xml.XMLContext; import com.c2kernel.common.InvalidDataException; import com.c2kernel.persistency.outcome.Outcome; /************************************************************************** * Loads all mapfiles, and wraps marshalling/unmarshalling * * @author $Author: abranson $ $Date: 2004/10/20 14:10:21 $ * @version $Revision: 1.12 $ **************************************************************************/ public class CastorXMLUtility { private XMLContext mappingContext; /** * Looks for a file called 'index.xml' at the given URL, and loads every file * listed in there by relative path * * @param mapURL - map root */ public CastorXMLUtility(URL mapURL) throws InvalidDataException { // load index Logger.msg(3, "Loading maps from "+mapURL); String index; try { index = FileStringUtility.url2String( new URL(mapURL, "index") ); } catch (Exception e) { Logger.warning("Could not load map index from "+mapURL.toString()); return; } StringTokenizer sTokenizer = new StringTokenizer(index); Mapping thisMapping = new Mapping(); HashSet loadedMapURLs = new HashSet(); try { while( sTokenizer.hasMoreTokens() ) { String thisMap = sTokenizer.nextToken(); URL thisMapURL = new URL(mapURL, thisMap); if( !loadedMapURLs.contains(thisMapURL) ) { Logger.msg(7, "Adding mapping file:"+thisMapURL); thisMapping.loadMapping( thisMapURL ); loadedMapURLs.add( thisMapURL ); } else { Logger.msg("Map file already loaded:"+thisMapURL); } } mappingContext = new XMLContext(); mappingContext.addMapping(thisMapping); } catch (MappingException ex) { Logger.error(ex); throw new InvalidDataException("XML Mapping files are not valid: "+ex.getMessage()); } catch (MalformedURLException ex) { Logger.error(ex); throw new InvalidDataException("Mapping file location invalid: "+ex.getMessage()); } catch (IOException ex) { Logger.error(ex); throw new InvalidDataException("Could not read XML mapping files: "+ex.getMessage()); } Logger.msg(1, "Loaded all maps from "+mapURL.toString()); } /************************************************************************** * Marshalls a mapped object to string. The mapping must be loaded before. * See updateMapping(). **************************************************************************/ public String marshall( Object obj ) throws IOException, MappingException, MarshalException, ValidationException { if (obj == null) return ""; if (obj instanceof Outcome) return ((Outcome)obj).getData(); StringWriter sWriter = new StringWriter(); Marshaller marshaller = mappingContext.createMarshaller(); marshaller.setWriter(sWriter); marshaller.setMarshalAsDocument( false ); marshaller.marshal( obj ); return sWriter.toString(); } /************************************************************************** * Unmarshalls a mapped object from string. The mapping must be loaded before. * See updateMapping(). **************************************************************************/ public Object unmarshall( String data ) throws IOException, MappingException, MarshalException, ValidationException { if (data.equals("")) return null; StringReader sReader = new StringReader( data ); Unmarshaller unmarshaller = mappingContext.createUnmarshaller(); return unmarshaller.unmarshal( sReader ); } }