ProcessDataInfo sig = getPaProcessDef().contextSignature ();
for (Iterator i = newValues.keySet().iterator(); i.hasNext();) {
String name = (String)i.next();
Object type = sig.get (name);
if (type == null) {
throw new InvalidDataException ("No such data field: " + name);
}
Object v = newValues.get(name);
if (v == null) {
continue;
}
if ((type instanceof ExternalReference)
&& XPDLUtil.isJavaType((ExternalReference)type)) {
Class vc = null;
try {
vc = XPDLUtil.getJavaType((ExternalReference)type);
} catch (ClassNotFoundException e) {
throw (InvalidDataException)
(new InvalidDataException
("Required Java class no longer available: "
+ e.getMessage())).initCause(e);
}
if (vc.isAssignableFrom(v.getClass())) {
continue;
} else {
throw new InvalidDataException
("Context entry " + name + " is "
+ v.getClass().getName() + " must be instance of "
+ vc.getName());
}
}
if ((type instanceof SAXEventBuffer)
|| (type instanceof ExternalReference)
|| type.equals(org.w3c.dom.Element.class)) {
boolean elemList = false;
if (v instanceof List) {
if (((List)v).size() == 0) {
elemList = true;
} else {
Iterator ti = ((List)v).iterator ();
if (ti.next() instanceof Element) {
elemList = true;
}
}
}
if (! ((v instanceof Element)
|| (v instanceof Document)
|| elemList
|| (v instanceof org.w3c.dom.Element)
|| (v instanceof org.w3c.dom.DocumentFragment)
|| (v instanceof org.w3c.dom.Document)
|| (v instanceof SAXEventBuffer))) {
throw new InvalidDataException
("Not a usable XML representation: " + name);
}
continue;
}
if (type instanceof Class) {
Class vc = v.getClass();
if (v instanceof Float) {
vc = Double.class;
} else if (v instanceof Integer) {
vc = Long.class;
}
if (type == Participant.class) {
type = String.class;
}
if (! ((Class)type).isAssignableFrom (vc)) {
throw new InvalidDataException
("Values for data field \"" + name
+ "\" must be of type " + ((Class)type).getName ()
+ " (is " + v.getClass().getName () + ")");
}
continue;
}
throw new InvalidDataException
("Invalid type for data field \"" + name
+ "\": " + ((Class)type).getName ());
}
// now do changes
DOMBuilder builder = null;
ProcessData oldValues = new DefaultProcessData();
for (Iterator i = (new ArrayList (newValues.keySet())).iterator();
i.hasNext();) {
String name = (String)i.next();
oldValues.put(name, getPaProcessData().get(name));
Object v = newValues.get(name);
if (logger.isDebugEnabled ()) {
logger.debug ("Setting context data item " + name + " = " + v);
}
Object type = sig.get (name);
if ((type instanceof ExternalReference)
&& XPDLUtil.isJavaType((ExternalReference)type)) {
// accept literally
} else if (v instanceof Float) {
v = new Double (((Float)v).doubleValue ());
newValues.put (name, v);
} else if (v instanceof Integer) {
v = new Long (((Integer)v).longValue ());
newValues.put (name, v);
} else if ((v instanceof Element)
|| (v instanceof Document)
|| (v instanceof List)) {
try {
if (logger.isDebugEnabled ()) {
logger.debug
("Convering item " + name + " from JDOM to SAX");
}
SAXOutputter outputter = new SAXOutputter();
SAXEventBufferImpl b = new SAXEventBufferImpl ();
outputter.setContentHandler(b);
outputter.setLexicalHandler(b);
if (v instanceof Document) {
outputter.output ((Document)v);
} else {
List l;
if (v instanceof List) {
l = (List)v;
} else {
l = new ArrayList ();
l.add (v);
}
outputter.output (l);
}
b.pack();
v = b;
newValues.put (name, v);
} catch (JDOMException e) {
logger.error (e.getMessage (), e);
throw new InvalidDataException (e.getMessage ());
}
} else if ((v instanceof org.w3c.dom.Element)
|| (v instanceof org.w3c.dom.DocumentFragment)
|| (v instanceof org.w3c.dom.Document)) {
try {
if (logger.isDebugEnabled ()) {
logger.debug
("Convering item " + name + " from W3C DOM to SAX");
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer ();
SAXEventBufferImpl b = new SAXEventBufferImpl ();
// There seems to be a bug in Xalan that causes it to
// fire two endDocument events when transforming a
// DocumentFragment, filter it out.
t.transform (new DOMSource ((org.w3c.dom.Node)v),
new SAXResult(new DupEndFilter(b)));
b.pack();
v = b;
newValues.put (name, v);
} catch (TransformerConfigurationException e) {
String s = "Error converting DOM to SAX: "+e.getMessage ();
logger.error (s, e);
throw new IllegalStateException (s);
} catch (TransformerException e) {
String s = "Error converting DOM to SAX: "+e.getMessage ();
logger.error (s, e);
throw new InvalidDataException (s);
}
}
getPaProcessData().put(name, v);
}