From 74d433dc0083324e7e2774148f3d92f7644e1e64 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 10 Jul 2013 10:50:15 +0200 Subject: CastorXMLUtility switched to recommended XMLContext. Should be faster and may help with IssueID #132 --- .../java/com/c2kernel/utils/CastorXMLUtility.java | 86 +++++++++------------- 1 file changed, 36 insertions(+), 50 deletions(-) (limited to 'src/main/java/com/c2kernel/utils/CastorXMLUtility.java') diff --git a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java index 6a9350f..98ebbda 100644 --- a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java +++ b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java @@ -4,6 +4,7 @@ package com.c2kernel.utils; 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; @@ -14,6 +15,7 @@ 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; @@ -27,8 +29,7 @@ import com.c2kernel.persistency.outcome.Outcome; **************************************************************************/ public class CastorXMLUtility { - private final Mapping mMapping = new Mapping(); - private final HashSet mMappingKeys = new HashSet(); + private XMLContext mappingContext; /** * Looks for a file called 'index.xml' at the given URL, and loads every file @@ -48,53 +49,38 @@ public class CastorXMLUtility } StringTokenizer sTokenizer = new StringTokenizer(index); - while( sTokenizer.hasMoreTokens() ) { - String thisMap = sTokenizer.nextToken(); - try { - addMapping( new URL(mapURL, thisMap)); - } catch (Exception e) { - Logger.error(e); - throw new InvalidDataException("Error loading map '"+thisMap+"'", ""); - } - } - - // Test the maps now, instead of on first use. + 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()); + } - try { - new Unmarshaller( mMapping ); - } catch (MappingException e) { - Logger.error(e); - throw new InvalidDataException("Castor mapfiles are not valid", ""); - } Logger.msg(1, "Loaded all maps from "+mapURL.toString()); } - - /************************************************************************** - * Updates a mapping referenced by the mapID. - * The same mapping cannot be loaded many times as it generates an exception. - * That is the reason for this method as it maintains the HashSet of MappingKeys. - **************************************************************************/ - private void addMapping( URL mapID ) - throws IOException, - MappingException, - MarshalException, - ValidationException - { - - if( !mMappingKeys.contains(mapID) ) - { - Logger.msg(7, "Added mapping file:"+mapID); - - mMapping.loadMapping( mapID ); - mMappingKeys.add( mapID ); - } - else - { - Logger.msg(1, "Map file already loaded:"+mapID); - } - } - /************************************************************************** * Marshalls a mapped object to string. The mapping must be loaded before. * See updateMapping(). @@ -109,9 +95,9 @@ public class CastorXMLUtility if (obj instanceof Outcome) return ((Outcome)obj).getData(); StringWriter sWriter = new StringWriter(); - Marshaller marshaller = new Marshaller( sWriter ); - - marshaller.setMapping( mMapping ); + Marshaller marshaller = mappingContext.createMarshaller(); + + marshaller.setWriter(sWriter); marshaller.setMarshalAsDocument( false ); marshaller.marshal( obj ); @@ -128,9 +114,9 @@ public class CastorXMLUtility MarshalException, ValidationException { - StringReader sReader = new StringReader( data ); - Unmarshaller unmarshaller = new Unmarshaller( mMapping ); if (data.equals("")) return null; + StringReader sReader = new StringReader( data ); + Unmarshaller unmarshaller = mappingContext.createUnmarshaller(); return unmarshaller.unmarshal( sReader ); } } -- cgit v1.2.3