summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/src')
-rw-r--r--java/src/Freeze/EvictorI.java239
-rw-r--r--java/src/Freeze/Map.java48
-rw-r--r--java/src/Ice/Object.java8
-rw-r--r--java/src/Ice/ObjectImpl.java104
-rw-r--r--java/src/Ice/UserException.java14
-rw-r--r--java/src/IceInternal/BasicStream.java12
-rw-r--r--java/src/IceXML/StreamI.java1616
7 files changed, 186 insertions, 1855 deletions
diff --git a/java/src/Freeze/EvictorI.java b/java/src/Freeze/EvictorI.java
index 95794af1a96..45e72977870 100644
--- a/java/src/Freeze/EvictorI.java
+++ b/java/src/Freeze/EvictorI.java
@@ -16,8 +16,8 @@ package Freeze;
class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
{
- public EvictorI(Ice.Communicator communicator, String envName,
- String dbName, boolean createDb)
+ public
+ EvictorI(Ice.Communicator communicator, String envName, String dbName, boolean createDb)
{
_communicator = communicator;
_dbEnvHolder = SharedDbEnv.get(communicator, envName);
@@ -26,8 +26,8 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
init(envName, dbName, createDb);
}
- public EvictorI(Ice.Communicator communicator, com.sleepycat.db.DbEnv dbEnv,
- String dbName, boolean createDb)
+ public
+ EvictorI(Ice.Communicator communicator, com.sleepycat.db.DbEnv dbEnv, String dbName, boolean createDb)
{
_communicator = communicator;
_dbEnvHolder = null;
@@ -151,7 +151,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
while(p.hasNext())
{
java.util.Map.Entry entry = (java.util.Map.Entry) p.next();
-
destroyFacetImpl((Facet) entry.getValue());
}
}
@@ -351,13 +350,10 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
//
// Destroy all existing facets
//
-
java.util.Iterator p = element.facets.entrySet().iterator();
-
while(p.hasNext())
{
java.util.Map.Entry entry = (java.util.Map.Entry) p.next();
-
destroyFacetImpl((Facet) entry.getValue());
}
}
@@ -388,7 +384,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
_communicator.getLogger().trace("Freeze::Evictor",
"destroyed \"" + Ice.Util.identityToString(ident) + "\"");
}
-
}
public void
@@ -489,7 +484,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
public void
removeAllFacets(Ice.Identity ident)
{
-
EvictorElement loadedElement = null;
int loadedElementGeneration = 0;
@@ -808,7 +802,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
if(cookie != null)
{
-
Facet facet= (Facet)cookie;
assert(facet != null);
@@ -1045,7 +1038,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
EvictorStorageKey esk = new EvictorStorageKey();
esk.identity = facet.element.identity;
esk.facet = facet.path;
- obj.key = marshal(esk, _communicator);
+ obj.key = marshalKey(esk, _communicator);
obj.status = status;
if(status != destroyed)
{
@@ -1159,8 +1152,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
}
}
} while(tryAgain);
-
-
+
synchronized(this)
{
_generation++;
@@ -1187,118 +1179,111 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
}
}
-
static byte[]
- marshalRoot(EvictorStorageKey v, Ice.Communicator communicator)
+ marshalRootKey(EvictorStorageKey v, Ice.Communicator communicator)
{
- try
- {
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- Ice.Stream __os = new IceXML.StreamI(communicator, pw);
- v.ice_marshal("Key", __os);
- String str = sw.toString();
-
- //
- // TODO: fix this!
- //
- int index = str.indexOf("</identity>");
- String root = str.substring(0, index + 11);
- return root.getBytes("UTF8");
- }
- catch(java.io.UnsupportedEncodingException ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
+ IceInternal.BasicStream os = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
+ try
+ {
+ v.identity.__write(os);
+ java.nio.ByteBuffer buf = os.prepareWrite();
+ byte[] r = new byte[buf.limit()];
+ buf.get(r);
+ return r;
+ }
+ finally
+ {
+ os.destroy();
+ }
}
static byte[]
- marshal(EvictorStorageKey v, Ice.Communicator communicator)
+ marshalKey(EvictorStorageKey v, Ice.Communicator communicator)
{
- try
- {
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- Ice.Stream __os = new IceXML.StreamI(communicator, pw);
- v.ice_marshal("Key", __os);
- return sw.toString().getBytes("UTF8");
- }
- catch(java.io.UnsupportedEncodingException ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
+ IceInternal.BasicStream os = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
+ try
+ {
+ v.__write(os);
+ java.nio.ByteBuffer buf = os.prepareWrite();
+ byte[] r = new byte[buf.limit()];
+ buf.get(r);
+ return r;
+ }
+ finally
+ {
+ os.destroy();
+ }
}
-
+
static EvictorStorageKey
unmarshalKey(byte[] b, Ice.Communicator communicator)
{
- try
- {
- final String data = _header + new String(b, "UTF8") + _footer;
- Ice.Stream __is = new IceXML.StreamI(communicator, new java.io.StringReader(data));
- Freeze.EvictorStorageKey __r = new Freeze.EvictorStorageKey();
- __r.ice_unmarshal("Key", __is);
- return __r;
- }
- catch(java.io.UnsupportedEncodingException ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
+ IceInternal.BasicStream is = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
+ try
+ {
+ is.resize(b.length, true);
+ java.nio.ByteBuffer buf = is.prepareRead();
+ buf.position(0);
+ buf.put(b);
+ buf.position(0);
+ EvictorStorageKey key = new EvictorStorageKey();
+ key.__read(is);
+ return key;
+ }
+ finally
+ {
+ is.destroy();
+ }
}
-
+
static byte[]
- marshal(ObjectRecord v, Ice.Communicator communicator)
+ marshalValue(ObjectRecord v, Ice.Communicator communicator)
{
- try
- {
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- Ice.Stream __os = new IceXML.StreamI(communicator, pw);
- __os.marshalFacets(false);
- v.ice_marshal("Value", __os);
- return sw.toString().getBytes("UTF8");
- }
- catch(java.io.UnsupportedEncodingException ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
+ IceInternal.BasicStream os = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
+ os.marshalFacets(false);
+ try
+ {
+ v.__write(os);
+ os.writePendingObjects();
+ java.nio.ByteBuffer buf = os.prepareWrite();
+ byte[] r = new byte[buf.limit()];
+ buf.get(r);
+ return r;
+ }
+ finally
+ {
+ os.destroy();
+ }
}
static ObjectRecord
unmarshalValue(byte[] b, Ice.Communicator communicator)
{
- try
- {
- final String data = _header + new String(b, "UTF8") + _footer;
- Ice.Stream __is = new IceXML.StreamI(communicator, new java.io.StringReader(data));
- Freeze.ObjectRecord __r = new Freeze.ObjectRecord();
- __r.ice_unmarshal("Value", __is);
- return __r;
- }
- catch(java.io.UnsupportedEncodingException ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
+ IceInternal.BasicStream is = new IceInternal.BasicStream(Ice.Util.getInstance(communicator));
+ try
+ {
+ is.resize(b.length, true);
+ java.nio.ByteBuffer buf = is.prepareRead();
+ buf.position(0);
+ buf.put(b);
+ buf.position(0);
+ ObjectRecord key = new ObjectRecord();
+ key.__read(is);
+ is.readPendingObjects();
+ return key;
+ }
+ finally
+ {
+ is.destroy();
+ }
}
private void
init(String envName, String dbName, boolean createDb)
{
- _trace = _communicator.getProperties().getPropertyAsInt(
- "Freeze.Trace.Evictor");
+ _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor");
- _errorPrefix = "Freeze Evictor DbEnv(\"" + envName + "\") Db(\"" +
- dbName + "\"): ";
+ _errorPrefix = "Freeze Evictor DbEnv(\"" + envName + "\") Db(\"" + dbName + "\"): ";
String propertyPrefix = "Freeze.Evictor." + envName + '.' + dbName;
@@ -1322,8 +1307,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
}
_db = new com.sleepycat.db.Db(_dbEnv, 0);
- _db.open(null, dbName, null, com.sleepycat.db.Db.DB_BTREE,
- flags, 0);
+ _db.open(null, dbName, null, com.sleepycat.db.Db.DB_BTREE, flags, 0);
//
// TODO: FREEZE_DB_MODE
@@ -1397,7 +1381,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
esk.identity = ident;
esk.facet = null;
- byte[] key = marshal(esk, _communicator);
+ byte[] key = marshalKey(esk, _communicator);
com.sleepycat.db.Dbt dbKey = new com.sleepycat.db.Dbt(key);
@@ -1443,7 +1427,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
}
}
-
private void
addToModifiedQueue(Facet facet)
{
@@ -1493,18 +1476,25 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
stats.lastSaveTime = saveStart - stats.creationTime;
stats.avgSaveTime = (long)(stats.avgSaveTime * 0.95 + diff * 0.05);
}
- return marshal(rec, _communicator);
+ return marshalValue(rec, _communicator);
}
private EvictorElement
load(Ice.Identity ident)
{
- EvictorStorageKey esk = new EvictorStorageKey();
- esk.identity = ident;
- esk.facet = null;
- byte[] root = marshalRoot(esk, _communicator);
-
- com.sleepycat.db.Dbt dbKey = new com.sleepycat.db.Dbt(root);
+ //
+ // This method attempts to restore an object and all of its facets from the database. It works by
+ // iterating over the database keys that match the "root" key. The root key is the encoded portion
+ // of the EvictorStorageKey struct that the object and its facets all have in common, namely the
+ // identity.
+ //
+ EvictorStorageKey rootEsk = new EvictorStorageKey();
+ rootEsk.identity = ident;
+ rootEsk.facet = null;
+ byte[] root = marshalRootKey(rootEsk, _communicator);
+ byte[] mainKey = marshalKey(rootEsk, _communicator);
+
+ com.sleepycat.db.Dbt dbKey;
com.sleepycat.db.Dbt dbValue = new com.sleepycat.db.Dbt();
EvictorElement result = null;
@@ -1521,9 +1511,10 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
//
// Get first pair
//
- int rs = dbc.get(dbKey, dbValue, com.sleepycat.db.Db.DB_SET_RANGE);
+ dbKey = new com.sleepycat.db.Dbt(mainKey);
+ int rs = dbc.get(dbKey, dbValue, com.sleepycat.db.Db.DB_SET);
- if(rs != 0 || !startWith(dbKey.get_data(), root))
+ if(rs != 0)
{
if(_trace >= 2)
{
@@ -1540,7 +1531,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
//
// Unmarshal key and data and insert it into result's facet map
//
- esk = unmarshalKey(dbKey.get_data(), _communicator);
+ EvictorStorageKey esk = unmarshalKey(dbKey.get_data(), _communicator);
Facet facet = new Facet(result);
facet.status = clean;
@@ -1748,7 +1739,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
}
}
-
private void
removeFacetImpl(java.util.Map facets, String[] facetPath)
{
@@ -1851,7 +1841,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
byte[] key;
byte[] value;
byte status;
- };
+ }
class EvictorElement
{
@@ -1860,7 +1850,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
java.util.Map facets = new java.util.HashMap();
Ice.Identity identity;
Facet mainObject;
- };
+ }
class Facet extends Ice.LocalObjectImpl
{
@@ -1872,7 +1862,7 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
ObjectRecord rec;
EvictorElement element;
String[] path;
- };
+ }
//
// Wrapper to use a String[] as key of a HashMap.
@@ -1918,7 +1908,6 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
String[] array;
}
-
//
// Clean object; can become modified or destroyed
//
@@ -2004,12 +1993,4 @@ class EvictorI extends Ice.LocalObjectImpl implements Evictor, Runnable
// this element, then the loaded value is current.
//
private int _generation = 0;
-
-
-
- private static final String _header = "<ice:data xmlns=\"http://www.noorg.org/schemas\"" +
- " xmlns:ice=\"http://www.zeroc.com/schemas\"" +
- " xmlns:xsi=\"http://www.w3.org/2003/XMLSchema-instance\"" +
- " xsi:schemaLocation=\"http://www.noorg.org/schemas IdentityObjectRecordDict.xsd\">";
- private static final String _footer = "</ice:data>";
}
diff --git a/java/src/Freeze/Map.java b/java/src/Freeze/Map.java
index 58eda7aba1e..90b3932d45d 100644
--- a/java/src/Freeze/Map.java
+++ b/java/src/Freeze/Map.java
@@ -585,7 +585,8 @@ public abstract class Map extends java.util.AbstractMap
{
if(_trace >= 3)
{
- _communicator.getLogger().trace("DB", "starting transaction for cursor on database \"" + _dbName + "\"");
+ _communicator.getLogger().trace("DB", "starting transaction for cursor on database \"" + _dbName +
+ "\"");
}
try
@@ -786,7 +787,8 @@ public abstract class Map extends java.util.AbstractMap
{
if(_trace >= 3)
{
- _communicator.getLogger().trace("DB", "committing transaction for cursor on database \"" + _dbName + "\"");
+ _communicator.getLogger().trace("DB", "committing transaction for cursor on database \"" +
+ _dbName + "\"");
}
try
@@ -912,7 +914,6 @@ public abstract class Map extends java.util.AbstractMap
}
}
-
private void
closeCursor(com.sleepycat.db.Dbc cursor)
{
@@ -951,7 +952,8 @@ public abstract class Map extends java.util.AbstractMap
{
if(_trace >= 3)
{
- _communicator.getLogger().trace("DB", "rolling back transaction for cursor on database \"" + _dbName + "\"");
+ _communicator.getLogger().trace("DB", "rolling back transaction for cursor on database \"" +
+ _dbName + "\"");
}
try
@@ -988,7 +990,8 @@ public abstract class Map extends java.util.AbstractMap
static class Entry implements java.util.Map.Entry
{
public
- Entry(Map.EntryIterator iterator, Map map, Ice.Communicator communicator, com.sleepycat.db.Dbt dbKey, byte[] valueBytes)
+ Entry(Map.EntryIterator iterator, Map map, Ice.Communicator communicator, com.sleepycat.db.Dbt dbKey,
+ byte[] valueBytes)
{
_iterator = iterator;
_map = map;
@@ -1059,7 +1062,8 @@ public abstract class Map extends java.util.AbstractMap
return getKey() + "=" + getValue();
}
- com.sleepycat.db.Dbt getDbKey()
+ com.sleepycat.db.Dbt
+ getDbKey()
{
return _dbKey;
}
@@ -1080,7 +1084,37 @@ public abstract class Map extends java.util.AbstractMap
private Object _value = null;
private boolean _haveValue = false;
}
-
+
+ public static class Patcher implements IceInternal.Patcher
+ {
+ public
+ Patcher(String type)
+ {
+ this.type = type;
+ }
+
+ public void
+ patch(Ice.Object v)
+ {
+ value = v;
+ }
+
+ public String
+ type()
+ {
+ return this.type;
+ }
+
+ public Ice.Object
+ value()
+ {
+ return this.value;
+ }
+
+ public String type;
+ public Ice.Object value;
+ }
+
private java.util.Set _entrySet = null;
private SharedDbEnv _dbEnvHolder = null;
private com.sleepycat.db.DbEnv _dbEnv = null;
diff --git a/java/src/Ice/Object.java b/java/src/Ice/Object.java
index e8d08afd670..eac98105262 100644
--- a/java/src/Ice/Object.java
+++ b/java/src/Ice/Object.java
@@ -34,16 +34,10 @@ public interface Object
IceInternal.DispatchStatus __dispatch(IceInternal.Incoming in, Current current);
- void __write(IceInternal.BasicStream __os);
+ void __write(IceInternal.BasicStream __os, boolean __marshalFacets);
void __read(IceInternal.BasicStream __is, boolean __rid);
- void __marshal(Ice.Stream __os, boolean __marshalFacets);
-
- void __unmarshal(Ice.Stream __is);
-
- void ice_marshal(String name, Ice.Stream stream);
-
void ice_addFacet(Object facet, String name);
Object ice_removeFacet(String name);
diff --git a/java/src/Ice/ObjectImpl.java b/java/src/Ice/ObjectImpl.java
index 0600b15bd46..adbe1191736 100644
--- a/java/src/Ice/ObjectImpl.java
+++ b/java/src/Ice/ObjectImpl.java
@@ -194,25 +194,34 @@ public class ObjectImpl implements Object, java.lang.Cloneable
}
public void
- __write(IceInternal.BasicStream __os)
+ __write(IceInternal.BasicStream __os, boolean __marshalFacets)
{
- synchronized(_activeFacetMap)
+ __os.writeTypeId(ice_staticId());
+ __os.startWriteSlice();
+
+ if(__marshalFacets)
{
- __os.writeTypeId(ice_staticId());
- __os.startWriteSlice();
- final int sz = _activeFacetMap.size();
- __os.writeSize(sz);
-
- java.util.Set set = _activeFacetMap.keySet();
- String[] keys = new String[sz];
- set.toArray(keys);
- for(int i = 0; i < sz; i++)
+ synchronized(_activeFacetMap)
{
- __os.writeString(keys[i]);
- __os.writeObject((Object)_activeFacetMap.get(keys[i]));
+ final int sz = _activeFacetMap.size();
+ __os.writeSize(sz);
+
+ java.util.Set set = _activeFacetMap.keySet();
+ String[] keys = new String[sz];
+ set.toArray(keys);
+ for(int i = 0; i < sz; i++)
+ {
+ __os.writeString(keys[i]);
+ __os.writeObject((Object)_activeFacetMap.get(keys[i]));
+ }
}
- __os.endWriteSlice();
}
+ else
+ {
+ __os.writeSize(0);
+ }
+
+ __os.endWriteSlice();
}
private class Patcher implements IceInternal.Patcher
@@ -263,73 +272,6 @@ public class ObjectImpl implements Object, java.lang.Cloneable
}
}
- public void
- __marshal(Ice.Stream __os, boolean __marshalFacets)
- {
- if(__marshalFacets)
- {
- synchronized(_activeFacetMap)
- {
- final int sz = _activeFacetMap.size();
-
- __os.startWriteDictionary("ice:facets", sz);
- java.util.Set set = _activeFacetMap.keySet();
- String[] keys = new String[sz];
- set.toArray(keys);
- for(int i = 0; i < sz; i++)
- {
- __os.startWriteDictionaryElement();
- __os.writeString("ice:key", keys[i]);
- __os.writeObject("ice:value", (Object)_activeFacetMap.get(keys[i]));
- __os.endWriteDictionaryElement();
- }
- }
- }
- else
- {
- __os.startWriteDictionary("ice:facets", 0);
- }
-
- __os.endWriteDictionary();
- }
-
- public void
- __unmarshal(Ice.Stream __is)
- {
- synchronized(_activeFacetMap)
- {
- final String facetsName = "ice:facets";
- final String keyName = "ice:key";
- final String valueName = "ice:value";
-
- int sz = __is.startReadDictionary(facetsName);
-
- _activeFacetMap.clear();
-
- while(sz-- > 0)
- {
- __is.startReadDictionaryElement();
- String key = __is.readString(keyName);
- Object value = __is.readObject(valueName, "", null);
- _activeFacetMap.put(key, value);
- __is.endReadDictionaryElement();
- }
- __is.endReadDictionary();
- }
- }
-
- public final void
- ice_marshal(String name, Ice.Stream stream)
- {
- stream.writeObject(name, this);
- }
-
- public static Object
- ice_unmarshal(String name, Ice.Stream stream)
- {
- return stream.readObject(name, "", null);
- }
-
public final void
ice_addFacet(Object facet, String name)
{
diff --git a/java/src/Ice/UserException.java b/java/src/Ice/UserException.java
index d8d41b3c3a5..b338d13113a 100644
--- a/java/src/Ice/UserException.java
+++ b/java/src/Ice/UserException.java
@@ -39,23 +39,9 @@ public abstract class UserException extends Exception
public abstract void
__read(IceInternal.BasicStream __is, boolean __rid);
- public abstract void
- __marshal(Stream __os);
-
- public abstract void
- __unmarshal(Stream __is);
-
public boolean
__usesClasses()
{
return false;
}
-
- public final void
- ice_marshal(String __name, Stream __os)
- {
- __os.startWriteException(__name);
- __marshal(__os);
- __os.endWriteException();
- }
}
diff --git a/java/src/IceInternal/BasicStream.java b/java/src/IceInternal/BasicStream.java
index b8b175f5100..d7b5d5d3f6b 100644
--- a/java/src/IceInternal/BasicStream.java
+++ b/java/src/IceInternal/BasicStream.java
@@ -34,6 +34,8 @@ public class BasicStream
_traceSlicing = -1;
+ _marshalFacets = true;
+
_messageSizeMax = _instance.messageSizeMax(); // Cached for efficiency.
}
@@ -1311,11 +1313,17 @@ public class BasicStream
while(num > 0);
}
+ public void
+ marshalFacets(boolean b)
+ {
+ _marshalFacets = b;
+ }
+
void
writeInstance(Ice.Object v, Integer index)
{
writeInt(index.intValue());
- v.__write(this);
+ v.__write(this, _marshalFacets);
}
void
@@ -1642,5 +1650,7 @@ public class BasicStream
private int _traceSlicing;
private String _slicingCat;
+ private boolean _marshalFacets;
+
private int _messageSizeMax;
}
diff --git a/java/src/IceXML/StreamI.java b/java/src/IceXML/StreamI.java
deleted file mode 100644
index 189a7d52720..00000000000
--- a/java/src/IceXML/StreamI.java
+++ /dev/null
@@ -1,1616 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003
-// ZeroC, Inc.
-// Billerica, MA, USA
-//
-// All Rights Reserved.
-//
-// Ice is free software; you can redistribute it and/or modify it under
-// the terms of the GNU General Public License version 2 as published by
-// the Free Software Foundation.
-//
-// **********************************************************************
-
-package IceXML;
-
-public class StreamI extends Ice.LocalObjectImpl implements Ice.Stream
-{
- private
- StreamI()
- {
- }
-
- public
- StreamI(Ice.Communicator communicator, java.io.PrintWriter writer)
- {
- _communicator = communicator;
- _os = new IceUtil.XMLOutput(writer);
- _level = 0;
- _nextId = 0;
- _objects = new java.util.IdentityHashMap();
- _dump = false;
- }
-
- public
- StreamI(Ice.Communicator communicator, java.io.Reader input)
- {
- this(communicator, input, true);
- }
-
- public
- StreamI(Ice.Communicator communicator, java.io.Reader input, boolean schema)
- {
- _communicator = communicator;
- _nodeStack = new java.util.LinkedList();
- _readObjects = new java.util.HashMap();
- _nextId = 0;
-
- Ice.Logger logger = communicator.getLogger();
-
- //
- // Create our parser, then attach an error handler to the parser.
- // The parser will call back to methods of the ErrorHandler if it
- // discovers errors during the course of parsing the XML
- // document.
- //
- javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
- //
- // TODO: Using the standard JDK parser, calling setValidating(true)
- // appears to enable DTD validation, not schema validation. This
- // needs to be generalized to support other implementations.
- //
- factory.setValidating(false);
- if(schema)
- {
- factory.setNamespaceAware(true);
- //factory.setDoSchema(true); // TODO
- }
-
- javax.xml.parsers.DocumentBuilder parser = null;
- try
- {
- parser = factory.newDocumentBuilder();
- }
- catch(javax.xml.parsers.ParserConfigurationException ex)
- {
- logger.error("parser configuration error: " + ex.getMessage());
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- parser.setErrorHandler(new DOMTreeErrorReporter(logger));
- //parser.setCreateEntityReferenceNodes(false); // TODO
- //parser.setToCreateXMLDeclTypeNode(true); // TODO
-
- // TODO:
- // parser.setEntityResolver
-
- //
- // Parse the XML file, catching any XML exceptions that might propagate
- // out of it.
- //
- boolean errorsOccurred = false;
- try
- {
- _document = parser.parse(new org.xml.sax.InputSource(input));
- }
- catch(java.io.IOException ex)
- {
- logger.error("I/O error during parsing: " + ex.getMessage());
- errorsOccurred = true;
- }
- catch(org.xml.sax.SAXException ex)
- {
- logger.error("parsing error: " + ex.getMessage());
- errorsOccurred = true;
- }
-
- if(errorsOccurred)
- {
- throw new Ice.MarshalException();
- }
-
- //
- // The first child of the document is the root node - ignore
- // that. Move to the top-level node in the document content.
- //
- _current = _document.getFirstChild();
- _current = _current.getFirstChild();
- }
-
- public
- StreamI(Ice.Communicator communicator, org.w3c.dom.Document document)
- {
- _communicator = communicator;
- _nodeStack = new java.util.LinkedList();
- _readObjects = new java.util.HashMap();
- _nextId = 0;
- _document = document;
-
- //
- // The first child of the document is the root node - ignore
- // that. Move to the top-level node in the document content.
- //
- _current = _document.getFirstChild();
- _current = _current.getFirstChild();
- }
-
- public void
- startWriteDictionary(String name, int size)
- {
- String s = name + " length=\"" + size + "\"";
- startWrite(s);
- }
-
- public void
- endWriteDictionary()
- {
- endWrite();
- }
-
- public void
- startWriteDictionaryElement()
- {
- startWrite(getWritePrefix() + seqElementName);
- }
-
- public void
- endWriteDictionaryElement()
- {
- endWrite();
- }
-
- public int
- startReadDictionary(String name)
- {
- startRead(name);
- int size = readLength();
- _current = _current.getFirstChild();
- return size;
- }
-
- public void
- endReadDictionary()
- {
- endRead();
- }
-
- public void
- startReadDictionaryElement()
- {
- startRead(getReadPrefix() + seqElementName);
- _current = _current.getFirstChild();
- }
-
- public void
- endReadDictionaryElement()
- {
- endRead();
- }
-
- public void
- startWriteSequence(String name, int size)
- {
- String s = name + " length=\"" + size + "\"";
- startWrite(s);
- }
-
- public void
- endWriteSequence()
- {
- endWrite();
- }
-
- public void
- startWriteSequenceElement()
- {
- startWrite(getWritePrefix() + seqElementName);
- }
-
- public void
- endWriteSequenceElement()
- {
- endWrite();
- }
-
- public int
- startReadSequence(String name)
- {
- startRead(name);
- int size = readLength();
- _current = _current.getFirstChild();
- return size;
- }
-
- public void
- endReadSequence()
- {
- endRead();
- }
-
- public void
- startReadSequenceElement()
- {
- startRead(getReadPrefix() + seqElementName);
- }
-
- public void
- endReadSequenceElement()
- {
- endRead();
- }
-
- public void
- startWriteStruct(String name)
- {
- startWrite(name);
- }
-
- public void
- endWriteStruct()
- {
- endWrite();
- }
-
- public void
- startReadStruct(String name)
- {
- startRead(name);
- _current = _current.getFirstChild();
- }
-
- public void
- endReadStruct()
- {
- endRead();
- }
-
- public void
- startWriteException(String name)
- {
- startWrite(name);
- }
-
- public void
- endWriteException()
- {
- endWrite();
- }
-
- public void
- startReadException(String name)
- {
- startRead(name);
- _current = _current.getFirstChild();
- }
-
- public void
- endReadException()
- {
- endRead();
- }
-
- public void
- writeEnum(String name, String[] table, int ordinal)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name);
- _os.startEscapes().write(table[ordinal]).endEscapes();
- _os.ee();
- }
-
- public int
- readEnum(String name, String[] table)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String value = child.getNodeValue();
- int ordinal;
- for(ordinal = 0; ordinal < table.length; ordinal++)
- {
- if(value.equals(table[ordinal]))
- {
- break;
- }
- }
- if(ordinal == table.length)
- {
- throw new Ice.MarshalException();
- }
-
- endRead();
-
- return ordinal;
- }
-
- public void
- writeByte(String name, byte value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- // The schema encoding for xs:byte is a value from -127 to 128.
- _os.se(name);
- _os.startEscapes().write("" + (int)value).endEscapes();
- _os.ee();
- }
-
- public void
- writeByteSeq(String name, byte[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- startWriteSequenceElement();
- _os.startEscapes().write("" + (int)value[i]).endEscapes();
- endWriteSequenceElement();
- }
- endWriteSequence();
- }
- }
-
- public byte
- readByte(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- byte i = 0;
- try
- {
- i = Byte.parseByte(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public byte[]
- readByteSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- byte[] value = new byte[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readByte(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeBool(String name, boolean value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write(value ? "true" : "false").ee();
- }
-
- public void
- writeBoolSeq(String name, boolean[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write(value[i] ? "true" : "false").ee();
- }
- endWriteSequence();
- }
- }
-
- public boolean
- readBool(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- boolean value = s.equals("true");
-
- endRead();
-
- return value;
- }
-
- public boolean[]
- readBoolSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- boolean[] value = new boolean[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readBool(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeShort(String name, short value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write("" + value).ee();
- }
-
- public void
- writeShortSeq(String name, short[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write("" + value[i]).ee();
- }
- endWriteSequence();
- }
- }
-
- public short
- readShort(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- short i = 0;
- try
- {
- i = Short.parseShort(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public short[]
- readShortSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- short[] value = new short[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readShort(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeInt(String name, int value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write("" + value).ee();
- }
-
- public void
- writeIntSeq(String name, int[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write("" + value[i]).ee();
- }
- endWriteSequence();
- }
- }
-
- public int
- readInt(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- int i = 0;
- try
- {
- i = Integer.parseInt(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public int[]
- readIntSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- int[] value = new int[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readInt(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeLong(String name, long value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write("" + value).ee();
- }
-
- public void
- writeLongSeq(String name, long[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write("" + value[i]).ee();
- }
- endWriteSequence();
- }
- }
-
- public long
- readLong(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- long i = 0;
- try
- {
- i = Long.parseLong(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public long[]
- readLongSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- long[] value = new long[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readLong(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeFloat(String name, float value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write("" + value).ee();
- }
-
- public void
- writeFloatSeq(String name, float[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write("" + value[i]).ee();
- }
- endWriteSequence();
- }
- }
-
- public float
- readFloat(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- float i = 0;
- try
- {
- i = Float.parseFloat(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public float[]
- readFloatSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- float[] value = new float[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readFloat(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeDouble(String name, double value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name).write("" + value).ee();
- }
-
- public void
- writeDoubleSeq(String name, double[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e").write("" + value[i]).ee();
- }
- endWriteSequence();
- }
- }
-
- public double
- readDouble(String name)
- {
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
-
- String s = child.getNodeValue();
- double i = 0;
- try
- {
- i = Double.parseDouble(s);
- }
- catch(NumberFormatException ex)
- {
- Ice.MarshalException e = new Ice.MarshalException();
- e.initCause(ex);
- throw e;
- }
-
- endRead();
-
- return i;
- }
-
- public double[]
- readDoubleSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- double[] value = new double[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readDouble(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeString(String name, String value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
-
- _os.se(name);
- if(value != null && value.length() > 0)
- {
- _os.startEscapes().write(value == null ? "" : value).endEscapes();
- }
- _os.ee();
- }
-
- public void
- writeStringSeq(String name, String[] value)
- {
- if(value == null)
- {
- startWriteSequence(name, 0);
- endWriteSequence();
- }
- else
- {
- startWriteSequence(name, value.length);
- for(int i = 0; i < value.length; i++)
- {
- _os.se("e");
- if(value[i] != null && value[i].length() > 0)
- {
- _os.startEscapes().write(value[i] == null ? "" : value[i]).endEscapes();
- }
- _os.ee();
- }
- endWriteSequence();
- }
- }
-
- public String
- readString(String name)
- {
- String value = null;
-
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- if(child != null)
- {
- if(child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
- value = child.getNodeValue();
- }
- else
- {
- value = "";
- }
-
- endRead();
-
- return value;
- }
-
- public String[]
- readStringSeq(String name)
- {
- startRead(name);
-
- final String elem = getPrefix(name) + seqElementName;
- int size = readLength();
- String[] value = new String[size];
- if(size > 0)
- {
- _current = _current.getFirstChild();
- for(int i = 0; i < size; i++)
- {
- value[i] = readString(elem);
- }
- }
-
- endRead();
-
- return value;
- }
-
- public void
- writeProxy(String name, Ice.ObjectPrx value)
- {
- // No attributes
- assert(IceInternal.StringUtil.findFirstOf(name, " \t") == -1);
- String s = _communicator.proxyToString(value);
-
- _os.se(name);
- if(s.length() > 0)
- {
- _os.startEscapes().write(s).endEscapes();
- }
- _os.ee();
- }
-
- public Ice.ObjectPrx
- readProxy(String name)
- {
- Ice.ObjectPrx value = null;
-
- startRead(name);
-
- org.w3c.dom.Node child = _current.getFirstChild();
- String s = "";
- if(child != null)
- {
- if(child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
- {
- throw new Ice.MarshalException();
- }
- s = child.getNodeValue();
- }
-
- value = _communicator.stringToProxy(s);
-
- endRead();
-
- return value;
- }
-
- public void
- writeObject(String name, Ice.Object value)
- {
- //
- // If at the top level of the document then the object itself must
- // be written, otherwise write a reference.
- //
- boolean writeReference = (_level != 0);
-
- //
- // If the object doesn't exist in the map add it.
- //
- ObjectInfo info = (ObjectInfo)_objects.get(value);
- if(info == null)
- {
- String s = "object" + _nextId++;
- info = new ObjectInfo(s, !writeReference);
- _objects.put(value, info);
- }
-
- if(writeReference)
- {
- _os.nl();
- _os.print("<" + name + " href=\"#" + info.id + "\"/>");
- }
- else
- {
- //
- // Precondition: If the actual object is being written the
- // written flag should be true.
- //
- assert(info.written);
- writeObjectData(name, info.id, value);
- }
- }
-
- public Ice.Object
- readObject(String name, String signatureType, Ice.ObjectFactory factory)
- {
- Ice.Object value = null;
-
- startRead(name);
-
- //
- // Node attributes. Either id & type will be set, or href.
- //
- // TODO: Eliminate Holders?
- //
- Ice.StringHolder id = new Ice.StringHolder();
- Ice.StringHolder type = new Ice.StringHolder();
- Ice.StringHolder href = new Ice.StringHolder();
- readAttributes(id, type, href);
-
- //
- // If this object is an href then locate the object. If the object
- // has been previously read then it will be contained in the
- // readObjects list. Otherwise, it should be in the object-set at
- // the top-level of the document.
- //
- if(href.value != null && href.value.length() > 0)
- {
- href.value = href.value.substring(1); // Skip the '#'
-
- value = (Ice.Object)_readObjects.get(href.value);
- if(value != null)
- {
- endRead();
- return value;
- }
-
- //
- // The first child of the document is the root node - ignore
- // that. Move to the top-level node in the document content.
- //
- _current = _document.getFirstChild();
- _current = _current.getFirstChild();
- while(_current != null)
- {
- if(_current.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
- {
- Ice.StringHolder dummy = new Ice.StringHolder();
- readAttributes(id, type, dummy);
- if(id.value != null && id.value.equals(href.value))
- {
- break;
- }
- }
- _current = _current.getNextSibling();
- }
- //
- // If the object isn't found, that's an error.
- //
- if(_current == null)
- {
- throw new Ice.MarshalException();
- }
- }
-
- //
- // Empty type indicates nil object.
- //
- if(type.value.length() == 0)
- {
- value = null;
- }
- else
- {
- if(type.value.equals("::Ice::Object"))
- {
- value = new Ice.ObjectImpl();
- }
- else
- {
- //
- // First, determine if there is an installed factory for this
- // type. If so use that. If that factory doesn't exist, or it
- // doesn't create an object of that type then use the supplied
- // factory provided that the types are the same. If no value
- // can be created then throw a NoObjectFactoryException.
- //
- Ice.ObjectFactory userFactory = _communicator.findObjectFactory(type.value);
- if(userFactory != null)
- {
- value = userFactory.create(type.value);
- }
-
- if(value == null && type.value.equals(signatureType))
- {
- assert(factory != null);
- value = factory.create(type.value);
- assert(value != null);
- }
-
- if(value == null)
- {
- userFactory = loadObjectFactory(type.value);
- if(userFactory != null)
- {
- value = userFactory.create(type.value);
- }
- }
-
- if(value == null)
- {
- Ice.NoObjectFactoryException e = new Ice.NoObjectFactoryException();
- e.type = type.value;
- throw e;
- }
- }
-
- //
- // Add the object to the readObjects map, move to the first
- // child node & unmarshal the object.
- //
- _readObjects.put(id.value, value);
- _current = _current.getFirstChild();
- value.__unmarshal(this);
- }
-
- endRead();
-
- return value;
- }
-
- public void
- marshalFacets(boolean setting)
- {
- _marshalFacets = setting;
- }
-
- private void
- startWrite(String name)
- {
- _os.se(name);
- ++_level;
- }
-
- private void
- endWrite()
- {
- --_level;
-
- _os.ee();
-
- if(_level == 0 && !_dump)
- {
- dumpUnwrittenObjects();
- }
- }
-
- private void
- startRead(String name)
- {
- while(_current != null && _current.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
- {
- _current = _current.getNextSibling();
- }
- if(_current == null)
- {
- throw new Ice.MarshalException();
- }
-
- String nodeName = _current.getNodeName();
- //
- // TODO: Work around for bug in xerces
- //
- final String facets = "facets";
- final String facetsNS = "ice:facets";
- if((!name.equals(facetsNS) || !nodeName.equals(facets)) && !name.equals(nodeName))
- {
- throw new Ice.MarshalException();
- }
-
- _nodeStack.add(_current);
- }
-
- private void
- endRead()
- {
- _current = (org.w3c.dom.Node)_nodeStack.removeLast();
- _current = _current.getNextSibling();
- }
-
- private void
- dumpUnwrittenObjects()
- {
- //
- // Precondition: Must be at the top-level.
- //
- assert(_level == 0 && !_dump);
-
- _dump = true;
-
- //
- // It's necessary to run through the set of unwritten objects
- // repeatedly until all referenced objects have been written
- // (since referenced objects can themselves reference other
- // objects).
- //
- int nwritten;
- do
- {
- nwritten = 0;
- java.util.Iterator p = _objects.entrySet().iterator();
- try
- {
- while(p.hasNext())
- {
- java.util.Map.Entry e = (java.util.Map.Entry)p.next();
- Ice.Object obj = (Ice.Object)e.getKey();
- ObjectInfo info = (ObjectInfo)e.getValue();
- if(!info.written)
- {
- info.written = true;
- writeObjectData("ice:object", info.id, obj);
- }
- ++nwritten;
- }
- }
- catch(java.util.ConcurrentModificationException ex)
- {
- //
- // This will be thrown if _objects has been modified, e.g.,
- // if a new object is added. We need to start the loop
- // again with a new iterator.
- //
- }
- }
- while(_objects.size() != nwritten);
-
- _dump = false;
- }
-
- private void
- writeObjectData(String name, String id, Ice.Object obj)
- {
- String xsdType = "";
- String typeId = "";
-
- if(obj != null)
- {
- //
- // Convert the type-id to the equivalent schema type
- // definition name.
- //
- typeId = obj.ice_id(null);
-
- final String xsdPrefix = "_internal.";
- final String xsdSuffix = "Type";
- xsdType += xsdPrefix;
- int i = 0;
- if(typeId.charAt(0) == ':') // Skip the preceeding "::".
- {
- i = 2;
- }
- for(; i < typeId.length(); ++i)
- {
- if(typeId.charAt(i) == ':')
- {
- xsdType += '.';
- ++i;
- }
- else
- {
- xsdType += typeId.charAt(i);
- }
- }
- xsdType += xsdSuffix;
- }
- else
- {
- xsdType = "";
- }
-
- String s = name + " id=\"" + id + "\" type=\"" + typeId + "\"" + " xsi:type=\"" + xsdType + "\"";
- if(obj == null)
- {
- s += " xsi:nil=\"true\"";
- }
-
- startWrite(s);
- if(obj != null)
- {
- obj.__marshal(this, _marshalFacets);
- }
- endWrite();
- }
-
- private void
- readAttributes(Ice.StringHolder id, Ice.StringHolder type, Ice.StringHolder href)
- {
- final String idStr = "id";
- final String typeStr = "type";
- final String hrefStr = "href";
-
- org.w3c.dom.NamedNodeMap attributes = _current.getAttributes();
- int attrCount = attributes.getLength();
- for(int i = 0; i < attrCount; i++)
- {
- org.w3c.dom.Node attribute = attributes.item(i);
- String name = attribute.getNodeName();
- if(name.equals(idStr))
- {
- id.value = attribute.getNodeValue();
- }
- else if(name.equals(typeStr))
- {
- type.value = attribute.getNodeValue();
- }
- else if(name.equals(hrefStr))
- {
- href.value = attribute.getNodeValue();
- }
- }
- }
-
- private int
- readLength()
- {
- final String lengthStr = "length";
-
- org.w3c.dom.NamedNodeMap attributes = _current.getAttributes();
- int attrCount = attributes.getLength();
- for(int i = 0; i < attrCount; i++)
- {
- org.w3c.dom.Node attribute = attributes.item(i);
- String name = attribute.getNodeName();
- if(name.equals(lengthStr))
- {
- try
- {
- return Integer.parseInt(attribute.getNodeValue());
- }
- catch(NumberFormatException ex)
- {
- throw new Ice.MarshalException();
- }
- }
- }
-
- throw new Ice.MarshalException();
- }
-
- private String
- getWritePrefix()
- {
- String name = _os.currentElement();
- assert(name.length() != 0);
- return getPrefix(name);
- }
-
- private String
- getReadPrefix()
- {
- assert(!_nodeStack.isEmpty());
- return getPrefix(((org.w3c.dom.Node)_nodeStack.getLast()).getNodeName());
- }
-
- private static String
- getPrefix(String s)
- {
- int pos = s.indexOf(':');
- if(pos != -1)
- {
- return s.substring(0, pos + 1);
- }
- else
- {
- return "";
- }
- }
-
- private static final class DynamicObjectFactory extends Ice.LocalObjectImpl implements Ice.ObjectFactory
- {
- DynamicObjectFactory(Class c)
- {
- _class = c;
- }
-
- public Ice.Object
- create(String type)
- {
- try
- {
- return (Ice.Object)_class.newInstance();
- }
- catch(Exception ex)
- {
- Ice.SyscallException e = new Ice.SyscallException();
- e.initCause(ex);
- throw e;
- }
- }
-
- public void
- destroy()
- {
- }
-
- private Class _class;
- }
-
- private Ice.ObjectFactory
- loadObjectFactory(String id)
- {
- Ice.ObjectFactory factory = null;
-
- try
- {
- Class c = Class.forName(typeToClass(id));
- //
- // Ensure the class is instantiable. The constants are
- // defined in the JVM specification (0x200 = interface,
- // 0x400 = abstract).
- //
- int modifiers = c.getModifiers();
- if((modifiers & 0x200) == 0 && (modifiers & 0x400) == 0)
- {
- Ice.ObjectFactory dynamicFactory = new DynamicObjectFactory(c);
- //
- // We will try to install the dynamic factory, but another thread
- // may install a factory first.
- //
- while(factory == null)
- {
- try
- {
- _communicator.addObjectFactory(dynamicFactory, id);
- factory = dynamicFactory;
- }
- catch(Ice.AlreadyRegisteredException ex)
- {
- //
- // Another thread already installed the factory, so try
- // to obtain it. It's possible (but unlikely) that the factory
- // will have already been removed, in which case the return
- // value will be null and the while loop will attempt to
- // install the dynamic factory again.
- //
- factory = _communicator.findObjectFactory(id);
- }
- }
- }
- }
- catch(ClassNotFoundException ex)
- {
- // Ignore
- }
- catch(Exception ex)
- {
- Ice.NoObjectFactoryException e = new Ice.NoObjectFactoryException();
- e.type = id;
- e.initCause(ex);
- throw e;
- }
-
- return factory;
- }
-
- private static String
- typeToClass(String id)
- {
- assert(id.startsWith("::"));
- return id.substring(2).replaceAll("::", ".");
- }
-
- private Ice.Communicator _communicator;
-
- //
- // The sequence element name
- //
- private static final String seqElementName = "e";
-
- //
- // For reading.
- //
- private org.w3c.dom.Document _document;
- private org.w3c.dom.Node _current;
- private java.util.LinkedList _nodeStack;
- private java.util.HashMap _readObjects;
-
- //
- // For writing.
- //
-
- //
- // XML Output stream.
- //
- private IceUtil.XMLOutput _os;
-
- //
- // Current document level.
- //
- private int _level;
-
- //
- // Next id for marshalling objects.
- //
- private int _nextId;
- private static class ObjectInfo
- {
- ObjectInfo(String i, boolean w)
- {
- id = i;
- written = w;
- }
-
- String id; // The id of the associated object
- boolean written; // Has the object been written?
- }
- private java.util.IdentityHashMap _objects;
- private boolean _dump;
- private boolean _marshalFacets = true;
-
- private static class DOMTreeErrorReporter implements org.xml.sax.ErrorHandler
- {
- DOMTreeErrorReporter(Ice.Logger logger)
- {
- _logger = logger;
- _sawErrors = false;
- }
-
- public void
- warning(org.xml.sax.SAXParseException ex)
- throws org.xml.sax.SAXException
- {
- String s = "file: \"" + ex.getSystemId() +
- "\", line: " + ex.getLineNumber() +
- ", column: " + ex.getColumnNumber() +
- "\nmessage: " + ex.getMessage();
- _logger.warning(s);
- }
-
- public void
- error(org.xml.sax.SAXParseException ex)
- throws org.xml.sax.SAXException
- {
- _sawErrors = true;
- String s = "file: \"" + ex.getSystemId() +
- "\", line: " + ex.getLineNumber() +
- ", column: " + ex.getColumnNumber() +
- "\nmessage: " + ex.getMessage();
- _logger.error(s);
- }
-
- public void
- fatalError(org.xml.sax.SAXParseException ex)
- throws org.xml.sax.SAXException
- {
- _sawErrors = true;
- String s = "file: \"" + ex.getSystemId() +
- "\", line: " + ex.getLineNumber() +
- ", column: " + ex.getColumnNumber() +
- "\nmessage: " + ex.getMessage();
- _logger.error(s);
- }
-
- boolean
- getSawErrors()
- {
- return _sawErrors;
- }
-
- private Ice.Logger _logger;
- private boolean _sawErrors;
- }
-}