You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2006/05/14 05:26:02 UTC
svn commit: r406215 [6/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/...
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/MetaDataSerializer.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/MetaDataSerializer.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/MetaDataSerializer.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/MetaDataSerializer.java Sat May 13 20:25:56 2006
@@ -0,0 +1,84 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.util.*;
+
+
+/**
+ * <p>Interface for meta data serializers.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public interface MetaDataSerializer
+{
+ public static final int COMPACT = 0;
+ public static final int PRETTY = 1;
+ public static final int APPEND = 2;
+ public static final int VERBOSE = 4;
+
+
+ /**
+ * Serialize the current set of objects to the files from which they were
+ * parsed. Any objects for which a source file cannot be determined will
+ * not be included in the output.
+ *
+ * @param flags bit flags specifying the output flags; e.g.
+ * {@link PRETTY #PRETTY}
+ */
+ public void serialize (int flags)
+ throws IOException;
+
+
+ /**
+ * Serialize the current set of objects to the files from which they were
+ * parsed. The objects must implement the {@link SourceTracker}
+ * interface.
+ *
+ * @param output if null, then serialize directly to the file system;
+ * othwerwise, populate the specified {@link Map} with
+ * keys that are the {@link File} instances, and
+ * values that are the {@link String} contents of
+ * the metadata
+ * @param flags bit flags specifying the output flags; e.g.
+ * {@link PRETTY #PRETTY}
+ */
+ public void serialize (Map output, int flags)
+ throws IOException;
+
+
+ /**
+ * Serialize the current set of objects to the given file.
+ *
+ * @param flags bit flags specifying the output flags; e.g.
+ * {@link PRETTY #PRETTY} | {@link APPEND #APPEND}
+ */
+ public void serialize (File file, int flags)
+ throws IOException;
+
+
+ /**
+ * Serialize the current set of objects to the given stream.
+ *
+ * @param flags bit flags specifying the output flags; e.g.
+ * {@link PRETTY #PRETTY}
+ */
+ public void serialize (Writer out, int flags)
+ throws IOException;
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/MetaDataSerializer.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ResourceMetaDataIterator.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ResourceMetaDataIterator.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ResourceMetaDataIterator.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ResourceMetaDataIterator.java Sat May 13 20:25:56 2006
@@ -0,0 +1,112 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import org.apache.openjpa.lib.util.*;
+
+
+/**
+ * <p>Iterator over a given metadata resource.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class ResourceMetaDataIterator
+ implements MetaDataIterator
+{
+ private List _urls = null;
+ private int _url = -1;
+
+
+ /**
+ * Constructor; supply the resource to parse.
+ */
+ public ResourceMetaDataIterator (String rsrc)
+ throws IOException
+ {
+ this (rsrc, null);
+ }
+
+
+ /**
+ * Constructor; supply the resource to parse.
+ */
+ public ResourceMetaDataIterator (String rsrc, ClassLoader loader)
+ throws IOException
+ {
+ if (loader == null)
+ {
+ MultiClassLoader multi = new MultiClassLoader ();
+ multi.addClassLoader (multi.SYSTEM_LOADER);
+ multi.addClassLoader (multi.THREAD_LOADER);
+ multi.addClassLoader (getClass ().getClassLoader ());
+ loader = multi;
+ }
+
+ Enumeration e = loader.getResources (rsrc);
+ while (e.hasMoreElements ())
+ {
+ if (_urls == null)
+ _urls = new ArrayList (3);
+ _urls.add (e.nextElement ());
+ }
+ }
+
+
+ public boolean hasNext ()
+ {
+ return _urls != null && _url + 1 < _urls.size ();
+ }
+
+
+ public Object next ()
+ {
+ if (!hasNext ())
+ throw new NoSuchElementException ();
+ return _urls.get (++_url);
+ }
+
+
+ public InputStream getInputStream ()
+ throws IOException
+ {
+ if (_url == -1 || _url >= _urls.size ())
+ throw new IllegalStateException ();
+ return ((URL) _urls.get (_url)).openStream ();
+ }
+
+
+ public File getFile ()
+ throws IOException
+ {
+ if (_url == -1 || _url >= _urls.size ())
+ throw new IllegalStateException ();
+ File file = new File (URLDecoder.decode (((URL) _urls.get (_url)).
+ getFile ()));
+ return (file.exists ()) ? file : null;
+ }
+
+
+ public void close ()
+ {
+ }
+}
+
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ResourceMetaDataIterator.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTracker.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTracker.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTracker.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTracker.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.meta;
+
+
+import java.io.*;
+
+
+/**
+ * <p>Interface that can optionally be implemented by metadata
+ * to include the source file from which the metadata was originally
+ * parsed.</p>
+ *
+ * @author Abe White
+ */
+public interface SourceTracker
+{
+ public static final int SRC_OTHER = 0;
+ public static final int SRC_ANNOTATIONS = 1;
+ public static final int SRC_XML = 2;
+
+
+ /**
+ * Return the file from which this instance was parsed.
+ */
+ public File getSourceFile ();
+
+
+ /**
+ * Return the domain-dependent scope of this instance within its file.
+ */
+ public Object getSourceScope ();
+
+
+ /**
+ * Return the type of source.
+ */
+ public int getSourceType ();
+
+
+ /**
+ * Return the domain-meaningful name of the resource that was loaded
+ * from this source. I.e., if we had loaded the source for a Java
+ * class, this would return the name of the class.
+ */
+ public String getResourceName ();
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTracker.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTrackers.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTrackers.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTrackers.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTrackers.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.meta;
+
+
+import org.apache.openjpa.lib.util.*;
+
+
+/**
+ * Utility class for performing common operations on {@link SourceTracker}s.
+ *
+ * @since 3.3.1
+ */
+public class SourceTrackers
+{
+ private static final Localizer _loc =
+ Localizer.forPackage (SourceTrackers.class);
+
+ private static final String SEP = System.getProperty ("line.separator");
+
+
+ /**
+ * Create a message appropriate for display to the user describing
+ * the location(s) that <code>trackers</code> were loaded from.
+ *
+ * @param trackers the source-trackers for which location info should
+ * be provided.
+ */
+ public static String getSourceLocationMessage (SourceTracker[] trackers)
+ {
+ StringBuffer buf = new StringBuffer (20 * (trackers.length + 1));
+ buf.append (_loc.get ("source-trackers-location-header")).append (SEP);
+ String sourceFilePath;
+ for (int i = 0; i < trackers.length; i++)
+ {
+ sourceFilePath = (trackers[i].getSourceFile () == null ?
+ _loc.get ("source-tracker-file-unknown") :
+ trackers[i].getSourceFile ().getAbsolutePath ());
+ buf.append (" ").append (_loc.get (
+ "source-trackers-location-line-item",
+ trackers[i].getResourceName (), sourceFilePath));
+ if (i < trackers.length - 1)
+ buf.append (SEP);
+ }
+ return buf.toString ();
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SourceTrackers.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SuffixMetaDataFilter.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SuffixMetaDataFilter.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SuffixMetaDataFilter.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SuffixMetaDataFilter.java Sat May 13 20:25:56 2006
@@ -0,0 +1,45 @@
+/*
+ * 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.meta;
+
+
+/**
+ * <p>Filters metadata iteration based on resource name suffix.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class SuffixMetaDataFilter
+ implements MetaDataFilter
+{
+ private final String _suffix;
+
+
+ /**
+ * Constructor; supply suffix to match against.
+ */
+ public SuffixMetaDataFilter (String suffix)
+ {
+ _suffix = suffix;
+ }
+
+
+ public boolean matches (Resource rsrc)
+ {
+ String name = rsrc.getName ();
+ return name != null && name.endsWith (_suffix);
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/SuffixMetaDataFilter.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/URLMetaDataIterator.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/URLMetaDataIterator.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/URLMetaDataIterator.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/URLMetaDataIterator.java Sat May 13 20:25:56 2006
@@ -0,0 +1,88 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.net.*;
+
+
+/**
+ * <p>Iterator over the metadata resource represented by a URL.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class URLMetaDataIterator
+ implements MetaDataIterator
+{
+ private final URL _url;
+ private boolean _iterated = false;
+
+
+ /**
+ * Constructor; supply resource URL.
+ */
+ public URLMetaDataIterator (URL url)
+ {
+ _url = url;
+ }
+
+
+ public boolean hasNext ()
+ {
+ return _url != null && !_iterated;
+ }
+
+
+ public Object next ()
+ throws IOException
+ {
+ if (!hasNext ())
+ throw new IllegalStateException ();
+
+ _iterated = true;
+ return _url;
+ }
+
+
+ public InputStream getInputStream ()
+ throws IOException
+ {
+ if (!_iterated)
+ throw new IllegalStateException ();
+ if (_url == null)
+ return null;
+ return _url.openStream ();
+ }
+
+
+ public File getFile ()
+ {
+ if (!_iterated)
+ throw new IllegalStateException ();
+ if (_url == null)
+ return null;
+ File file = new File (URLDecoder.decode (_url.getPath ()));
+ return (file.exists ()) ? file : null;
+ }
+
+
+ public void close ()
+ {
+ }
+}
+
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/URLMetaDataIterator.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataParser.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataParser.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataParser.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataParser.java Sat May 13 20:25:56 2006
@@ -0,0 +1,780 @@
+/*
+ * 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.meta;
+
+
+import java.util.*;
+import java.io.*;
+import java.net.*;
+import javax.xml.parsers.*;
+
+import org.xml.sax.helpers.*;
+import org.xml.sax.ext.*;
+import org.xml.sax.*;
+
+import org.apache.openjpa.lib.log.*;
+import org.apache.openjpa.lib.util.*;
+import org.apache.openjpa.lib.xml.*;
+
+
+/**
+ * <p>Custom SAX parser used by the system to quickly parse metadata files.
+ * Subclasses should handle the processing of the content.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public abstract class XMLMetaDataParser
+ extends DefaultHandler
+ implements LexicalHandler, MetaDataParser
+{
+ private static final Localizer _loc = Localizer.forPackage
+ (XMLMetaDataParser.class);
+
+ // map of classloaders to sets of parsed locations, so that we don't parse
+ // the same resource multiple times for the same class
+ private Map _parsed = null;
+
+ private Log _log = null;
+ private boolean _validating = true;
+ private boolean _systemId = true;
+ private boolean _caching = true;
+ private boolean _parseText = true;
+ private boolean _parseComments = true;
+ private String _suffix = null;
+ private ClassLoader _loader = null;
+ private ClassLoader _curLoader = null;
+
+ // state for current parse
+ private final Collection _curResults = new LinkedList ();
+ private List _results = null;
+ private String _sourceName = null;
+ protected File _sourceFile = null;
+ private StringBuffer _text = null;
+ private List _comments = null;
+ private Location _location = new Location ();
+ private LexicalHandler _lh = null;
+ private int _depth = -1;
+ private int _ignore = Integer.MAX_VALUE;
+
+
+ /**
+ * Whether to parse element text.
+ */
+ public boolean getParseText ()
+ {
+ return _parseText;
+ }
+
+
+ /**
+ * Whether to parse element text.
+ */
+ public void setParseText (boolean text)
+ {
+ _parseText = text;
+ }
+
+
+ /**
+ * Whether to parse element comments.
+ */
+ public boolean getParseComments ()
+ {
+ return _parseComments;
+ }
+
+
+ /**
+ * Whether to parse element comments.
+ */
+ public void setParseComments (boolean comments)
+ {
+ _parseComments = comments;
+ }
+
+
+ /**
+ * The XML document location.
+ */
+ public Location getLocation ()
+ {
+ return _location;
+ }
+
+
+ /**
+ * The lexical handler that should be registered with the SAX parser used
+ * by this class. Since the <code>org.xml.sax.ext</code> package is not
+ * a required part of SAX2, this handler might not be used by the parser.
+ */
+ public LexicalHandler getLexicalHandler ()
+ {
+ return _lh;
+ }
+
+
+ /**
+ * The lexical handler that should be registered with the SAX parser used
+ * by this class. Since the <code>org.xml.sax.ext</code> package is not
+ * a required part of SAX2, this handler might not be used by the parser.
+ */
+ public void setLexicalHandler (LexicalHandler lh)
+ {
+ _lh = lh;
+ }
+
+
+ /**
+ * The XML document location.
+ */
+ public void setLocation (Location location)
+ {
+ _location = location;
+ }
+
+
+ /**
+ * Whether to use the source name as the XML system id.
+ */
+ public boolean getSourceIsSystemId ()
+ {
+ return _systemId;
+ }
+
+
+ /**
+ * Whether to use the source name as the XML system id.
+ */
+ public void setSourceIsSystemId (boolean systemId)
+ {
+ _systemId = systemId;
+ }
+
+
+ /**
+ * Whether this is a validating parser.
+ */
+ public boolean isValidating ()
+ {
+ return _validating;
+ }
+
+
+ /**
+ * Whether this is a validating parser.
+ */
+ public void setValidating (boolean validating)
+ {
+ _validating = validating;
+ }
+
+
+ /**
+ * Expected suffix for metadata resources, or null if unknown.
+ */
+ public String getSuffix ()
+ {
+ return _suffix;
+ }
+
+
+ /**
+ * Expected suffix for metadata resources, or null if unknown.
+ */
+ public void setSuffix (String suffix)
+ {
+ _suffix = suffix;
+ }
+
+
+ /**
+ * Whether parsed resource names are cached to avoid duplicate parsing.
+ */
+ public boolean isCaching ()
+ {
+ return _caching;
+ }
+
+
+ /**
+ * Whether parsed resource names are cached to avoid duplicate parsing.
+ */
+ public void setCaching (boolean caching)
+ {
+ _caching = caching;
+ if (!caching)
+ clear ();
+ }
+
+
+ /**
+ * The log to write to.
+ */
+ public Log getLog ()
+ {
+ return _log;
+ }
+
+
+ /**
+ * The log to write to.
+ */
+ public void setLog (Log log)
+ {
+ _log = log;
+ }
+
+
+ /**
+ * Classloader to use for class name resolution.
+ */
+ public ClassLoader getClassLoader ()
+ {
+ return _loader;
+ }
+
+
+ /**
+ * Classloader to use for class name resolution.
+ */
+ public void setClassLoader (ClassLoader loader)
+ {
+ _loader = loader;
+ }
+
+
+ public List getResults ()
+ {
+ if (_results == null)
+ return Collections.EMPTY_LIST;
+ return _results;
+ }
+
+
+ public void parse (String rsrc)
+ throws IOException
+ {
+ if (rsrc != null)
+ parse (new ResourceMetaDataIterator (rsrc, _loader));
+ }
+
+
+ public void parse (URL url)
+ throws IOException
+ {
+ if (url != null)
+ parse (new URLMetaDataIterator (url));
+ }
+
+
+ public void parse (File file)
+ throws IOException
+ {
+ if (file == null)
+ return;
+ if (!file.isDirectory ())
+ parse (new FileMetaDataIterator (file));
+ else
+ {
+ String suff = (_suffix == null) ? "" : _suffix;
+ parse (new FileMetaDataIterator (file,
+ new SuffixMetaDataFilter (suff)));
+ }
+ }
+
+
+ public void parse (Class cls, boolean topDown)
+ throws IOException
+ {
+ String suff = (_suffix == null) ? "" : _suffix;
+ parse (new ClassMetaDataIterator (cls, suff, topDown), !topDown);
+ }
+
+
+ public void parse (Reader xml, String sourceName)
+ throws IOException
+ {
+ if (xml != null && (sourceName == null || !parsed (sourceName)))
+ parseNewResource (xml, sourceName);
+ }
+
+
+ public void parse (MetaDataIterator itr)
+ throws IOException
+ {
+ parse (itr, false);
+ }
+
+
+ /**
+ * Parse the resources returned by the given iterator, optionally stopping
+ * when the first valid resource is found.
+ */
+ private void parse (MetaDataIterator itr, boolean stopFirst)
+ throws IOException
+ {
+ if (itr == null)
+ return;
+ try
+ {
+ String sourceName;
+ while (itr.hasNext ())
+ {
+ sourceName = itr.next ().toString ();
+ if (parsed (sourceName))
+ {
+ if (stopFirst)
+ break;
+ continue;
+ }
+
+ // individual files of the resource might already be parsed
+ _sourceFile = itr.getFile ();
+ parseNewResource (new InputStreamReader (itr.getInputStream ()),
+ sourceName);
+ if (stopFirst)
+ break;
+ }
+ }
+ finally
+ {
+ itr.close ();
+ }
+ }
+
+
+ /**
+ * Parse a previously-unseen source. All parsing methods delegate
+ * to this one.
+ */
+ protected void parseNewResource (Reader xml, String sourceName)
+ throws IOException
+ {
+ if (_log != null && _log.isInfoEnabled ())
+ _log.info (_loc.get ("start-parse", sourceName));
+
+ // parse the metadata with a SAX parser
+ try
+ {
+ _sourceName = sourceName;
+ SAXParser parser = XMLFactory.getSAXParser (_validating, true);
+ Object schema = null;
+ if (_validating)
+ {
+ schema = getSchemaSource ();
+ if (schema == null)
+ xml = new DocTypeReader (xml, getDocType ());
+ }
+
+ if (_parseComments || _lh != null)
+ parser.setProperty
+ ("http://xml.org/sax/properties/lexical-handler", this);
+
+ if (schema != null)
+ {
+ parser.setProperty
+ ("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
+ "http://www.w3.org/2001/XMLSchema");
+ parser.setProperty
+ ("http://java.sun.com/xml/jaxp/properties/schemaSource",
+ schema);
+ }
+
+ InputSource is = new InputSource (xml);
+ if (_systemId && sourceName != null)
+ is.setSystemId (sourceName);
+ parser.parse (is, this);
+ finish ();
+ }
+ catch (SAXException se)
+ {
+ IOException ioe = new IOException (se.toString ());
+ JavaVersions.initCause (ioe, se);
+ throw ioe;
+ }
+ finally
+ {
+ reset ();
+ }
+ }
+
+
+ /**
+ * Return true if the given source is parsed. Otherwise, record that
+ * it will be parsed.
+ */
+ protected boolean parsed (String src)
+ {
+ if (!_caching)
+ return false;
+ if (_parsed == null)
+ _parsed = new HashMap ();
+
+ ClassLoader loader = currentClassLoader ();
+ boolean added;
+ synchronized (_parsed)
+ {
+ Set set = (Set) _parsed.get (loader);
+ if (set == null)
+ {
+ set = new HashSet ();
+ _parsed.put (loader, set);
+ }
+ added = set.add (src);
+ }
+ if (!added && _log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("already-parsed", src));
+ return !added;
+ }
+
+
+ public void clear ()
+ {
+ if (_log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("clear-parser", this));
+ if (_parsed != null)
+ _parsed.clear ();
+ }
+
+
+ public void error (SAXParseException se)
+ throws SAXException
+ {
+ throw getException (se.toString ());
+ }
+
+
+ public void fatalError (SAXParseException se)
+ throws SAXException
+ {
+ throw getException (se.toString ());
+ }
+
+
+ public void setDocumentLocator (Locator locator)
+ {
+ _location.setLocator (locator);
+ }
+
+
+ public void startElement (String uri, String name, String qName,
+ Attributes attrs)
+ throws SAXException
+ {
+ _depth++;
+ if (_depth <= _ignore)
+ if (!startElement (qName, attrs))
+ ignoreContent (true);
+ }
+
+
+ public void endElement (String uri, String name, String qName)
+ throws SAXException
+ {
+ if (_depth < _ignore)
+ endElement (qName);
+ _text = null;
+ if (_comments != null)
+ _comments.clear ();
+ if (_depth == _ignore)
+ _ignore = Integer.MAX_VALUE;
+ _depth--;
+ }
+
+
+ public void characters (char[] ch, int start, int length)
+ {
+ if (_parseText && _depth <= _ignore)
+ {
+ if (_text == null)
+ _text = new StringBuffer ();
+ _text.append (ch, start, length);
+ }
+ }
+
+
+ public void comment (char[] ch, int start, int length)
+ throws SAXException
+ {
+ if (_parseComments && _depth <= _ignore)
+ {
+ if (_comments == null)
+ _comments = new ArrayList (3);
+ _comments.add (String.valueOf (ch, start, length));
+ }
+ if (_lh != null)
+ _lh.comment (ch, start, length);
+ }
+
+
+ public void startCDATA ()
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.startCDATA ();
+ }
+
+
+ public void endCDATA ()
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.endCDATA ();
+ }
+
+
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.startDTD (name, publicId, systemId);
+ }
+
+
+ public void endDTD ()
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.endDTD ();
+ }
+
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.startEntity (name);
+ }
+
+
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (_lh != null)
+ _lh.endEntity (name);
+ }
+
+
+ /**
+ * Override this method marking the start of some element. If this method
+ * returns false, the content of the element and the end element event will
+ * be ignored.
+ */
+ protected abstract boolean startElement (String name, Attributes attrs)
+ throws SAXException;
+
+
+ /**
+ * Override this method marking the end of some element.
+ */
+ protected abstract void endElement (String name)
+ throws SAXException;
+
+
+ /**
+ * Add a result to be returned from the current parse.
+ */
+ protected void addResult (Object result)
+ {
+ if (_log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("add-result", result));
+ _curResults.add (result);
+ }
+
+
+ /**
+ * Override this method to finish up after a parse; this is only
+ * called if no errors are encountered during parsing. Subclasses should
+ * call <code>super.finish ()</code> to resolve superclass state.
+ */
+ protected void finish ()
+ {
+ if (_log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("end-parse", getSourceName ()));
+ _results = new ArrayList (_curResults);
+ }
+
+
+ /**
+ * Override this method to clear any state and ready the parser for
+ * a new document. Subclasses should call
+ * <code>super.reset ()</code> to clear superclass state.
+ */
+ protected void reset ()
+ {
+ _curResults.clear ();
+ _curLoader = null;
+ _sourceName = null;
+ _sourceFile = null;
+ _depth = -1;
+ _ignore = Integer.MAX_VALUE;
+ if (_comments != null)
+ _comments.clear ();
+ }
+
+
+ /**
+ * Implement to return the XML schema source for the document. Returns
+ * null by default. May return:
+ * <ul>
+ * <li><code>String</code> pointing to schema URI.</li>
+ * <li><code>InputStream</code> containing schema contents.</li>
+ * <li><code>InputSource</code> containing schema contents.</li>
+ * <li><code>File</code> containing schema contents.</li>
+ * <li>Array of any of the above elements.</li>
+ * </ul>
+ */
+ protected Object getSchemaSource ()
+ throws IOException
+ {
+ return null;
+ }
+
+
+ /**
+ * Override this method to return any <code>DOCTYPE</code> declaration
+ * that should be dynamically included in xml documents that will be
+ * validated. Returns null by default.
+ */
+ protected Reader getDocType ()
+ throws IOException
+ {
+ return null;
+ }
+
+
+ /**
+ * Return the name of the source file being parsed.
+ */
+ protected String getSourceName ()
+ {
+ return _sourceName;
+ }
+
+
+ /**
+ * Return the file of the source being parsed.
+ */
+ protected File getSourceFile ()
+ {
+ return _sourceFile;
+ }
+
+
+ /**
+ * Add current comments to the given entity. By default, assumes entity
+ * is {@link Commentable}.
+ */
+ protected void addComments (Object obj)
+ {
+ String[] comments = currentComments ();
+ if (comments.length > 0 && obj instanceof Commentable)
+ ((Commentable) obj).setComments (comments);
+ }
+
+
+ /**
+ * Array of comments for the current node, or empty array if none.
+ */
+ protected String[] currentComments ()
+ {
+ if (_comments == null || _comments.isEmpty ())
+ return Commentable.EMPTY_COMMENTS;
+ return (String[]) _comments.toArray (new String[_comments.size ()]);
+ }
+
+
+ /**
+ * Return the text value within the current node.
+ */
+ protected String currentText ()
+ {
+ if (_text == null)
+ return "";
+ return _text.toString ().trim ();
+ }
+
+
+ /**
+ * Return the current location within the source file.
+ */
+ protected String currentLocation ()
+ {
+ return " [" + _loc.get ("loc-prefix") + _location.getLocation () + "]";
+ }
+
+
+ /**
+ * Return the parse depth. Within the root element, the depth is 0,
+ * within the first nested element, it is 1, and so forth.
+ */
+ protected int currentDepth ()
+ {
+ return _depth;
+ }
+
+
+ /**
+ * Return the class loader to use when resolving resources and loading
+ * classes.
+ */
+ protected ClassLoader currentClassLoader ()
+ {
+ if (_loader != null)
+ return _loader;
+ if (_curLoader == null)
+ _curLoader = Thread.currentThread ().getContextClassLoader ();
+ return _curLoader;
+ }
+
+
+ /**
+ * Ignore all content below the current element.
+ *
+ * @param ignoreEnd whether to ignore the end element event
+ */
+ protected void ignoreContent (boolean ignoreEnd)
+ {
+ _ignore = _depth;
+ if (!ignoreEnd)
+ _ignore++;
+ }
+
+
+ /**
+ * Returns a SAXException with the source file name and the given error
+ * message.
+ */
+ protected SAXException getException (String msg)
+ {
+ return new SAXException (getSourceName () + currentLocation () +
+ ": " + msg);
+ }
+
+
+ /**
+ * Returns a SAXException with the source file name and the given error
+ * message.
+ */
+ protected SAXException getException (String msg, Throwable cause)
+ {
+ if (cause != null && _log != null && _log.isTraceEnabled ())
+ _log.trace (cause);
+ return new SAXException (getSourceName () + currentLocation () +
+ ": " + msg + " [" + cause + "]");
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataParser.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataSerializer.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataSerializer.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataSerializer.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataSerializer.java Sat May 13 20:25:56 2006
@@ -0,0 +1,418 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.util.*;
+import javax.xml.transform.*;
+import javax.xml.transform.sax.*;
+import javax.xml.transform.stream.*;
+
+import org.xml.sax.*;
+import org.xml.sax.helpers.*;
+import org.xml.sax.ext.*;
+
+import org.apache.openjpa.lib.log.*;
+import org.apache.openjpa.lib.util.*;
+import org.apache.openjpa.lib.xml.*;
+
+
+/**
+ * <p>Abstract base type for serlializers that transfer groups of objects
+ * to XML. Includes a way of serializing objects back to the XML files
+ * they were parsed from.</p>
+ *
+ * <p>Serializers are not thread safe.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public abstract class XMLMetaDataSerializer
+ implements MetaDataSerializer
+{
+ private static final Localizer _loc = Localizer.forPackage
+ (XMLMetaDataSerializer.class);
+ private static final SAXTransformerFactory _factory =
+ (SAXTransformerFactory) TransformerFactory.newInstance ();
+
+ private Log _log = null;
+
+ // current serialization state
+ private final AttributesImpl _attrs = new AttributesImpl ();
+ private ContentHandler _handler = null;
+ private int _flags = 0;
+ private File _backup = null;
+
+
+ /**
+ * The log to write to.
+ */
+ public Log getLog ()
+ {
+ return _log;
+ }
+
+
+ /**
+ * The log to write to.
+ */
+ public void setLog (Log log)
+ {
+ _log = log;
+ }
+
+
+ public void serialize (int flags)
+ throws IOException
+ {
+ serialize ((Map) null, flags);
+ }
+
+
+ public void serialize (Map output, int flags)
+ throws IOException
+ {
+ Map files = getFileMap ();
+ if (files == null)
+ return;
+
+ // for each file, serialize objects
+ Map.Entry entry;
+ for (Iterator itr = files.entrySet ().iterator (); itr.hasNext ();)
+ {
+ entry = (Map.Entry) itr.next ();
+ File file = (File) entry.getKey ();
+ Collection fileObjs = (Collection) entry.getValue ();
+
+ if (_log != null && _log.isInfoEnabled ())
+ _log.info (_loc.get ("ser-file", file));
+
+ try
+ {
+ TransformerHandler trans = _factory.newTransformerHandler ();
+ Writer writer;
+ if (output == null)
+ {
+ _backup = prepareWrite (file);
+ writer = new FileWriter (file);
+ }
+ else
+ writer = new StringWriter ();
+
+ Writer xml = writer;
+ if ((flags & PRETTY) > 0)
+ xml = new XMLWriter (writer);
+ trans.setResult (new StreamResult (xml));
+ serialize (fileObjs, trans, flags);
+
+ if (output != null)
+ output.put (file, ((StringWriter) writer).toString ());
+ }
+ catch (SAXException se)
+ {
+ throw new IOException (se.toString ());
+ }
+ catch (TransformerConfigurationException tce)
+ {
+ throw new IOException (tce.toString ());
+ }
+ }
+ }
+
+
+ /**
+ * Prepare to write to the given file. Back up the file and make sure the
+ * path to it is created.
+ */
+ protected File prepareWrite (File file)
+ throws IOException
+ {
+ File backup = Files.backup (file, false);
+ if (backup == null)
+ {
+ File parent = file.getParentFile ();
+ if (parent != null && !parent.exists ())
+ parent.mkdirs ();
+ }
+ return backup;
+ }
+
+
+ /**
+ * Returns a {@link Map} with keys of the {@link File} to be
+ * written to, and values of a {@link Collection} of
+ * {@link SourceTracker} instances.
+ */
+ protected Map getFileMap ()
+ {
+ Collection objs = getObjects ();
+ if (objs == null || objs.isEmpty ())
+ return null;
+
+ // create a map of files to lists of objects
+ Map files = new HashMap ();
+ File file;
+ Collection fileObjs;
+ Object obj;
+ for (Iterator itr = objs.iterator (); itr.hasNext ();)
+ {
+ obj = itr.next ();
+ file = getSourceFile (obj);
+ if (file == null)
+ {
+ if (_log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("no-file", obj));
+ continue;
+ }
+
+ fileObjs = (Collection) files.get (file);
+ if (fileObjs == null)
+ {
+ fileObjs = new LinkedList ();
+ files.put (file, fileObjs);
+ }
+ fileObjs.add (obj);
+ }
+
+ return files;
+ }
+
+
+ /**
+ * Return the source file for the given instance. By default, checks
+ * to see if the instance implements {@link SourceTracker}.
+ */
+ protected File getSourceFile (Object obj)
+ {
+ if (obj instanceof SourceTracker)
+ return ((SourceTracker) obj).getSourceFile ();
+ return null;
+ }
+
+
+ public void serialize (File file, int flags)
+ throws IOException
+ {
+ if (_log != null)
+ _log.info (_loc.get ("ser-file", file));
+
+ _backup = prepareWrite (file);
+ FileWriter out = new FileWriter (file.getCanonicalPath (),
+ (flags & APPEND) > 0);
+ serialize (out, flags);
+ out.close ();
+ }
+
+
+ public void serialize (Writer out, int flags)
+ throws IOException
+ {
+ try
+ {
+ if ((flags & PRETTY) > 0)
+ serialize (new StreamResult (new XMLWriter (out)), flags);
+ else
+ serialize (new StreamResult (out), flags);
+ }
+ catch (SAXException se)
+ {
+ throw new IOException (se.toString ());
+ }
+ }
+
+
+ /**
+ * Serialize the current set of objects to the given result.
+ */
+ public void serialize (Result result, int flags)
+ throws SAXException
+ {
+ try
+ {
+ TransformerHandler trans = _factory.newTransformerHandler ();
+ trans.setResult (result);
+ serialize (trans, flags);
+ }
+ catch (TransformerConfigurationException tce)
+ {
+ throw new SAXException (tce);
+ }
+ }
+
+
+ /**
+ * Serilize the current set of objects to a series of SAX events on the
+ * given handler.
+ */
+ public void serialize (ContentHandler handler, int flags)
+ throws SAXException
+ {
+ serialize (getObjects (), handler, flags);
+ }
+
+
+ /**
+ * Serialize the given collection of objects to the given handler.
+ */
+ private void serialize (Collection objs, ContentHandler handler, int flags)
+ throws SAXException
+ {
+ if (_log != null && _log.isTraceEnabled ())
+ _log.trace (_loc.get ("ser-objs", objs));
+
+ _handler = handler;
+ _flags = flags;
+ try
+ {
+ if (!objs.isEmpty ())
+ {
+ handler.startDocument ();
+ serialize (objs);
+ handler.endDocument ();
+ }
+ }
+ finally
+ {
+ reset ();
+ }
+ }
+
+
+ /**
+ * Whether this serialization is in verbose mode.
+ */
+ protected boolean isVerbose ()
+ {
+ return (_flags & VERBOSE) > 0;
+ }
+
+
+ /**
+ * The backup file made for the current file being parsed.
+ */
+ protected File currentBackupFile ()
+ {
+ return _backup;
+ }
+
+
+ /**
+ * Start an element with the current attribute settings. Clears the
+ * attributes as well.
+ */
+ protected void startElement (String name)
+ throws SAXException
+ {
+ _handler.startElement ("", name, name, _attrs);
+ _attrs.clear ();
+ }
+
+
+ /**
+ * End the current element.
+ */
+ protected void endElement (String name)
+ throws SAXException
+ {
+ _handler.endElement ("", name, name);
+ }
+
+
+ /**
+ * Add text to the current element.
+ */
+ protected void addText (String text)
+ throws SAXException
+ {
+ _handler.characters (text.toCharArray (), 0, text.length ());
+ }
+
+
+ /**
+ * Add an attribute to the current group.
+ */
+ protected void addAttribute (String name, String value)
+ {
+ _attrs.addAttribute ("", name, name, "CDATA", value);
+ }
+
+
+ /**
+ * The current attributes.
+ */
+ protected Attributes getAttributes ()
+ {
+ return _attrs;
+ }
+
+
+ /**
+ * Add a comment to the stream.
+ */
+ protected void addComments (String[] comments)
+ throws SAXException
+ {
+ if (comments == null || comments.length == 0
+ || !(_handler instanceof LexicalHandler))
+ return;
+
+ LexicalHandler lh = (LexicalHandler) _handler;
+ char[] chars;
+ for (int i = 0; i < comments.length; i++)
+ {
+ chars = comments[i].toCharArray ();
+ lh.comment (chars, 0, chars.length);
+ }
+ }
+
+
+ /**
+ * Write the given entity's comments. By default, tests if entity is
+ * {@link Commentable}.
+ */
+ protected void addComments (Object obj)
+ throws SAXException
+ {
+ if (obj instanceof Commentable)
+ addComments (((Commentable) obj).getComments ());
+ }
+
+
+ /**
+ * Reset serialization state for the next document.
+ */
+ protected void reset ()
+ {
+ _attrs.clear ();
+ _handler = null;
+ _flags = 0;
+ _backup = null;
+ }
+
+
+ /**
+ * Serialize the given set of objects.
+ */
+ protected abstract void serialize (Collection objs)
+ throws SAXException;
+
+
+ /**
+ * Return the current set of objects for serialization.
+ */
+ protected abstract Collection getObjects ();
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/XMLMetaDataSerializer.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipFileMetaDataIterator.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipFileMetaDataIterator.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipFileMetaDataIterator.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipFileMetaDataIterator.java Sat May 13 20:25:56 2006
@@ -0,0 +1,156 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.zip.*;
+
+
+/**
+ * <p>Iterator over all metadata resources in a given zip file.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class ZipFileMetaDataIterator
+ implements MetaDataIterator, MetaDataFilter.Resource
+{
+ private final ZipFile _file;
+ private final MetaDataFilter _filter;
+ private final Enumeration _entries;
+ private final boolean _close;
+ private ZipEntry _entry = null;
+ private ZipEntry _last = null;
+
+
+ /**
+ * Constructor; supply zip/jar URL and optional file filter.
+ */
+ public ZipFileMetaDataIterator (URL url, MetaDataFilter filter)
+ throws IOException
+ {
+ _file = (url == null) ? null : (ZipFile) url.getContent ();
+ _filter = filter;
+ _entries = (_file == null) ? null : _file.entries ();
+ _close = false;
+ }
+
+
+ /**
+ * Constructor; supply zip file and optional file filter.
+ */
+ public ZipFileMetaDataIterator (ZipFile file, MetaDataFilter filter)
+ {
+ _file = file;
+ _filter = filter;
+ _entries = (file == null) ? null : file.entries ();
+ _close = true;
+ }
+
+
+ public boolean hasNext ()
+ throws IOException
+ {
+ if (_entries == null)
+ return false;
+
+ // search for next metadata file
+ while (_entry == null && _entries.hasMoreElements ())
+ {
+ _entry = (ZipEntry) _entries.nextElement ();
+ if (_filter != null && !_filter.matches (this))
+ _entry = null;
+ }
+ return _entry != null;
+ }
+
+
+ public Object next ()
+ throws IOException
+ {
+ if (!hasNext ())
+ throw new NoSuchElementException ();
+ String ret = _entry.getName ();
+ _last = _entry;
+ _entry = null;
+ return ret;
+ }
+
+
+ public InputStream getInputStream ()
+ throws IOException
+ {
+ if (_last == null)
+ throw new IllegalStateException ();
+ return _file.getInputStream (_last);
+ }
+
+
+ public File getFile ()
+ {
+ if (_last == null)
+ throw new IllegalStateException ();
+ return null;
+ }
+
+
+ public void close ()
+ {
+ if (_close)
+ try { _file.close (); } catch (IOException ioe) {}
+ }
+
+
+ //////////////////////////////////////////
+ // MetaDataFilter.Resource implementation
+ //////////////////////////////////////////
+
+
+ public String getName ()
+ {
+ return _entry.getName ();
+ }
+
+
+ public byte[] getContent ()
+ throws IOException
+ {
+ long size = _entry.getSize ();
+ if (size == 0)
+ return new byte[0];
+
+ InputStream in = _file.getInputStream (_entry);
+ byte[] content;
+ if (size < 0)
+ {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
+ byte[] buf = new byte[1024];
+ for (int r; (r = in.read (buf)) != -1; bout.write (buf, 0, r));
+ content = bout.toByteArray ();
+ }
+ else
+ {
+ content = new byte[(int) size];
+ in.read (content);
+ }
+ in.close ();
+ return content;
+ }
+}
+
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipFileMetaDataIterator.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipStreamMetaDataIterator.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipStreamMetaDataIterator.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipStreamMetaDataIterator.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipStreamMetaDataIterator.java Sat May 13 20:25:56 2006
@@ -0,0 +1,182 @@
+/*
+ * 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.meta;
+
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+
+/**
+ * <p>Iterator over all metadata resources in a given zip input stream.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class ZipStreamMetaDataIterator
+ implements MetaDataIterator, MetaDataFilter.Resource
+{
+ private final ZipInputStream _stream;
+ private final MetaDataFilter _filter;
+ private ZipEntry _entry = null;
+ private ZipEntry _last = null;
+ private byte[] _buf = null;
+
+
+ /**
+ * Constructor; supply zip stream and optional metadata filter.
+ */
+ public ZipStreamMetaDataIterator (ZipInputStream stream,
+ MetaDataFilter filter)
+ {
+ _stream = stream;
+ _filter = filter;
+ }
+
+
+ public boolean hasNext ()
+ throws IOException
+ {
+ if (_stream == null)
+ return false;
+ if (_entry != null)
+ return true;
+
+ // close last rsrc
+ if (_buf == null && _last != null)
+ _stream.closeEntry ();
+ _last = null;
+ _buf = null;
+
+ // search for next file
+ ZipEntry entry;
+ while (_entry == null && (entry = _stream.getNextEntry ()) != null)
+ {
+ _entry = entry;
+ if (_filter != null && !_filter.matches (this))
+ {
+ _entry = null;
+ _stream.closeEntry ();
+ }
+ }
+ return _entry != null;
+ }
+
+
+ public Object next ()
+ throws IOException
+ {
+ if (!hasNext ())
+ throw new NoSuchElementException ();
+ String ret = _entry.getName ();
+ _last = _entry;
+ _entry = null;
+ return ret;
+ }
+
+
+ public InputStream getInputStream ()
+ {
+ if (_last == null)
+ throw new IllegalStateException ();
+
+ if (_buf != null)
+ return new ByteArrayInputStream (_buf);
+ return new NoCloseInputStream ();
+ }
+
+
+ public File getFile ()
+ {
+ return null;
+ }
+
+
+ public void close ()
+ {
+ try { _stream.close (); } catch (IOException ioe) {}
+ }
+
+
+ //////////////////////////////////////////
+ // MetaDataFilter.Resource implementation
+ //////////////////////////////////////////
+
+
+ public String getName ()
+ {
+ return _entry.getName ();
+ }
+
+
+ public byte[] getContent ()
+ throws IOException
+ {
+ // buffer content so that future calls to getInputStream can read
+ // the same data
+ int size = (int) _entry.getSize ();
+ if (size < 0)
+ {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
+ byte[] buf = new byte[1024];
+ for (int r; (r = _stream.read (buf)) != -1; bout.write (buf, 0, r));
+ _buf = bout.toByteArray ();
+ }
+ else
+ {
+ _buf = new byte[size];
+ _stream.read (_buf);
+ }
+ _stream.closeEntry ();
+ return _buf;
+ }
+
+
+ /**
+ * Non-closing input stream used to make sure the underlying zip
+ * stream is not closed.
+ */
+ private class NoCloseInputStream
+ extends InputStream
+ {
+ public int available ()
+ throws IOException
+ {
+ return _stream.available ();
+ }
+
+
+ public int read ()
+ throws IOException
+ {
+ return _stream.read ();
+ }
+
+
+ public int read (byte[] b, int off, int len)
+ throws IOException
+ {
+ return _stream.read (b, off, len);
+ }
+
+
+ public void close ()
+ {
+ }
+ }
+}
+
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/ZipStreamMetaDataIterator.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/package.html
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/package.html?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/package.html (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/package.html Sat May 13 20:25:56 2006
@@ -0,0 +1,23 @@
+/*
+ * 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>Metadata Framework</strong></p>
+ <p>
+ Extendable framework for metadata parsing and serializing.
+ </p>
+</body>
+</html>
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/meta/package.html
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractListIterator.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractListIterator.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractListIterator.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractListIterator.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.rop;
+
+
+import java.util.*;
+
+import org.apache.openjpa.lib.util.*;
+
+
+/**
+ * <p>Abstract read-only list iterator.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+abstract class AbstractListIterator
+ implements ListIterator
+{
+ private static final Localizer _loc = Localizer.forPackage
+ (AbstractListIterator.class);
+
+
+ public void add (Object o)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public void set (Object o)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public void remove ()
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractListIterator.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractNonSequentialResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractNonSequentialResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractNonSequentialResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractNonSequentialResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,225 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+import org.apache.commons.lang.*;
+
+
+/**
+ * <p>Abstract base class for random-access result lists. Unlike the
+ * {@link AbstractList}, this class doesn't rely on the
+ * {@link Collection#size} method.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public abstract class AbstractNonSequentialResultList
+ extends AbstractResultList
+{
+ protected static final Object PAST_END = new Object ();
+
+
+ /**
+ * Implement this method and {@link #size}. Return {@link #PAST_END}
+ * if the index is out of bounds.
+ */
+ protected abstract Object getInternal (int index);
+
+
+ public boolean contains (Object o)
+ {
+ assertOpen ();
+ Object obj;
+ for (int i = 0; true; i++)
+ {
+ obj = getInternal (i);
+ if (obj == PAST_END)
+ break;
+ if (ObjectUtils.equals (o, obj))
+ return true;
+ }
+ return false;
+ }
+
+
+ public boolean containsAll (Collection c)
+ {
+ assertOpen ();
+ for (Iterator itr = c.iterator (); itr.hasNext ();)
+ if (!contains (itr.next ()))
+ return false;
+ return true;
+ }
+
+
+ public Object get (int index)
+ {
+ assertOpen ();
+ Object obj = getInternal (index);
+ if (obj == PAST_END)
+ throw new NoSuchElementException ();
+ return obj;
+ }
+
+
+ public int indexOf (Object o)
+ {
+ assertOpen ();
+ Object obj;
+ for (int i = 0; true; i++)
+ {
+ obj = getInternal (i);
+ if (obj == PAST_END)
+ break;
+ if (ObjectUtils.equals (o, obj))
+ return i;
+ }
+ return -1;
+ }
+
+
+ public int lastIndexOf (Object o)
+ {
+ assertOpen ();
+ int index = -1;
+ Object obj;
+ for (int i = 0; true; i++)
+ {
+ obj = getInternal (i);
+ if (obj == PAST_END)
+ break;
+ if (ObjectUtils.equals (o, obj))
+ index = i;
+ }
+ return index;
+ }
+
+
+ public boolean isEmpty ()
+ {
+ assertOpen ();
+ return getInternal (0) == PAST_END;
+ }
+
+
+ public Iterator iterator ()
+ {
+ return listIterator ();
+ }
+
+
+ public ListIterator listIterator ()
+ {
+ return listIterator (0);
+ }
+
+
+ public ListIterator listIterator (int index)
+ {
+ return new ResultListIterator (new Itr (index), this);
+ }
+
+
+ public Object[] toArray ()
+ {
+ assertOpen ();
+ ArrayList list = new ArrayList ();
+ Object obj;
+ for (int i = 0; true; i++)
+ {
+ obj = getInternal (i);
+ if (obj == PAST_END)
+ break;
+ list.add (obj);
+ }
+ return list.toArray ();
+ }
+
+
+ public Object[] toArray (Object[] a)
+ {
+ assertOpen ();
+ ArrayList list = new ArrayList ();
+ Object obj;
+ for (int i = 0; true; i++)
+ {
+ obj = getInternal (i);
+ if (obj == PAST_END)
+ break;
+ list.add (obj);
+ }
+ return list.toArray (a);
+ }
+
+
+ private class Itr
+ extends AbstractListIterator
+ {
+ private int _idx = 0;
+ private Object _next = PAST_END;
+
+
+ public Itr (int index)
+ {
+ _idx = index;
+ }
+
+
+ public int nextIndex ()
+ {
+ return _idx;
+ }
+
+
+ public int previousIndex ()
+ {
+ return _idx - 1;
+ }
+
+
+ public boolean hasNext ()
+ {
+ _next = getInternal (_idx);
+ return _next != PAST_END;
+ }
+
+
+ public boolean hasPrevious ()
+ {
+ return _idx > 0;
+ }
+
+
+ public Object previous ()
+ {
+ if (_idx == 0)
+ throw new NoSuchElementException ();
+ return getInternal (--_idx);
+ }
+
+
+ public Object next ()
+ {
+ if (!hasNext ())
+ throw new NoSuchElementException ();
+ _idx++;
+ return _next;
+ }
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractNonSequentialResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,116 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+import org.apache.openjpa.lib.util.*;
+
+
+/**
+ * <p>Abstract base class for read-only result lists.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public abstract class AbstractResultList
+ implements ResultList
+{
+ private static final Localizer _loc = Localizer.forPackage
+ (AbstractResultList.class);
+
+
+ public void add (int index, Object element)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean add (Object o)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean addAll (Collection c)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean addAll (int index, Collection c)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public Object remove (int index)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean remove (Object o)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean removeAll (Collection c)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public boolean retainAll (Collection c)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public Object set (int index, Object element)
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public void clear ()
+ {
+ throw new UnsupportedOperationException (_loc.get ("read-only"));
+ }
+
+
+ public List subList (int from, int to)
+ {
+ throw new UnsupportedOperationException ();
+ }
+
+
+ protected void finalize ()
+ throws Throwable
+ {
+ super.finalize ();
+ close ();
+ }
+
+
+ protected void assertOpen ()
+ {
+ if (isClosed ())
+ throw new NoSuchElementException (_loc.get ("closed"));
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractSequentialResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractSequentialResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractSequentialResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractSequentialResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,134 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+import org.apache.commons.lang.*;
+
+
+/**
+ * <p>Abstract base class for sequential result lists. Unlike the
+ * {@link AbstractSequentialList}, this class doesn't rely on the
+ * {@link Collection#size} method.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public abstract class AbstractSequentialResultList
+ extends AbstractResultList
+{
+ /**
+ * Implement this method and {@link #size}.
+ */
+ protected abstract ListIterator itr (int index);
+
+
+ public boolean contains (Object o)
+ {
+ assertOpen ();
+ for (Iterator itr = itr (0); itr.hasNext ();)
+ if (ObjectUtils.equals (o, itr.next ()))
+ return true;
+ return false;
+ }
+
+
+ public boolean containsAll (Collection c)
+ {
+ assertOpen ();
+ for (Iterator itr = c.iterator (); itr.hasNext ();)
+ if (!contains (itr.next ()))
+ return false;
+ return true;
+ }
+
+
+ public Object get (int index)
+ {
+ assertOpen ();
+ return itr (index).next ();
+ }
+
+
+ public int indexOf (Object o)
+ {
+ assertOpen ();
+ int index = 0;
+ for (Iterator itr = itr (0); itr.hasNext (); index++)
+ if (ObjectUtils.equals (o, itr.next ()))
+ return index;
+ return -1;
+ }
+
+
+ public int lastIndexOf (Object o)
+ {
+ assertOpen ();
+ int index = -1;
+ int i = 0;
+ for (Iterator itr = itr (0); itr.hasNext (); i++)
+ if (ObjectUtils.equals (o, itr.next ()))
+ index = i;
+ return index;
+ }
+
+
+ public boolean isEmpty ()
+ {
+ assertOpen ();
+ return !itr (0).hasNext ();
+ }
+
+
+ public Iterator iterator ()
+ {
+ return listIterator ();
+ }
+
+
+ public ListIterator listIterator ()
+ {
+ return listIterator (0);
+ }
+
+
+ public ListIterator listIterator (int index)
+ {
+ return new ResultListIterator (itr (index), this);
+ }
+
+
+ public Object[] toArray ()
+ {
+ assertOpen ();
+ ArrayList list = new ArrayList ();
+ for (Iterator itr = itr (0); itr.hasNext ();)
+ list.add (itr.next ());
+ return list.toArray ();
+ }
+
+
+ public Object[] toArray (Object[] a)
+ {
+ assertOpen ();
+ ArrayList list = new ArrayList ();
+ for (Iterator itr = itr (0); itr.hasNext ();)
+ list.add (itr.next ());
+ return list.toArray (a);
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/AbstractSequentialResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/EagerResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/EagerResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/EagerResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/EagerResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,55 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+
+/**
+ * Simple, non-lazy ResultList implementation
+ *
+ * @author Patrick Linskey
+ * @nojavadoc
+ */
+public class EagerResultList
+ extends ListResultList
+ implements ResultList
+{
+ public EagerResultList (ResultObjectProvider rop)
+ {
+ super (new ArrayList ());
+ try
+ {
+ rop.open ();
+ while (rop.next ())
+ getDelegate ().add (rop.getResultObject ());
+ }
+ catch (RuntimeException re)
+ {
+ throw re;
+ }
+ catch (Exception e)
+ {
+ rop.handleCheckedException (e);
+ }
+ finally
+ {
+ try { rop.close (); } catch (Exception e) {}
+ }
+ }
+}
+
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/EagerResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/LazyForwardResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/LazyForwardResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/LazyForwardResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/LazyForwardResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,249 @@
+/*
+ * 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.rop;
+
+
+import java.io.*;
+import java.util.*;
+
+
+/**
+ * <p>Lazy forward-only result list.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class LazyForwardResultList
+ extends AbstractSequentialResultList
+ implements ResultList
+{
+ private static final int OPEN = 0;
+ private static final int CLOSED = 1;
+ private static final int FREED = 2;
+
+ private ResultObjectProvider _rop = null;
+ private final List _list = new ArrayList ();
+ private int _state = OPEN;
+ private int _size = -1;
+
+
+ public LazyForwardResultList (ResultObjectProvider rop)
+ {
+ _rop = rop;
+ try
+ {
+ _rop.open ();
+ }
+ catch (RuntimeException re)
+ {
+ close ();
+ throw re;
+ }
+ catch (Exception e)
+ {
+ close ();
+ _rop.handleCheckedException (e);
+ }
+ }
+
+
+ public boolean isProviderOpen ()
+ {
+ return _state == OPEN;
+ }
+
+
+ public boolean isClosed ()
+ {
+ return _state == CLOSED;
+ }
+
+
+ public void close ()
+ {
+ if (_state != CLOSED)
+ {
+ free ();
+ _state = CLOSED;
+ }
+ }
+
+
+ public Object get (int index)
+ {
+ assertOpen ();
+
+ // optimization for getting sequntially
+ if (index == _list.size ())
+ addNext ();
+ if (index < _list.size ())
+ return _list.get (index);
+
+ return super.get (index);
+ }
+
+
+ protected ListIterator itr (int index)
+ {
+ return (_state != OPEN) ? _list.listIterator (index) : new Itr (index);
+ }
+
+
+ public int size ()
+ {
+ assertOpen ();
+ if (_size != -1)
+ return _size;
+ if (_state != OPEN)
+ return _list.size ();
+ try
+ {
+ _size = _rop.size ();
+ return _size;
+ }
+ catch (RuntimeException re)
+ {
+ close ();
+ throw re;
+ }
+ catch (Exception e)
+ {
+ close ();
+ _rop.handleCheckedException (e);
+ return -1;
+ }
+ }
+
+
+ private boolean addNext ()
+ {
+ try
+ {
+ if (!_rop.next ())
+ {
+ free ();
+ return false;
+ }
+ _list.add (_rop.getResultObject ());
+ return true;
+ }
+ catch (RuntimeException re)
+ {
+ close ();
+ throw re;
+ }
+ catch (Exception e)
+ {
+ close ();
+ _rop.handleCheckedException (e);
+ return false;
+ }
+ }
+
+
+ private void free ()
+ {
+ if (_state == OPEN)
+ {
+ try { _rop.close (); } catch (Exception e) {}
+ _state = FREED;
+ }
+ }
+
+
+ public Object writeReplace ()
+ throws ObjectStreamException
+ {
+ // fully traverse results
+ if (_state == OPEN)
+ for (Iterator itr = itr (_list.size ()); itr.hasNext ();)
+ itr.next ();
+ return _list;
+ }
+
+
+ public int hashCode ()
+ {
+ // superclass tries to traverses entire list for hashcode
+ return System.identityHashCode (this);
+ }
+
+
+ public boolean equals (Object other)
+ {
+ // superclass tries to traverse entire list for equality
+ return other == this;
+ }
+
+
+ private class Itr
+ extends AbstractListIterator
+ {
+ private int _idx = 0;
+
+
+ public Itr (int index)
+ {
+ _idx = Math.min (index, _list.size ());
+ while (_idx < index)
+ next ();
+ }
+
+
+ public int nextIndex ()
+ {
+ return _idx;
+ }
+
+
+ public int previousIndex ()
+ {
+ return _idx - 1;
+ }
+
+
+ public boolean hasNext ()
+ {
+ if (_list.size () > _idx)
+ return true;
+ if (_state != OPEN)
+ return false;
+ return addNext ();
+ }
+
+
+ public boolean hasPrevious ()
+ {
+ return _idx > 0;
+ }
+
+
+ public Object previous ()
+ {
+ if (_idx == 0)
+ throw new NoSuchElementException ();
+ return _list.get (--_idx);
+ }
+
+
+ public Object next ()
+ {
+ if (!hasNext ())
+ throw new NoSuchElementException ();
+ return _list.get (_idx++);
+ }
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/LazyForwardResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultList.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultList.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultList.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultList.java Sat May 13 20:25:56 2006
@@ -0,0 +1,156 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+
+/**
+ * <p>A basic {@link ResultList} implementation that wraps a normal list.</p>
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
+public class ListResultList
+ extends AbstractResultList
+{
+ private final List _list;
+ private boolean _closed = false;
+
+
+ /**
+ * Constructor. Supply delegate.
+ */
+ public ListResultList (List list)
+ {
+ _list = list;
+ }
+
+
+ /**
+ * Return the wrapped list.
+ */
+ public List getDelegate ()
+ {
+ return _list;
+ }
+
+
+ public boolean isProviderOpen ()
+ {
+ return false;
+ }
+
+
+ public boolean isClosed ()
+ {
+ return _closed;
+ }
+
+
+ public void close ()
+ {
+ _closed = true;
+ }
+
+
+ public boolean contains (Object o)
+ {
+ assertOpen ();
+ return _list.contains (o);
+ }
+
+
+ public boolean containsAll (Collection c)
+ {
+ assertOpen ();
+ return _list.containsAll (c);
+ }
+
+
+ public Object get (int index)
+ {
+ assertOpen ();
+ return _list.get (index);
+ }
+
+
+ public int indexOf (Object o)
+ {
+ assertOpen ();
+ return _list.indexOf (o);
+ }
+
+
+ public int lastIndexOf (Object o)
+ {
+ assertOpen ();
+ return _list.lastIndexOf (o);
+ }
+
+
+ public int size ()
+ {
+ assertOpen ();
+ return _list.size ();
+ }
+
+
+ public boolean isEmpty ()
+ {
+ assertOpen ();
+ return _list.isEmpty ();
+ }
+
+
+ public Iterator iterator ()
+ {
+ return listIterator ();
+ }
+
+
+ public ListIterator listIterator ()
+ {
+ return new ResultListIterator (_list.listIterator (), this);
+ }
+
+
+ public ListIterator listIterator (int index)
+ {
+ return new ResultListIterator (_list.listIterator (index), this);
+ }
+
+
+ public Object[] toArray ()
+ {
+ assertOpen ();
+ return _list.toArray ();
+ }
+
+
+ public Object[] toArray (Object[] a)
+ {
+ assertOpen ();
+ return _list.toArray (a);
+ }
+
+
+ public Object writeReplace ()
+ {
+ return _list;
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultList.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultObjectProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultObjectProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultObjectProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultObjectProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,118 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+import org.apache.commons.lang.exception.*;
+
+import org.apache.openjpa.lib.util.*;
+
+
+/**
+ * <p>A result object provider wrapped around a normal list.</p>
+ *
+ * @author Abe White
+ */
+public class ListResultObjectProvider
+ implements ResultObjectProvider
+{
+ private final List _list;
+ private int _idx = -1;
+
+
+ /**
+ * Constructor. Supply delegate.
+ */
+ public ListResultObjectProvider (List list)
+ {
+ _list = list;
+ }
+
+
+ public List getDelegate ()
+ {
+ return _list;
+ }
+
+
+ public boolean supportsRandomAccess ()
+ {
+ return true;
+ }
+
+
+ public void open ()
+ throws Exception
+ {
+ }
+
+
+ public Object getResultObject ()
+ throws Exception
+ {
+ return _list.get (_idx);
+ }
+
+
+ public boolean next ()
+ throws Exception
+ {
+ return absolute (_idx + 1);
+ }
+
+
+ public boolean absolute (int pos)
+ throws Exception
+ {
+ if (pos >= 0 && pos < _list.size ())
+ {
+ _idx = pos;
+ return true;
+ }
+ return false;
+ }
+
+
+ public int size ()
+ throws Exception
+ {
+ return _list.size ();
+ }
+
+
+ public void reset ()
+ throws Exception
+ {
+ _idx = -1;
+ }
+
+
+ public void close ()
+ throws Exception
+ {
+ if (_list instanceof Closeable)
+ try { ((Closeable) _list).close (); } catch (Exception e) {}
+ }
+
+
+ public void handleCheckedException (Exception e)
+ {
+ // shouldn't ever happen
+ throw new NestableRuntimeException (e);
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/ListResultObjectProvider.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/MergedResultObjectProvider.java
URL: http://svn.apache.org/viewcvs/incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/MergedResultObjectProvider.java?rev=406215&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/MergedResultObjectProvider.java (added)
+++ incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/MergedResultObjectProvider.java Sat May 13 20:25:56 2006
@@ -0,0 +1,283 @@
+/*
+ * 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.rop;
+
+
+import java.util.*;
+
+import org.apache.commons.lang.exception.*;
+
+
+/**
+ * <p>A result object provider that merges multiple result object provider
+ * delegates. Support exists for maintaining ordering of the internally held
+ * results, provided that each of the individual results is itself ordered.</p>
+ *
+ * @author Abe White
+ * @author Marc Prud'hommeaux
+ */
+public class MergedResultObjectProvider
+ implements ResultObjectProvider
+{
+ private static final byte UNOPENED = 0;
+ private static final byte OPENED = 1;
+ private static final byte VALUE = 2;
+ private static final byte DONE = 3;
+
+ private final ResultObjectProvider[] _rops;
+ private final Comparator _comp;
+ private final byte[] _status;
+ private Object[] _values;
+ private Object[] _orderValues;
+ private Object _cur = null;
+ private int _size = -1;
+
+
+ /**
+ * Constructor. Provide delegates.
+ */
+ public MergedResultObjectProvider (ResultObjectProvider[] rops)
+ {
+ this (rops, null);
+ }
+
+
+ /**
+ * Constructor. Provide delegates and optional comparator.
+ */
+ public MergedResultObjectProvider (ResultObjectProvider[] rops,
+ Comparator comp)
+ {
+ _rops = rops;
+ _comp = comp;
+ _status = new byte[rops.length];
+ _values = (comp == null) ? null : new Object[rops.length];
+ _orderValues = (comp == null) ? null : new Object[rops.length];
+ }
+
+
+ public boolean supportsRandomAccess ()
+ {
+ return false;
+ }
+
+
+ public void open ()
+ throws Exception
+ {
+ // if we have a comparator, then open all; else open first
+ int len = (_comp != null) ? _rops.length : 1;
+ for (int i = 0; i < len; i++)
+ {
+ _rops[i].open ();
+ _status[i] = OPENED;
+ }
+ }
+
+
+ public boolean absolute (int pos)
+ throws Exception
+ {
+ throw new UnsupportedOperationException ();
+ }
+
+
+ public int size ()
+ throws Exception
+ {
+ if (_size != -1)
+ return _size;
+
+ // have to open all to get sizes
+ for (int i = 0; i < _status.length; i++)
+ {
+ if (_status[i] == UNOPENED)
+ {
+ _rops[i].open ();
+ _status[i] = OPENED;
+ }
+ }
+
+ int total = 0;
+ int size;
+ for (int i = 0; i < _rops.length; i++)
+ {
+ size = _rops[i].size ();
+ if (size == Integer.MAX_VALUE)
+ {
+ total = size;
+ break;
+ }
+ total += size;
+ }
+ _size = total;
+ return _size;
+ }
+
+
+ public void reset ()
+ throws Exception
+ {
+ for (int i = 0; i < _rops.length; i++)
+ if (_status[i] != UNOPENED)
+ _rops[i].reset ();
+ clear ();
+ }
+
+
+ public void close ()
+ throws Exception
+ {
+ Exception err = null;
+ for (int i = 0; i < _rops.length; i++)
+ {
+ try
+ {
+ if (_status[i] != UNOPENED)
+ _rops[i].close ();
+ }
+ catch (Exception e)
+ {
+ if (err == null)
+ err = e;
+ }
+ }
+
+ clear ();
+ if (err != null)
+ throw err;
+ }
+
+
+ private void clear ()
+ {
+ _cur = null;
+ for (int i = 0; i < _rops.length; i++)
+ {
+ _status[i] = OPENED;
+ if (_values != null)
+ _values[i] = null;
+ if (_orderValues != null)
+ _orderValues[i] = null;
+ }
+ }
+
+
+ public void handleCheckedException (Exception e)
+ {
+ if (_rops.length == 0)
+ throw new NestableRuntimeException (e);
+ _rops[0].handleCheckedException (e);
+ }
+
+
+ public boolean next ()
+ throws Exception
+ {
+ // initialize all rops with the latest values
+ boolean hasValue = false;
+ for (int i = 0; i < _status.length; i++)
+ {
+ switch (_status[i])
+ {
+ case UNOPENED:
+ // this will only ever be the case if we aren't ordering
+ _rops[i].open ();
+ _status[i] = OPENED;
+ // no break
+ case OPENED:
+ // if this rop has a value, cache it; if we're not ordering,
+ // then that's the value to return
+ if (_rops[i].next ())
+ {
+ if (_comp == null)
+ {
+ _cur = _rops[i].getResultObject ();
+ return true;
+ }
+ else
+ {
+ hasValue = true;
+ _status[i] = VALUE;
+ _values[i] = _rops[i].getResultObject ();
+ _orderValues[i] = getOrderingValue (_values[i],
+ i, _rops[i]);
+ }
+ }
+ else
+ _status[i] = DONE;
+ break;
+ case VALUE:
+ // we only use this state when ordering
+ hasValue = true;
+ break;
+ }
+ }
+
+ // if we get to this point without a comparator, it means none
+ // of our rops have any more values
+ if (_comp == null || !hasValue)
+ return false;
+
+ // for all the rops with values, find the 'least' one according to
+ // the comparator
+ int least = -1;
+ Object orderVal = null;
+ for (int i = 0; i < _orderValues.length; i++)
+ {
+ if (_status[i] != VALUE)
+ continue;
+ if (least == -1 || _comp.compare (_orderValues[i], orderVal) < 0)
+ {
+ least = i;
+ orderVal = _orderValues[i];
+ }
+ }
+
+ // assign the least value to the current one, and clear the cached
+ // value for that rop so that we know to get the next value for
+ // the next comparison
+ _cur = _values[least];
+ _values[least] = null;
+ _orderValues[least] = null;
+ _status[least] = OPENED;
+ return true;
+ }
+
+
+ public Object getResultObject ()
+ throws Exception
+ {
+ return _cur;
+ }
+
+
+ /**
+ * Return the value to use for ordering on the given result value. Returns
+ * the result value by default.
+ *
+ * @param val the result value
+ * @param idx the index of the result object provider in the array
+ * given on construction that produced the result value
+ * @param rop the result object provider that produced the result
+ * value
+ */
+ protected Object getOrderingValue (Object val, int idx,
+ ResultObjectProvider rop)
+ {
+ return val;
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-lib/java/org/apache/openjpa/lib/rop/MergedResultObjectProvider.java
------------------------------------------------------------------------------
svn:executable = *