openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r406215 [9/10] - in /incubator/openjpa/trunk/openjpa-lib: ./ java/ java/org/ java/org/apache/ java/org/apache/openjpa/ java/org/apache/openjpa/lib/ java/org/apache/openjpa/lib/conf/ java/org/apache/openjpa/lib/jdbc/ java/org/apache/openjpa/...
Date Sun, 14 May 2006 03:26:02 GMT
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParameterTemplate.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParameterTemplate.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParameterTemplate.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParameterTemplate.java Sat May 13 20:25:56 2006
@@ -0,0 +1,298 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+import java.util.*;
+
+
+/**
+ *	<p>A template that allows parameter substitutions.  Parameters should be
+ *	placed in the template in the form ${param-name}.  Use the 
+ *	{@link #setParameter} method to set the parameter values, which will be
+ *	substituted into the template on calls to {@link #write} and
+ *	{@link #toString}.  If a parameter is encountered that hasn't been set, then
+ *	the parameter key is used to lookup the corresponding System property.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class ParameterTemplate
+{
+	private static final String SEP = System.getProperty ("line.separator");
+
+	private final StringBuffer	_buf 	= new StringBuffer ();
+	private final Map			_params	= new HashMap ();
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (String value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (boolean value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (char value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (double value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (float value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (int value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (long value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (short value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (Object value)
+	{
+		_buf.append (value);
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (InputStream in)
+		throws IOException
+	{
+		return append (new InputStreamReader (in));	
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (Reader reader)
+		throws IOException
+	{
+		BufferedReader buf = new BufferedReader (reader);
+		String line;
+		while ((line = buf.readLine ()) != null)
+			_buf.append (line).append (SEP);		
+		return this;
+	}
+
+
+	/**
+	 *	Add the given value to the internal template.
+	 */
+	public ParameterTemplate append (File file)
+		throws IOException
+	{
+		FileReader reader = new FileReader (file);
+		try 
+		{ 
+			return append (reader); 
+		} 
+		finally 
+		{ 
+			try { reader.close (); } catch (IOException ioe) {}
+		}
+	}
+
+
+	/**
+	 *	Return true if the given parameter has been given a value.
+	 */
+	public boolean hasParameter (String name)
+	{
+		return _params.containsKey (name);
+	}
+
+
+	/**
+	 *	Return the value set for the given parameter.
+	 */
+	public Object getParameter (String name)
+	{
+		return _params.get (name);
+	}
+
+
+	/**
+	 *	Set the value for the given parameter.
+	 */
+	public Object setParameter (String name, Object val)
+	{
+		return _params.put (name, val);
+	}
+
+
+	/**
+	 *	Set the values for all the parameters in the given map.
+	 */
+	public void setParameters (Map params)
+	{
+		_params.putAll (params);
+	}
+
+
+	/**
+	 *	Clear the recorded parameter values.
+	 */
+	public void clearParameters ()
+	{
+		_params.clear ();
+	}
+
+
+	/**
+	 *	Return a copy of the internal value template with all parameters
+	 *	substituted with their current values.
+	 */
+	public String toString ()
+	{
+		if (_buf.length () == 0 || _params.isEmpty ())
+			return _buf.toString ();
+
+		StringBuffer copy = new StringBuffer ();
+		StringBuffer param = null;
+		char ch, last = 0;
+		for (int i = 0; i < _buf.length (); i++)
+		{
+			ch = _buf.charAt (i);
+			if (last == '$' && ch == '{')
+			{
+				copy.deleteCharAt (copy.length () - 1);
+				param = new StringBuffer ();
+			}
+			else if (ch == '}' && param != null)
+			{
+				if (_params.containsKey (param.toString ()))
+					copy.append (_params.get (param.toString ()));
+				else
+					copy.append (System.getProperty (param.toString ()));
+				param = null;			
+			}
+			else if (param != null)
+				param.append (ch);
+			else
+				copy.append (ch);
+
+			last = ch;
+		}
+		return copy.toString ();
+	}
+
+
+	/**
+	 *	Write the internal value template with all parameters
+	 *	substituted with their current values.
+	 */
+	public void write (OutputStream out)
+		throws IOException
+	{
+		write (new OutputStreamWriter (out));
+	}
+
+
+	/**
+	 *	Write the internal value template with all parameters
+	 *	substituted with their current values.
+	 */
+	public void write (Writer writer)
+		throws IOException
+	{
+		writer.write (toString ());
+		writer.flush ();
+	}
+
+
+	/**
+	 *	Write the internal value template with all parameters
+	 *	substituted with their current values.
+	 */
+	public void write (File file)
+		throws IOException
+	{
+		FileWriter writer = new FileWriter (file);
+		try 
+		{ 
+			write (writer); 
+		} 
+		finally 
+		{ 
+			try { writer.close (); } catch (IOException ioe) {}
+		}
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParameterTemplate.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParseException.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParseException.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParseException.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParseException.java Sat May 13 20:25:56 2006
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import org.apache.commons.lang.exception.*;
+
+
+/**
+ *	<p>Exception type for parse errors.</p>
+ *
+ *	@author		Abe White
+ *	@since		4.0
+ *	@nojavadoc
+ */
+public class ParseException
+	extends NestableRuntimeException
+{
+	public ParseException ()
+	{
+	}
+
+
+	public ParseException (String msg)
+	{
+		super (msg);
+	}
+
+
+	public ParseException (Throwable cause)
+	{
+		super (cause);
+	}
+
+
+	public ParseException (String msg, Throwable cause)
+	{
+		super (msg, cause);
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ParseException.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ReferenceSet.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ReferenceSet.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ReferenceSet.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ReferenceSet.java Sat May 13 20:25:56 2006
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+import java.util.*;
+
+import org.apache.commons.collections.set.*;
+import org.apache.commons.collections.map.*;
+
+
+/**
+ *	<p>A set whose values may be stored as weak or soft references.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class ReferenceSet
+	implements Set, Serializable
+{
+	/**
+	 *	Hard reference marker.
+ 	 */
+	public static final int HARD = 0;	
+
+	/**
+	 *	Soft reference marker.
+ 	 */
+	public static final int SOFT = 1;	
+
+	/**
+	 *	Weak reference marker.
+	 */
+	public static final int WEAK = 2;
+
+	private static final Object DUMMY_VAL = new Object ();
+
+	private final Set _set;
+
+
+	/**
+	 *	Construct a set with the given reference type.
+	 */
+	public ReferenceSet (int refType)
+	{
+		if (refType == HARD)
+			_set = new HashSet ();
+		else
+		{
+			int mapRefType = (refType == WEAK) ? ReferenceMap.WEAK 
+				: ReferenceMap.SOFT;
+			_set = MapBackedSet.decorate (new EfficientEmptyReferenceMap 
+				(mapRefType, ReferenceMap.HARD), DUMMY_VAL);
+		}
+	}
+
+
+	public boolean add (Object obj)
+	{
+		return _set.add (obj);
+	}
+
+
+	public boolean addAll (Collection coll)
+	{
+		return _set.addAll (coll);
+	}
+
+
+	public void clear ()
+	{
+		_set.clear ();
+	}
+
+
+	public boolean contains (Object obj)
+	{
+		return _set.contains (obj);
+	}
+
+
+	public boolean containsAll (Collection coll)
+	{
+		return _set.containsAll (coll);
+	}
+
+
+	public boolean isEmpty ()
+	{
+		return _set.isEmpty ();
+	}
+
+
+	public Iterator iterator ()
+	{
+		return _set.iterator ();
+	}
+
+
+	public boolean remove (Object obj)
+	{
+		return _set.remove (obj);
+	}
+
+
+	public boolean removeAll (Collection coll)
+	{
+		return _set.removeAll (coll);
+	}
+
+
+	public boolean retainAll (Collection coll)
+	{
+		return _set.retainAll (coll);
+	}
+
+
+	public int size ()
+	{
+		return _set.size ();
+	}
+
+
+	public Object[] toArray ()
+	{
+		return _set.toArray ();
+	}
+
+
+	public Object[] toArray (Object[] arr)
+	{
+		return _set.toArray (arr);
+	}
+
+
+	public int hashCode ()
+	{
+		return _set.hashCode ();
+	}
+
+
+	public boolean equals (Object obj)
+	{
+		if (this == obj)
+			return true;
+		if (obj instanceof ReferenceSet)
+			obj = ((ReferenceSet) obj)._set;
+		return _set.equals (obj);
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ReferenceSet.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ResourceBundleProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ResourceBundleProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ResourceBundleProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ResourceBundleProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.util.*;
+
+
+/**
+ *	<p>A simple mechanism for looking up ResourceBundle instances
+ *	across different potential sources.</p>
+ *
+ *	@author	Stephen Kim
+ */
+interface ResourceBundleProvider
+{
+	/**
+	 *	Find a ResourceBundle with the given name, locale, and class loader
+	 *	(which may be null).
+	 */
+	public ResourceBundle findResource (String name, Locale locale,
+		ClassLoader loader);
+}
+

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ResourceBundleProvider.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/Services.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/Services.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/Services.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/Services.java Sat May 13 20:25:56 2006
@@ -0,0 +1,231 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+
+/** 
+ *  <p>Utility classes to locate services, as defined in the
+ *  <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
+ *	#Service%20Provider">Jar File Specification</a>.
+ *	Most of the methods in this class can also
+ *  be found in the <em>sun.misc.Service</em> class, but since it is
+ *  undocumented, we cannot rely on its API.</p>
+ *
+ *  <p>Service location for a specified interface is done by searching
+ *  for the resource <em>/META-INF/services/</em><i>service.class.name</i>,
+ *  and loading the resource.</p>
+ *
+ *  <p>Methods in this class that do not declare exceptions will never
+ *  throw Runtime exceptions: exceptions are silently swallowed
+ *  and empty array values are returned.</p>
+ *
+ *  @author  Marc Prud'hommeaux
+ *	@nojavadoc
+ */
+public class Services
+{
+	private static final String PREFIX = "META-INF/services/";
+
+
+	/** 
+	 *  Return an array of Strings of class names of all known
+	 *  service implementors of the specified interface or class. 
+	 */
+	public static String[] getImplementors (Class serviceClass)
+	{
+		return getImplementors (serviceClass, null);
+	}
+
+
+	/** 
+	 *  Return an array of Strings of class names of all known
+	 *  service implementors of the specified interface or class. 
+	 */
+	public static String[] getImplementors (Class serviceClass, 
+		ClassLoader loader)
+	{
+		return getImplementors (serviceClass.getName (), loader);
+	}
+
+
+	/** 
+	 *  Return an array of Strings of class names of all known
+	 *  service implementors of the specified class name (as resolved
+	 *  by the current thread's context class loader).
+	 */
+	public static String[] getImplementors (String serviceName)
+	{
+		return getImplementors (serviceName, null);
+	}
+
+
+	/** 
+	 *  Return an array of Strings of class names of all known
+	 *  service implementors of the specified class name, as resolved
+	 *  by the specified {@link ClassLoader}.
+	 */
+	public static String[] getImplementors (String serviceName,
+		ClassLoader loader)
+	{
+		if (loader == null)
+			loader = Thread.currentThread ().getContextClassLoader ();	
+
+		try
+		{
+			Set resourceList = new TreeSet ();
+			Enumeration resources = loader.getResources (PREFIX + serviceName);
+			while (resources.hasMoreElements ())
+				addResources ((URL)resources.nextElement (), resourceList);
+
+			return (String[]) resourceList.toArray 
+				(new String[resourceList.size ()]);
+		}
+		catch (Exception e)
+		{
+			// silently swallow all exceptions.
+			return new String[0];
+		}
+	}
+
+
+	/** 
+	 *  Parse the URL resource and add the listed class names
+	 *  to the specified Set. Class names are separated by lines.
+	 *  Lines starting with '#' are ignored.
+	 */
+	private static void addResources (URL url, Set set)
+		throws IOException
+	{
+		InputStream in = url.openConnection ().getInputStream ();
+		BufferedReader reader = new BufferedReader (new InputStreamReader (in));
+
+		try
+		{
+			String line;
+			while ((line = reader.readLine ()) != null)
+			{
+				if (line.trim ().startsWith ("#") ||
+					line.trim ().length () == 0)
+					continue;
+
+				StringTokenizer tok = new StringTokenizer (line, "# \t");
+				if (tok.hasMoreTokens ())
+				{
+					String next = tok.nextToken ();
+					if (next != null)
+					{
+						next = next.trim ();
+						if (next.length () > 0 && !next.startsWith ("#"))
+							set.add (next);
+					}
+				}
+			}
+		}
+		finally
+		{
+			reader.close ();
+		}
+	}
+
+
+	public static Class[] getImplementorClasses (Class serviceClass)
+	{
+		return getImplementorClasses (serviceClass.getName (), null);
+	}
+
+
+	public static Class[] getImplementorClasses (Class serviceClass, 
+		ClassLoader loader)
+	{
+		return getImplementorClasses (serviceClass.getName (), loader);
+	}
+
+
+	/** 
+	 *  Return an array of Class objects of all known
+	 *  service implementors of the specified class name (as resolved
+	 *  by the current thread's context class loader).
+	 */
+	public static Class[] getImplementorClasses (String serviceName)
+	{
+		return getImplementorClasses (serviceName, null);
+	}
+
+
+	public static Class[] getImplementorClasses (String serviceName,
+		ClassLoader loader)
+	{
+		try
+		{
+			return getImplementorClasses (serviceName, loader, true); 
+		}
+		catch (Exception cnfe)
+		{
+			// this will never happen with skipmissing
+			return new Class[0];
+		}
+	}
+
+
+	/** 
+	 *  Return an array of Class objects of all known
+	 *  service implementors of the specified class name, as resolved
+	 *  by the specified {@link ClassLoader}.
+	 *
+	 *  @param skipMissing	if true, then ignore classes that cannot
+	 *  					be loaded by the classloader; otherwise,
+	 *  					resolution failures will throw a
+	 *  					{@link ClassNotFoundException}.
+	 */
+	public static Class[] getImplementorClasses (String serviceName,
+		ClassLoader loader, boolean skipMissing)
+		throws ClassNotFoundException
+	{
+		if (loader == null)
+			loader = Thread.currentThread ().getContextClassLoader ();	
+
+		String[] names = getImplementors (serviceName, loader);
+		if (names == null)
+			return new Class[0];
+
+		List classes = new ArrayList (names.length);
+		for (int i = 0; i < names.length; i++)
+		{
+			try
+			{
+				classes.add (Class.forName (names[i], false, loader));
+			}
+			catch (ClassNotFoundException e)
+			{
+				if (!skipMissing)
+					throw e;
+			}
+			catch (UnsupportedClassVersionError ecve)
+			{
+				// #skim - prob should fail regardless 
+				if (!skipMissing)
+					throw ecve;
+			}
+		}
+		return (Class[]) classes.toArray (new Class[classes.size ()]);
+	}
+}
+

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/Services.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleRegex.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleRegex.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleRegex.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleRegex.java Sat May 13 20:25:56 2006
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+/**
+ *  <p>Allows for simple regex style testing of strings.  The wildcard '.'
+ *	is used to represent any single character, while '.*' is used to represent
+ *	any series of 0 or more characters.</p>
+ *
+ *	<p>Examples:<br />
+ *	<code>
+ *	SimpleRegex re = new SimpleRegex ("the qu.ck .* dog", true);
+ *	boolean matches = re.matches ("The quick fox jumped over the lazy dog");
+ *	</code></p>
+ *	@nojavadoc
+ */
+public class SimpleRegex
+{
+	private final String expr;
+	private final boolean caseInsensitive;
+
+
+	public SimpleRegex (String expr, boolean caseInsensitive)
+	{
+		this.caseInsensitive = caseInsensitive;
+
+		// If we're case insensitive, toLowerCase the expr.  We'll toLowerCase
+		// each target, too, in the matches call.
+		if (caseInsensitive)
+			this.expr = expr.toLowerCase ();
+		else
+			this.expr = expr;
+	}
+
+
+	public boolean matches (String target)
+	{
+		// If we're case insensitive, toLowerCase the target
+		if (caseInsensitive)
+			target = target.toLowerCase ();
+
+		// By default, we are not position independent ("mobile"). We only
+		// become position independent once we hit our first ".*".
+		boolean mobile = false;
+
+		// Find occurrences of ".*" in the expression.
+		int exprPos = 0;
+		int targetPos = 0;
+		while (true)
+		{
+			// Find the next occurrence of ".*"
+			int star = expr.indexOf (".*", exprPos);
+
+			// If we're at a ".*" now, simply skip it and become position
+			// independent
+			if (star == exprPos)
+			{
+				mobile = true;
+				exprPos += 2;
+				continue;
+			}
+			// If there are no more ".*"s, then we're effectively no longer
+			// position independent (assuming we even were before), since
+			// we must match the end of the string
+			else if (star == -1)
+			{
+				int len = expr.length () - exprPos;
+
+				// If we're not mobile, then the remainder of the expr and
+				// target must be the same length.  If not, then it's not a
+				// match.  If we're mobile, then the length doesn't have to
+				// be the same as long as the remainder of the expression
+				// is equal to the end of the target
+				if (!mobile && targetPos != target.length () - len)
+					return false;
+
+				// Match the end of the target to the remainder of the
+				// expression
+				int match = indexOf (target, target.length () - len, exprPos, 
+					len, true);
+				if (match != -1)
+					return true;
+				return false;
+			}
+
+			// Match the fragment of the expression to the target
+			int match = indexOf (target, targetPos, exprPos,
+				star - exprPos, !mobile);
+			if (match == -1)
+				return false;
+			targetPos = match + star - exprPos;
+			exprPos = star + 2;
+			mobile = true;
+		}
+	}
+
+
+	/**
+	 *	Match a section of target to a fragment of the expression.
+	 *	If we're only to match the beginning of the target, beginOnly
+	 *	will be true, otherwise we can match anymore in the target (starting
+	 *	at the targetStart position).  A "." in the expression matches any
+	 *	character.
+	 */
+	private int indexOf (String target, int targetStart,
+		int exprStart, int exprLength, boolean beginOnly)
+	{
+		// Run through the target seeing if there is a match
+		while (target.length () - targetStart >= exprLength)
+		{
+			// Assume success.  If there isn't a match we'll break out
+			boolean found = true;
+			for (int i = 0; i < exprLength; i++)
+			{
+				// "." in the expr matches any character in the target
+				if (expr.charAt (exprStart + i) != '.' && 
+					expr.charAt (exprStart + i) != 
+					target.charAt (targetStart + i))
+				{
+					found = false;
+					break;
+				}
+			}
+			if (found)
+				return targetStart;
+
+			// If we're position dependent (beginOnly == true), then don't
+			// continue the search
+			if (beginOnly)
+				return -1;
+
+			targetStart++;
+		}
+		return -1;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleRegex.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleResourceBundleProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleResourceBundleProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleResourceBundleProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleResourceBundleProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.util.*;
+
+
+/**
+ *	<p>{@link ResourceBundleProvider} that uses Java's built-in resource
+ *	bundle lookup methods.</p>
+ *
+ *	@author	Abe White
+ */
+class SimpleResourceBundleProvider
+	implements ResourceBundleProvider
+{
+	public ResourceBundle findResource (String name, Locale locale, 
+		ClassLoader loader)
+	{
+		ResourceBundle bundle = null;
+		if (loader != null)
+		{
+			try
+			{
+				bundle = ResourceBundle.getBundle (name, locale, loader);
+			}
+			catch (Throwable t)
+			{
+			}
+		}
+
+		// try with the default class loader
+		if (bundle == null)
+		{
+			try
+			{
+				bundle = ResourceBundle.getBundle (name, locale);
+			}
+			catch (Throwable t)
+			{
+			}
+		}
+
+		return bundle;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/SimpleResourceBundleProvider.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StreamResourceBundleProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StreamResourceBundleProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StreamResourceBundleProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StreamResourceBundleProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+import java.util.*;
+
+
+/**
+ *	<p>{@link ResourceBundleProvider} that uses the 
+ *	{@link ClassLoader#getResourceAsStream} method to load resources.  
+ *	Created for use under WSAD.</p>
+ *
+ *	@author	Stephen Kim
+ */
+class StreamResourceBundleProvider
+	implements ResourceBundleProvider
+{
+	public ResourceBundle findResource (String name, Locale locale, 
+		ClassLoader loader)
+	{
+		String rsrc = name.replace ('.', '/') + ".properties";
+		if (loader == null)
+			loader = Thread.currentThread ().getContextClassLoader ();
+
+		InputStream in = loader.getResourceAsStream (rsrc);
+		if (in != null)
+		{
+			try
+			{
+				return new PropertyResourceBundle (in);
+			}
+			catch (Exception e)
+			{
+			}
+			finally
+			{
+				try { in.close (); } catch (IOException ioe) {}	
+			}
+		}
+		return null;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StreamResourceBundleProvider.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StringDistance.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StringDistance.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StringDistance.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StringDistance.java Sat May 13 20:25:56 2006
@@ -0,0 +1,216 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+import java.util.*;
+
+
+/** 
+ *  Utilities for calculating string distance.
+ *  
+ *  @author  Marc Prud'hommeaux
+ *	@nojavadoc
+ */
+public class StringDistance
+{
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string.
+	 *  
+	 *  @see 	#getClosestLevenshteinDistance(String,Collection,int)
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		String[] candidates)
+	{
+		if (candidates == null)
+			return null;
+		return getClosestLevenshteinDistance (str, Arrays.asList (candidates));
+	}
+
+
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string.
+	 *  
+	 *  @see 	#getClosestLevenshteinDistance(String,Collection,int)
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		Collection candidates)
+	{
+		return getClosestLevenshteinDistance (str, candidates,
+			Integer.MAX_VALUE);
+	}
+
+
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string.
+	 *  
+	 *  @see 	#getClosestLevenshteinDistance(String,Collection,int)
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		String[] candidates, int threshold)
+	{
+		if (candidates == null)
+			return null;
+		return getClosestLevenshteinDistance (str, Arrays.asList (candidates),
+			threshold);
+	}
+
+
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string and using the threshold as the specified
+	 *	percentage of the length of the candidate string (0.0f-1.0f).
+	 *  
+	 *  @see 	#getClosestLevenshteinDistance(String,Collection,int)
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		String[] candidates, float thresholdPercentage)
+	{
+		if (candidates == null)
+			return null;
+
+		return getClosestLevenshteinDistance (str, Arrays.asList (candidates),
+			thresholdPercentage);
+	}
+
+
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string and using the threshold as the specified
+	 *	percentage of the length of the candidate string (0.0f-1.0f).
+	 *  
+	 *  @see 	#getClosestLevenshteinDistance(String,Collection,int)
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		Collection candidates, float thresholdPercentage)
+	{
+		if (str == null)
+			return null;
+
+		thresholdPercentage = Math.min (thresholdPercentage, 1.0f);
+		thresholdPercentage = Math.max (thresholdPercentage, 0.0f);
+
+		return getClosestLevenshteinDistance (str, candidates,
+			(int)(str.length () * thresholdPercentage));
+	}
+
+
+	/** 
+	 *  Returns the candidate string with the closest Levenshtein distance
+	 *	to the given string.
+	 *  
+	 *  @param  str 		the string to check
+	 *  @param  candidates  the list of strings to test against
+	 *	@param	threshold	the threshold distance a candidate must meet
+	 *
+	 *  @see 	#getLevenshteinDistance
+	 */
+	public static String getClosestLevenshteinDistance (String str,
+		Collection candidates, int threshhold)
+	{
+		if (candidates == null || candidates.isEmpty ())
+			return null;
+
+		String minString = null;
+		int minValue = Integer.MAX_VALUE;
+
+		for (Iterator i = candidates.iterator (); i.hasNext (); )
+		{
+			String candidate = (String) i.next ();
+			int distance = getLevenshteinDistance (str, candidate);
+			if (distance < minValue)
+			{
+				minValue = distance;
+				minString = candidate;
+			}
+		}
+
+		// return the lowest close string only if we surpass the threshhold
+		if (minValue <= threshhold)
+			return minString;
+		else
+			return null;
+	}
+
+
+	/** 
+	 *  Returns the Levenshtein distance between the two strings.
+	 *  The distance is the minimum number of changes that need to be
+	 *  applied to the first string in order to get to the second
+	 *  string. For details of the algorithm, see
+	 *  <a href="http://en.wikipedia.org/wiki/Levenshtein_distance">
+	 *  http://en.wikipedia.org/wiki/Levenshtein_distance</a>.
+	 */
+	public static int getLevenshteinDistance (String s, String t)
+	{
+		int n = s.length ();
+		int m = t.length ();
+
+		if (n == 0)
+			return m;
+
+		if (m == 0)
+			return n;
+
+		int[][] matrix = new int[n+1][m+1];
+
+		for (int i = 0; i <= n; i++)
+			matrix[i][0] = i;
+
+		for (int j = 0; j <= m; j++)
+			matrix[0][j] = j;
+
+		for (int i = 1; i <= n; i++)
+		{
+			int si = s.charAt (i - 1);
+
+			for (int j = 1; j <= m; j++)
+			{
+				int tj = t.charAt (j - 1);
+
+				int cost;
+
+				if (si == tj)
+					cost = 0;
+				else
+					cost = 1;
+
+				matrix[i][j] = min (matrix[i-1][j]+1,
+					matrix[i][j-1]+1,
+					matrix[i-1][j-1] + cost);
+			}
+		}
+
+		return matrix[n][m];
+	}
+
+
+	private static int min (int a, int b, int c)
+	{
+		int mi = a;
+
+		if (b < mi)
+			mi = b;
+
+		if (c < mi)
+			mi = c;
+
+		return mi;
+	}
+}
+

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/StringDistance.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TemporaryClassLoader.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TemporaryClassLoader.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TemporaryClassLoader.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TemporaryClassLoader.java Sat May 13 20:25:56 2006
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+
+import serp.bytecode.lowlevel.*;
+
+
+/** 
+ *  <p>ClassLoader implementation that allows classes to be temporarily
+ *  loaded and then thrown away. Useful for the enhancer to be able
+ *  to run against a class without first loading (and thus polluting)
+ *  the parent ClassLoader.</p>
+ *  
+ *  @author  Marc Prud'hommeaux
+ *	@nojavadoc
+ */
+public class TemporaryClassLoader
+	extends ClassLoader
+{
+	public TemporaryClassLoader (ClassLoader parent)
+	{
+		super (parent);
+	}
+
+
+	public Class loadClass (String name)
+		throws ClassNotFoundException
+	{
+		return loadClass (name, false);
+	}
+
+
+	protected Class loadClass (String name, boolean resolve)
+		throws ClassNotFoundException
+	{
+		// see if we've already loaded it
+		Class c = findLoadedClass (name);
+		if (c != null)
+			return c;
+
+		// bug #283. defer to system if the name is a protected name.
+		// "sun." is required for JDK 1.4, which has an access check for
+		// sun.reflect.GeneratedSerializationConstructorAccessor1
+		if (name.startsWith ("java.") || name.startsWith ("javax.")
+			|| name.startsWith ("sun."))
+			return Class.forName (name, resolve, getClass ().getClassLoader ());
+
+		String resourceName = name.replace ('.', '/') + ".class";
+		InputStream resource = getResourceAsStream (resourceName);
+		if (resource == null)
+			throw new ClassNotFoundException (name);
+
+		ByteArrayOutputStream bout = new ByteArrayOutputStream ();
+		byte[] b = new byte[1024];
+		try
+		{
+			for (int n = 0; (n = resource.read (b, 0, b.length)) != -1;
+				bout.write (b, 0, n));
+			byte[] classBytes = bout.toByteArray ();
+			if (isAnnotation (classBytes))
+				return Class.forName (name, resolve, getClass ().
+					getClassLoader ());
+
+			try
+			{
+				return defineClass (name, classBytes, 0, classBytes.length);
+			}
+			catch (SecurityException e)
+			{
+				// possible prohibited package: defer to the parent
+				return super.loadClass (name, resolve);
+			}
+		}
+		catch (IOException ioe)
+		{
+			// defer to the parent
+			return super.loadClass (name, resolve);
+		}
+	}
+
+
+	/**
+	 *	Fast-parse the given class bytecode to determine if it is an
+	 *	annotation class.
+	 */
+	private static boolean isAnnotation (byte[] b)
+	{
+		if (JavaVersions.VERSION < 5)
+			return false;
+		int idx = ConstantPoolTable.getEndIndex (b);
+		int access = ConstantPoolTable.readUnsignedShort (b, idx);
+		return (access & 0x2000) != 0;	// access constant for annotation type
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TemporaryClassLoader.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ThreadLock.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ThreadLock.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ThreadLock.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ThreadLock.java Sat May 13 20:25:56 2006
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+
+
+/**
+ *	<p>Lock implementation.  The lock is aware of the thread that owns it, and 
+ *	allows that thread to {@link #lock} multiple times without blocking.  Only
+ *	the owning thread can {@link #unlock}, and the lock will not be released to
+ *	other threads until {@link #unlock} has been called the same number of 
+ *	times as {@link #lock}.</p>
+ *
+ *	<p>Using this lock is similar to synchronizing on an object, but is more
+ *	flexible (for example, the calls to {@link #lock} and {@link #unlock} can
+ *	be surrounded by if statements).</p>
+ *
+ *	<p>Note that the lock resets on serialization.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class ThreadLock
+	implements Serializable
+{
+	private transient int		_count	= 0;
+	private transient Thread 	_owner 	= null;
+
+	
+	/**
+	 *	Atomically lock.  Blocks until the lock is available.
+	 */
+	public void lock ()
+	{
+		Thread thread = Thread.currentThread ();
+		synchronized (this)
+		{
+			if (thread == _owner)
+				_count++;
+			else
+			{
+				while (_owner != null)
+					try { wait (); } catch (InterruptedException ie) {}
+				_count = 1;
+				_owner = thread;
+			}
+		}
+	}
+
+
+	/**
+	 *	Atomically lock.  Blocks until the lock is available or a timeout
+	 *	occurs.
+	 *
+	 *	@para	timeout		the number of milliseconds to wait before timing out
+	 *	@return				true if the lock was obtained, false on timeout
+	 */
+	public boolean lock (long timeout)
+	{
+		// use version that doesn't need to check time; more efficient
+		if (timeout == 0)
+		{
+			lock ();
+			return true;
+		}
+
+		Thread thread = Thread.currentThread ();
+		synchronized (this)
+		{
+			if (thread == _owner)
+			{
+				_count++;
+				return true;
+			}
+			if (_owner == null)
+			{
+				_count = 1;
+				_owner = thread;
+				return true;
+			}
+
+			long time = System.currentTimeMillis ();
+			long end = time + timeout;
+			while (_owner != null && time < end)
+			{
+				try { wait (end - time); } catch (InterruptedException ie) {}
+				time = System.currentTimeMillis ();
+			}
+			if (_owner == null)
+			{
+				_count = 1;
+				_owner = thread;
+				return true;
+			}
+			return false;
+		}
+	}
+
+
+	/**
+	 *	Releases the lock.  This method can only be called by the owning
+	 *	thread.
+	 *
+	 *	@throws	IllegalStateException if current thread is not owner
+	 */
+	public void unlock ()
+	{
+		Thread thread = Thread.currentThread ();
+		synchronized (this)
+		{
+			if (thread != _owner)
+				throw new IllegalStateException ();
+		
+			_count--;
+			if (_count == 0)
+			{
+				_owner = null;
+				notify ();
+			}
+		}
+	}
+
+
+	/**
+	 *	Return true if this lock is locked by the current thread.
+	 */
+	public boolean isLocked ()
+	{
+		Thread thread = Thread.currentThread ();
+		synchronized (this)
+		{
+			if (thread != _owner)
+				return false;
+			return _count > 0;
+		}
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ThreadLock.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TypedProperties.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TypedProperties.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TypedProperties.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TypedProperties.java Sat May 13 20:25:56 2006
@@ -0,0 +1,391 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.util.*;
+
+
+/**
+ *	<p>A specialization of the {@link Properties} map type with added
+ *	convenience methods to retrieve	and set options as primitive values.  
+ *	The internal representation of all data is kept in string form.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class TypedProperties
+	extends Properties
+{
+	/**
+	 *	Default constructor.
+	 */
+	public TypedProperties ()
+	{
+		super ();
+	}
+
+
+	/**
+	 *	Construct the properties instance with the given set of defaults.
+	 *
+	 *	@see	Properties#Properties(Properties)
+	 */
+	public TypedProperties (Properties defaults)
+	{
+		super (defaults);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a boolean, or false if 
+	 *	it does not exist and has no set default.
+	 */
+	public boolean getBooleanProperty (String key)
+	{
+		return getBooleanProperty (key, false);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a boolean, or the given
+	 *	default if it does not exist.
+	 */
+	public boolean getBooleanProperty (String key, boolean def)
+	{
+		String val = getProperty (key);
+		if (val == null)
+			return def;
+		return "t".equalsIgnoreCase (val) || "true".equalsIgnoreCase (val);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a float, or 0 if 
+	 *	it does not exist and has no set default.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public float getFloatProperty (String key)
+	{
+		return getFloatProperty (key, 0F);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a float, or the given
+	 *	default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public float getFloatProperty (String key, float def)
+	{
+		String val = getProperty (key);
+		return (val == null) ? def : Float.parseFloat (val);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a double, or 0 if 
+	 *	it does not exist and has no set default.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public double getDoubleProperty (String key)
+	{
+		return getDoubleProperty (key, 0D);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a double, or the given
+	 *	default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public double getDoubleProperty (String key, double def)
+	{
+		String val = getProperty (key);
+		return (val == null) ? def : Double.parseDouble (val);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a long, or 0 if 
+	 *	it does not exist and has no set default.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public long getLongProperty (String key)
+	{
+		return getLongProperty (key, 0L);
+	}
+
+
+	/**
+	 *	Return the property under the given key as a double, or the given
+	 *	default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public long getLongProperty (String key, long def)
+	{
+		String val = getProperty (key);
+		return (val == null) ? def : Long.parseLong (val);
+	}
+
+
+	/**
+	 *	Return the property under the given key as an int, or 0 if 
+	 *	it does not exist and has no set default.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public int getIntProperty (String key)
+	{
+		return getIntProperty (key, 0);
+	}
+
+
+	/**
+	 *	Return the property under the given key as an int, or the given
+	 *	default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public int getIntProperty (String key, int def)
+	{
+		String val = getProperty (key);
+		return (val == null) ? def : Integer.parseInt (val);
+	}
+
+
+	/**
+	 *	Overrides {@link Properties#setProperty(String,String)} to remove
+	 *	the key if the given value is <code>null</code>.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public Object setProperty (String key, String val)
+	{
+		if (val == null)
+			return remove (key);
+		return super.setProperty (key, val);
+	}
+
+
+	/**
+	 *	Set the given key to a string version of the given value.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public void setProperty (String key, boolean val)
+	{
+		setProperty (key, String.valueOf (val));
+	}
+
+
+	/**
+	 *	Set the given key to a string version of the given value.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public void setProperty (String key, double val)
+	{
+		setProperty (key, String.valueOf (val));
+	}
+
+
+	/**
+	 *	Set the given key to a string version of the given value.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public void setProperty (String key, float val)
+	{
+		setProperty (key, String.valueOf (val));
+	}
+
+
+	/**
+	 *	Set the given key to a string version of the given value.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public void setProperty (String key, int val)
+	{
+		setProperty (key, String.valueOf (val));
+	}
+
+
+	/**
+	 *	Set the given key to a string version of the given value.
+	 *
+	 *	@see	Properties#setProperty(String,String)
+	 */
+	public void setProperty (String key, long val)
+	{
+		setProperty (key, String.valueOf (val));
+	}
+
+	
+	/**
+	 *	Remove the given property.
+	 */
+	public String removeProperty (String key)
+	{
+		Object val = remove (key);
+		return (val == null) ? null : val.toString ();
+	}
+
+
+	/**
+	 *	Remove the given property, or return the given default if it does
+	 *	not exist.
+	 */
+	public String removeProperty (String key, String def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeProperty (key);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a boolean.
+	 */
+	public boolean removeBooleanProperty (String key)
+	{
+		String val = removeProperty (key);
+		return "t".equalsIgnoreCase (val) || "true".equalsIgnoreCase (val);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a boolean, or return the 
+	 *	given default if it does not exist.
+	 */
+	public boolean removeBooleanProperty (String key, boolean def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeBooleanProperty (key);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a double.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public double removeDoubleProperty (String key)
+	{
+		String val = removeProperty (key);
+		return (val == null) ? 0D : Double.parseDouble (val);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a double, or return the 
+	 *	given default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public double removeDoubleProperty (String key, double def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeDoubleProperty (key);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a float.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public float removeFloatProperty (String key)
+	{
+		String val = removeProperty (key);
+		return (val == null) ? 0F : Float.parseFloat (val);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a float, or return the 
+	 *	given default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public float removeFloatProperty (String key, float def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeFloatProperty (key);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a int.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public int removeIntProperty (String key)
+	{
+		String val = removeProperty (key);
+		return (val == null) ? 0 : Integer.parseInt (val);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a int, or return the 
+	 *	given default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public int removeIntProperty (String key, int def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeIntProperty (key);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a long.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public long removeLongProperty (String key)
+	{
+		String val = removeProperty (key);
+		return (val == null) ? 0L : Long.parseLong (val);
+	}
+
+
+	/**
+	 *	Remove the property under the given key as a long, or return the 
+	 *	given default if it does not exist.
+	 *
+	 *	@throws		NumberFormatException on parse error
+	 */
+	public long removeLongProperty (String key, long def)
+	{
+		if (!containsKey (key))
+			return def;
+		return removeLongProperty (key);
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/TypedProperties.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ZipResourceBundleProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ZipResourceBundleProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ZipResourceBundleProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ZipResourceBundleProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.util;
+
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+
+/**
+ *	<p>{@link ResourceBundleProvider} that expects the
+ *	{@link ClassLoader#getResourceAsStream} method to return a zipped input
+ *	stream.  Created for use under Weblogic RARs.</p>
+ *
+ *	@author	Patrick Linskey
+ */
+class ZipResourceBundleProvider
+	implements ResourceBundleProvider
+{
+	public ResourceBundle findResource (String name, Locale locale, 
+		ClassLoader loader)
+	{
+		String rsrc = name.replace ('.', '/') + ".properties";
+		if (loader == null)
+			loader = Thread.currentThread ().getContextClassLoader ();
+
+		InputStream in = loader.getResourceAsStream (rsrc);
+		if (in == null)
+			return null;
+
+		ZipInputStream zip = new ZipInputStream (in);
+		ResourceBundle bundle = null;
+		try
+		{
+			ZipEntry ze;
+			while (true)
+			{
+				ze = zip.getNextEntry ();
+				if (ze == null)
+					break;
+
+				if (rsrc.equals (ze.getName ()))
+					return new PropertyResourceBundle (zip);
+
+				zip.closeEntry ();				
+			}
+		}
+		catch (Exception e)
+		{
+		}
+		finally
+		{
+			try { zip.close (); } catch (IOException ioe) {}
+		}
+		return null;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/ZipResourceBundleProvider.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/package.html
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/package.html?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/package.html (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/package.html Sat May 13 20:25:56 2006
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+<html>
+<body>
+	<p><strong>General Utilities</strong></p>
+</body>
+</html>

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/util/package.html
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Commentable.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Commentable.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Commentable.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Commentable.java Sat May 13 20:25:56 2006
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+
+/**
+ *	Implementations of this interface can be adorned with comments.
+ *
+ *	@since 3.3
+ *	@nojavadoc
+ */
+public interface Commentable
+{
+	public static final String[] EMPTY_COMMENTS = new String[0];
+
+
+	/**
+	 *	Set comments.
+	 */
+	public void setComments (String[] comments);
+
+
+	/**
+	 *	Return comments, or empty array if none.
+	 */
+	public String[] getComments ();
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Commentable.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/DocTypeReader.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/DocTypeReader.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/DocTypeReader.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/DocTypeReader.java Sat May 13 20:25:56 2006
@@ -0,0 +1,328 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+
+import java.io.*;
+
+
+/**
+ *	<p>The DocTypeReader can be used to dynamically include a 
+ *	<code>DOCTYPE</code> declaration in an XML stream.  Often it is 
+ *	inconvenient to specify a <code>DOCTYPE</code> in XML files -- you many 
+ *	want the option of parsing the files without reading the DTD, the files 
+ *	may move around, making placing a <code>DOCTYPE</code> path to the DTD in 
+ *	them unattractive, and you may have many files, making an in-line include 
+ *	of the DTD unattractive as well.  This class makes
+ *	it possible to maintain XML files without any <code>DOCTYPE</code>
+ *	declaration, then dynamically include the <code>DOCTYPE</code> information
+ *	at runtime.</p>
+ *
+ *	<p>If the XML stream already contains a <code>DOCTYPE</code> declaration,
+ *	the reader will not add an additional one.</p>
+ *
+ *	<p>The <code>DOCTYPE</code> information given to the reader will be placed
+ *	in the XML stream it wraps just before the root element of the document.</p>
+ *
+ *	<p>Note that all methods other than the various forms of <code>read</code>
+ *	apply onto the underlying XML stream and should not be used until the 
+ *	header and doc type have been read.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */	
+public class DocTypeReader
+	extends Reader
+{
+	private Reader 	_xml		= null;
+	private Reader	_docType	= null;
+
+	// use to hold all header information until the doctype dec should be 
+	// inserted
+	private char[]	_header 	= null;
+	private int		_headerPos	= 0;
+
+
+	/**
+	 *	Construct the reader with an XML stream, and set the 
+	 *	<code>DOCTYPE</code> information to be included.  The given
+	 *	reader should access an input source containing the exact declaration
+	 *	to include, such as:<br />
+	 *	<code>&lt;DOCTYPE schedule SYSTEM "schedule.dtd"&gt;</code><br />
+	 *	<code>&lt;DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...&gt;</code><br />
+	 *	<code>&lt;DOCTYPE stock-price [ &lt;ELEMENT symb ... ]&gt;</code><br />
+ 	 *	If the reader is null, no <code>DOCTYPE</code> information will be
+ 	 *	included in the stream.
+	 */
+	public DocTypeReader (Reader xml, Reader docType)
+		throws IOException
+	{
+		_docType = docType;
+		_xml = bufferHeader (xml);
+	}
+
+
+	public int read ()
+		throws IOException
+	{
+		int ch = readHeader ();
+		if (ch != -1)
+			return ch;
+
+		ch = readDocType ();
+		if (ch != -1)
+			return ch;
+
+		return _xml.read ();
+	}
+
+
+	public int read (char[] buf)
+		throws IOException
+	{
+		return read (buf, 0, buf.length);
+	}
+
+
+	public int read (char[] buf, int off, int len)
+		throws IOException
+	{
+		int headerRead = readHeader (buf, off, len);
+		off += headerRead;
+		len -= headerRead;
+
+		int docRead = readDocType (buf, off, len);
+		off += docRead;
+		len -= docRead;
+
+		return headerRead + docRead + _xml.read (buf, off, len);
+	}
+
+
+	public long skip (long len)
+		throws IOException
+	{
+		return _xml.skip (len);
+	}
+
+
+	public boolean ready ()
+		throws IOException
+	{
+		return _xml.ready ();
+	}
+
+
+	public boolean markSupported ()
+	{
+		return _xml.markSupported ();
+	}
+
+
+	public void mark (int readAheadLimit)
+		throws IOException
+	{
+		_xml.mark (readAheadLimit);
+	}
+
+
+	public void reset ()
+		throws IOException
+	{
+		_xml.reset ();
+	}
+
+
+	public void close ()
+		throws IOException
+	{
+		_xml.close ();
+		if (_docType != null)
+			_docType.close ();
+	}
+
+
+	/**
+	 *	Buffer all text until the doc type declaration should be inserted.
+	 */
+	private Reader bufferHeader (Reader origXML)
+		throws IOException
+	{
+		// don't bother if no doc type declaration
+		if (_docType == null)
+		{
+			_header = new char[0];
+			return origXML;
+		}
+
+		// create buffer
+		StringWriter writer = new StringWriter ();
+		PushbackReader xml = new PushbackReader (origXML, 3);
+		int ch, ch2, ch3;
+		boolean comment;
+
+		while (true)
+		{
+			// read leading space
+			for (ch = xml.read (); ch != -1 
+				&& Character.isWhitespace ((char) ch); ch = xml.read ())
+				writer.write (ch);
+			if (ch == -1)
+				return headerOnly (writer.toString ());
+
+			// if not XML, finish
+			if (ch != '<')
+			{
+				xml.unread (ch);
+				_header = writer.toString ().toCharArray ();
+				return xml;
+			}
+		
+			// if the root element, finish
+			ch = xml.read ();
+			if (ch != '?' && ch != '!')
+			{
+				xml.unread (ch);
+				xml.unread ('<');
+				_header = writer.toString ().toCharArray ();
+				return xml;
+			}
+
+			// if a doc type element, finish
+			ch2 = xml.read ();
+			if (ch == '!' && ch2 == 'D')
+			{
+				xml.unread (ch2);	
+				xml.unread (ch);	
+				xml.unread ('<');	
+				_header = writer.toString ().toCharArray ();
+				_docType = null;	// make sure doc type not included
+				return xml;
+			}
+		
+			// is this a comment?
+			ch3 = xml.read ();
+			comment = ch == '!' && ch2 == '-' && ch3 == '-';
+
+			// place everything read into the header material
+			writer.write ('<');
+			writer.write (ch);
+			writer.write (ch2);
+			writer.write (ch3);
+
+			// read until the next '>' or '-->' if a comment
+			ch2 = 0;
+			ch3 = 0;
+			while ((ch = xml.read ()) != -1)
+			{
+				writer.write (ch);
+
+				if ((!comment && ch == '>')
+					|| (comment && ch == '>' && ch2 == '-' && ch3 == '-'))
+					break;
+			
+				// track last two chars so we can tell if comment is ending
+				ch3 = ch2;
+				ch2 = ch;
+			}
+			if (ch == -1)
+				return headerOnly (writer.toString ());
+
+			// read the space after the declaration
+			for (ch = xml.read (); ch != -1 
+				&& Character.isWhitespace ((char) ch); ch = xml.read ())
+				writer.write (ch);
+			if (ch == -1)
+				return headerOnly (writer.toString ());
+			xml.unread (ch);
+		}
+	}
+
+
+	/**
+	 *	If the stream contained only space, think of it as pure XML with no
+	 *	header for consistency with the other methods.
+	 */
+	private Reader headerOnly (String header)
+	{
+		_header = new char[0];
+		_docType = null;
+		return new StringReader (header);
+	}
+
+
+	/**
+	 *	Return a single character from the buffered header, or -1 if none.
+	 */
+	private int readHeader ()
+	{
+		if (_headerPos == _header.length)
+			return -1;
+		return _header[_headerPos++];
+	}
+
+
+	/**
+	 *	Read from the buffered header to the given array, returning the
+	 *	number of characters read.
+	 */
+	private int readHeader (char[] buf, int off, int len)
+	{
+		int read = 0;
+		for (; len > 0 && _headerPos < _header.length; read++, off++, len--)
+			buf[off] = _header[_headerPos++];
+
+		return read;
+	}
+
+
+	/**
+	 *	Return a single character from the doc type declaration, or -1
+	 *	if none.
+	 */
+	private int readDocType ()
+		throws IOException
+	{
+		if (_docType == null)
+			return -1;
+
+		int ch = _docType.read ();
+		if (ch == -1)
+			_docType = null;
+
+		return ch;
+	}
+
+
+	/**
+	 *	Read from the doc type declaration to the given array, returning the
+	 *	number of characters read.
+	 */
+	private int readDocType (char[] buf, int off, int len)
+		throws IOException
+	{
+		if (_docType == null)
+			return 0;
+
+		int read = _docType.read (buf, off, len);
+		if (read < len)
+			_docType = null;
+		if (read == -1)
+			read = 0;
+
+		return read;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/DocTypeReader.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Location.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Location.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Location.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Location.java Sat May 13 20:25:56 2006
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+import java.text.*;
+import org.xml.sax.*;
+
+import org.apache.openjpa.lib.util.*;
+
+import serp.util.*;
+
+
+/**
+ *	@author		Stephen Kim
+ *	@nojavadoc
+ */
+public class Location
+{
+	private static final Localizer _loc = Localizer.forPackage (Location.class);
+	
+	private boolean _nullOnNoLocator = false;
+	private Locator	_locator	= null;
+
+	public Location ()
+	{
+		this (false);
+	}
+	
+	public Location (boolean nullOnNoLocator)
+	{
+		_nullOnNoLocator = nullOnNoLocator;
+	}
+
+	/**
+	 * for customized responses
+	 */ 
+	public String getLocation (String format)
+	{
+		if (_locator == null)
+		{
+			if (_nullOnNoLocator)
+				return null;
+			return _loc.get ("no-locator");
+		}
+		String forma = MessageFormat.format (format, new Object [] {
+			Numbers.valueOf (_locator.getLineNumber ()),
+			Numbers.valueOf (_locator.getColumnNumber ()),
+			_locator.getPublicId (),
+			_locator.getSystemId ()});
+		return forma;
+	}
+
+	public String getLocation ()
+	{
+		return getLocation (_loc.get ("location-format"));
+	}
+
+	public void setLocator (Locator locator)
+	{
+		_locator = locator;
+	}
+
+	public Locator getLocator ()
+	{
+		return _locator;
+	}
+
+	public void setNullOnNoLocator (boolean val)
+	{
+		_nullOnNoLocator = val;
+	}
+
+	public boolean isNullOnNoLocator ()
+	{
+		return _nullOnNoLocator;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/Location.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/ValidatingErrorHandler.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/ValidatingErrorHandler.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/ValidatingErrorHandler.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/ValidatingErrorHandler.java Sat May 13 20:25:56 2006
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+
+import org.xml.sax.*;
+
+
+/**
+ *	<p>ErrorHandler implementation which overrides the default 
+ *	behavior of ignoring parse errors to throw a {@link SAXException} instead.
+ *	This handler is used by the validating parsers of the 
+ *	{@link XMLFactory}.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class ValidatingErrorHandler
+	implements ErrorHandler
+{
+	public void warning (SAXParseException e)
+		throws SAXException
+	{
+		throw e;
+	}
+
+
+	public void error (SAXParseException e)
+		throws SAXException
+	{
+		throw e;
+	}
+
+
+	public void fatalError (SAXParseException e)
+		throws SAXException
+	{
+		throw e;
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/ValidatingErrorHandler.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLFactory.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLFactory.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLFactory.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLFactory.java Sat May 13 20:25:56 2006
@@ -0,0 +1,250 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+
+import javax.xml.parsers.*;
+import javax.xml.transform.*;
+import javax.xml.transform.sax.*;
+
+import org.apache.commons.lang.exception.*;
+import org.w3c.dom.*;
+import org.xml.sax.*;
+
+
+/**
+ *	<p>The XMLFactory produces validating and non-validating DOM level 2
+ *	and SAX level 2 parsers and XSL transformers through JAXP.  It uses 
+ *	caching to avoid repeatedly paying the relatively expensive runtime costs
+ *	associated with resolving the correct XML implementation through the
+ *	JAXP configuration mechanisms.</p>
+ *	
+ *	@author		Abe White
+ *	@nojavadoc
+ */
+public class XMLFactory
+{
+	// cache parsers and transformers in all possible configurations
+	private static SAXParserFactory[] 		_saxFactories	= null;
+	private static DocumentBuilderFactory[]	_domFactories 	= null;
+	private static TransformerFactory		_transFactory	= null;
+	private static ErrorHandler				_validating;
+	static
+	{
+		_saxFactories = new SAXParserFactory[4];
+		_domFactories = new DocumentBuilderFactory[4];
+		_validating = new ValidatingErrorHandler ();
+	}
+
+
+	/**
+	 *	Return a DocumentBuilder with the specified configuration.
+	 */
+	public static DocumentBuilder getDOMParser (boolean validating, 
+		boolean namespaceAware)
+	{
+		DocumentBuilder db = checkDOMCache (validating, namespaceAware);
+		if (validating)
+			db.setErrorHandler (_validating);
+
+		return db;
+	}
+
+
+	/**
+	 *	Return a new DOM Document.
+	 */
+	public static Document getDocument ()
+	{
+		DocumentBuilder db = checkDOMCache (false, false);
+		return db.newDocument ();
+	}
+
+
+	/**
+	 *	Return a SAXParser with the specified configuration.
+	 */
+	public static SAXParser getSAXParser (boolean validating, 
+		boolean namespaceAware)
+	{
+		SAXParser sp = checkSAXCache (validating, namespaceAware);
+		if (validating)
+		{
+			try
+			{
+				sp.getXMLReader ().setErrorHandler (_validating);
+			}
+			catch (SAXException se)
+			{
+				throw new NestableRuntimeException (se);
+			}
+		}
+
+		return sp;
+	}
+
+
+	/**
+	 *	Return a Transformer that will apply the XSL transformation
+	 *	from the given source.  If the source is null,
+	 *	no transformation will be applied.
+	 */
+	public static Transformer getTransformer (Source source)
+	{
+		TransformerFactory fact = checkTransCache ();
+		try
+		{
+			if (source == null)
+				return fact.newTransformer ();
+
+			return fact.newTransformer (source);
+		}
+		catch (TransformerConfigurationException tfce)
+		{
+			throw new NestableRuntimeException (tfce);
+		}
+	}
+
+
+	/**
+	 *	Return a Templates for the given XSL source.
+	 */
+	public static Templates getTemplates (Source source)
+	{
+		TransformerFactory fact = checkTransCache ();
+		try
+		{
+			return fact.newTemplates (source);
+		}
+		catch (TransformerConfigurationException tfce)
+		{
+			throw new NestableRuntimeException (tfce);
+		}
+	}
+
+
+	/**
+	 *	Return a TransformerHandler for transforming SAX events, applying the
+	 *	XSL transform from the given source.  If the source is null, no
+	 *	transform will be applied.  
+	 */
+	public static TransformerHandler getTransformerHandler (Source source)
+	{
+		SAXTransformerFactory fact = (SAXTransformerFactory) checkTransCache ();
+		try
+		{
+			if (source == null)
+				return fact.newTransformerHandler ();
+
+			return fact.newTransformerHandler (source);
+		}
+		catch (TransformerConfigurationException tfce)
+		{
+			throw new NestableRuntimeException (tfce);
+		}
+	}
+
+
+	/**
+	 *	Returns the cached TransformerFactory, creating it if necessary.
+	 */
+	private static TransformerFactory checkTransCache ()
+	{
+		// no synchronization necessary; multiple assignments OK
+		if (_transFactory == null)
+			_transFactory = TransformerFactory.newInstance ();
+
+		return _transFactory;	
+	}
+
+ 
+	/**
+	 *	Returns the cached DocumentBuilder matching the given configuration, 
+	 *	creating it if necessary.
+	 */
+	private static DocumentBuilder checkDOMCache (boolean validating, 
+		boolean namespaceAware)
+	{
+		// calculate where the factory with the correct config should
+		// be in our array cache
+		int arrayIndex = 0;
+		if (validating)
+			arrayIndex += 2;
+		if (namespaceAware)
+			arrayIndex += 1;
+
+		try
+		{
+			DocumentBuilderFactory factory = null;
+			factory = _domFactories[arrayIndex];
+
+			// no synchronization necessary; multiple assignments OK
+			if (factory == null)
+			{
+				factory = DocumentBuilderFactory.newInstance ();
+				factory.setValidating (validating);
+				factory.setNamespaceAware (namespaceAware);
+				_domFactories[arrayIndex] = factory;	
+			}
+			return factory.newDocumentBuilder ();
+		}
+		catch (ParserConfigurationException pce)
+		{
+			throw new NestableRuntimeException (pce);
+		}
+	}
+
+
+	/**
+	 *	Returns the cached SAXParser matching the given configuration, 
+	 *	creating it if necessary.
+	 */
+	private static SAXParser checkSAXCache (boolean validating,
+		boolean namespaceAware)
+	{
+		// calculate where the factory with the correct config should
+		// be in our array cache
+		int arrayIndex = 0;
+		if (validating)
+			arrayIndex += 2;
+		if (namespaceAware)
+			arrayIndex += 1;
+
+		try
+		{
+			SAXParserFactory factory = null;
+			factory = _saxFactories[arrayIndex];
+
+			// no synchronization necessary; multiple assignments OK
+			if (factory == null)
+			{
+				factory = SAXParserFactory.newInstance ();
+				factory.setValidating (validating);
+				factory.setNamespaceAware (namespaceAware);
+				_saxFactories[arrayIndex] = factory;	
+			}
+			return factory.newSAXParser ();
+		}
+		catch (ParserConfigurationException pce)
+		{
+			throw new NestableRuntimeException (pce);
+		}
+		catch (SAXException se)
+		{
+			throw new NestableRuntimeException (se);
+		}
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLWriter.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLWriter.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLWriter.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLWriter.java Sat May 13 20:25:56 2006
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.lib.xml;
+
+
+import java.io.*;
+
+
+/**
+ *	<p>The XMLWriter is a writer type for pretty-printing XML.
+ *	It assumes that the streamed XML will be given without any whitespace,
+ *	other than the space within text blocks.</p>
+ *
+ *	@author		Abe White
+ *	@nojavadoc
+ */	
+public class XMLWriter
+	extends FilterWriter
+{
+	private static String _endl	= System.getProperty ("line.separator");
+
+	private int	_lastChar 	= ' ';
+	private int	_lastChar2 	= ' ';
+	private int	_lastChar3 	= ' ';
+	private int	_depth		= 0;
+
+
+	/**
+	 *	Construct an XMLWriter that will write to the given stream.
+	 */
+	public XMLWriter (Writer out)
+	{
+		super (out);
+	}
+
+
+	public void write (char[] cbuf, int off, int len)
+		throws IOException
+	{
+		for (int i = 0; i < len; i++)
+			write (cbuf[off + i]);
+	}
+
+
+	public void write (String str, int off, int len)
+		throws IOException
+	{
+		for (int i = 0; i < len; i++)
+			write (str.charAt (off + i));
+	}
+
+
+	public void write (int c)
+		throws IOException
+	{
+		// the basic idea of this method is to make sure that when a tag
+		// or a text block starts, it is placed on a separate line and 
+		// indented an amount appropriate to the XML tree depth
+
+		if (_lastChar == '<')
+		{
+			// tag or processing instruction?
+			if (c != '?' && c != '!')
+			{
+				// end tag; decrease depth before writing spaces
+				if (c == '/')
+					_depth--;
+			
+				// tags are always on separate lines
+				out.write (_endl);
+				writeSpaces ();
+
+				// beginning tag; increase depth for tag body
+				if (c != '/')
+					_depth++;
+			}
+
+			// if this is not a processing instruction / comment,
+			// write the chars
+			if (c != '!')
+			{
+				out.write ('<');
+				out.write (c);
+			}
+		}
+		else if (c == '>')
+		{
+			// if unary tag decrease depth to undo the increase at tag start
+			if (_lastChar == '/')
+				_depth--;
+
+			// check for the comment-processing conditions
+			if (_lastChar2 == '<' && _lastChar == '!')
+				out.write ("<!");
+			else if (_lastChar3 == '<' && _lastChar2 == '!' && _lastChar == '-')
+				out.write ("<!-");
+
+			out.write ('>');
+		}
+		else if (c != '<')
+		{
+			// if we're at "<!--", indent and put in the beginning of
+			// the comment. if it's "<!-?" where ? is something other
+			// than -, dump what we've gotten so far
+			if (_lastChar3 == '<' && _lastChar2 == '!' && _lastChar == '-')
+			{
+				if (c == '-')
+				{
+					out.write (_endl);
+					writeSpaces ();
+					out.write ("<!--");
+				}
+				else
+				{
+					out.write ("<!-");
+					out.write (c);
+				}
+			}
+			// if we're at "<!-", keep on not writing data
+			else if (!(_lastChar2 == '<' && _lastChar == '!' && c == '-'))
+			{
+				// if just ended a tag and about to print text, put on
+				// separate line
+				if (_lastChar == '>' && _lastChar2 != '?' && _lastChar2 != '!')
+				{
+					out.write (_endl);
+					writeSpaces ();
+				}
+				out.write (c);
+			}
+		}
+
+		_lastChar3 = _lastChar2;
+		_lastChar2 = _lastChar;
+		_lastChar = c;
+	}
+
+
+	private void writeSpaces ()
+		throws IOException
+	{
+		for (int i = 0; i < _depth; i++)
+			out.write ("    ");
+	}
+}

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/XMLWriter.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/package.html
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/package.html?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/package.html (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/package.html Sat May 13 20:25:56 2006
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+<html>
+<body>
+	<p><strong>XML Handling</strong></p>
+</body>
+</html>

Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/xml/package.html
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message