1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package com.c2kernel.utils;
//Java
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
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 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 final Mapping mMapping = new Mapping();
private final HashSet<URL> mMappingKeys = new HashSet<URL>();
/**
* 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);
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.
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().
**************************************************************************/
public String marshall( Object obj )
throws IOException,
MappingException,
MarshalException,
ValidationException
{
if (obj == null) return "<NULL/>";
if (obj instanceof Outcome)
return ((Outcome)obj).getData();
StringWriter sWriter = new StringWriter();
Marshaller marshaller = new Marshaller( sWriter );
marshaller.setMapping( mMapping );
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
{
StringReader sReader = new StringReader( data );
Unmarshaller unmarshaller = new Unmarshaller( mMapping );
if (data.equals("<NULL/>")) return null;
return unmarshaller.unmarshal( sReader );
}
}
|