You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by dj...@apache.org on 2009/10/05 20:54:56 UTC
svn commit: r821961 [24/30] - in /geronimo/sandbox/djencks/osgi/framework:
./ buildsupport/ buildsupport/car-maven-plugin/
buildsupport/car-maven-plugin/src/main/java/org/apache/geronimo/mavenplugins/car/
buildsupport/geronimo-maven-plugin/src/main/jav...
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,667 @@
+package org.apache.geronimo.system.plugin.plexus.util.reflection;
+
+/*
+ * Copyright The Codehaus 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.
+ */
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Utility class used to instantiate an object using reflection. This utility
+ * hides many of the gory details needed to do this.
+ *
+ * @author John Casey
+ */
+public final class Reflector
+{
+ private static final String CONSTRUCTOR_METHOD_NAME = "$$CONSTRUCTOR$$";
+
+ private static final String GET_INSTANCE_METHOD_NAME = "getInstance";
+
+ private HashMap classMaps = new HashMap();
+
+ /** Ensure no instances of Reflector are created...this is a utility. */
+ public Reflector()
+ {
+ }
+
+ /**
+ * Create a new instance of a class, given the array of parameters... Uses
+ * constructor caching to find a constructor that matches the parameter
+ * types, either specifically (first choice) or abstractly...
+ *
+ * @param theClass
+ * The class to instantiate
+ * @param params
+ * The parameters to pass to the constructor
+ * @return The instantiated object
+ * @throws ReflectorException
+ * In case anything goes wrong here...
+ */
+ public Object newInstance( Class theClass, Object[] params )
+ throws ReflectorException
+ {
+ if ( params == null )
+ {
+ params = new Object[0];
+ }
+
+ Class[] paramTypes = new Class[params.length];
+
+ for ( int i = 0, len = params.length; i < len; i++ )
+ {
+ paramTypes[i] = params[i].getClass();
+ }
+
+ try
+ {
+ Constructor con = getConstructor( theClass, paramTypes );
+
+ if ( con == null )
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append( "Constructor not found for class: " );
+ buffer.append( theClass.getName() );
+ buffer.append( " with specified or ancestor parameter classes: " );
+
+ for ( int i = 0; i < paramTypes.length; i++ )
+ {
+ buffer.append( paramTypes[i].getName() );
+ buffer.append( ',' );
+ }
+
+ buffer.setLength( buffer.length() - 1 );
+
+ throw new ReflectorException( buffer.toString() );
+ }
+
+ return con.newInstance( params );
+ }
+ catch ( InstantiationException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ catch ( InvocationTargetException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ catch ( IllegalAccessException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ }
+
+ /**
+ * Retrieve the singleton instance of a class, given the array of
+ * parameters... Uses constructor caching to find a constructor that matches
+ * the parameter types, either specifically (first choice) or abstractly...
+ *
+ * @param theClass
+ * The class to retrieve the singleton of
+ * @param initParams
+ * The parameters to pass to the constructor
+ * @return The singleton object
+ * @throws ReflectorException
+ * In case anything goes wrong here...
+ */
+ public Object getSingleton( Class theClass, Object[] initParams )
+ throws ReflectorException
+ {
+ Class[] paramTypes = new Class[initParams.length];
+
+ for ( int i = 0, len = initParams.length; i < len; i++ )
+ {
+ paramTypes[i] = initParams[i].getClass();
+ }
+
+ try
+ {
+ Method method = getMethod( theClass, GET_INSTANCE_METHOD_NAME, paramTypes );
+
+ return method.invoke( null, initParams );
+ }
+ catch ( InvocationTargetException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ catch ( IllegalAccessException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ }
+
+ /**
+ * Invoke the specified method on the specified target with the specified
+ * params...
+ *
+ * @param target
+ * The target of the invocation
+ * @param methodName
+ * The method name to invoke
+ * @param params
+ * The parameters to pass to the method invocation
+ * @return The result of the method call
+ * @throws ReflectorException
+ * In case of an error looking up or invoking the method.
+ */
+ public Object invoke( Object target, String methodName, Object[] params )
+ throws ReflectorException
+ {
+ if ( params == null )
+ {
+ params = new Object[0];
+ }
+
+ Class[] paramTypes = new Class[params.length];
+
+ for ( int i = 0, len = params.length; i < len; i++ )
+ {
+ paramTypes[i] = params[i].getClass();
+ }
+
+ try
+ {
+ Method method = getMethod( target.getClass(), methodName, paramTypes );
+
+ if ( method == null )
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append( "Singleton-producing method named '" ).append( methodName )
+ .append( "' not found with specified parameter classes: " );
+
+ for ( int i = 0; i < paramTypes.length; i++ )
+ {
+ buffer.append( paramTypes[i].getName() );
+ buffer.append( ',' );
+ }
+
+ buffer.setLength( buffer.length() - 1 );
+
+ throw new ReflectorException( buffer.toString() );
+ }
+
+ return method.invoke( target, params );
+ }
+ catch ( InvocationTargetException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ catch ( IllegalAccessException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ }
+
+ public Object getStaticField( Class targetClass, String fieldName )
+ throws ReflectorException
+ {
+ try
+ {
+ Field field = targetClass.getField( fieldName );
+
+ return field.get( null );
+ }
+ catch ( SecurityException e )
+ {
+ throw new ReflectorException( e );
+ }
+ catch ( NoSuchFieldException e )
+ {
+ throw new ReflectorException( e );
+ }
+ catch ( IllegalArgumentException e )
+ {
+ throw new ReflectorException( e );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new ReflectorException( e );
+ }
+ }
+
+ public Object getField( Object target, String fieldName )
+ throws ReflectorException
+ {
+ return getField( target, fieldName, false );
+ }
+
+ public Object getField( Object target, String fieldName, boolean breakAccessibility )
+ throws ReflectorException
+ {
+ Class targetClass = target.getClass();
+ while ( targetClass != null )
+ {
+ try
+ {
+ Field field = targetClass.getDeclaredField( fieldName );
+
+ boolean accessibilityBroken = false;
+ if ( !field.isAccessible() && breakAccessibility )
+ {
+ field.setAccessible( true );
+ accessibilityBroken = true;
+ }
+
+ Object result = field.get( target );
+
+ if ( accessibilityBroken )
+ {
+ field.setAccessible( false );
+ }
+
+ return result;
+ }
+ catch ( SecurityException e )
+ {
+ throw new ReflectorException( e );
+ }
+ catch ( NoSuchFieldException e )
+ {
+ if ( targetClass == Object.class )
+ throw new ReflectorException( e );
+ targetClass = targetClass.getSuperclass();
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new ReflectorException( e );
+ }
+ }
+ // Never reached, but needed to satisfy compiler
+ return null;
+ }
+
+ /**
+ * Invoke the specified static method with the specified params...
+ *
+ * @param targetClass
+ * The target class of the invocation
+ * @param methodName
+ * The method name to invoke
+ * @param params
+ * The parameters to pass to the method invocation
+ * @return The result of the method call
+ * @throws ReflectorException
+ * In case of an error looking up or invoking the method.
+ */
+ public Object invokeStatic( Class targetClass, String methodName, Object[] params )
+ throws ReflectorException
+ {
+ if ( params == null )
+ {
+ params = new Object[0];
+ }
+
+ Class[] paramTypes = new Class[params.length];
+
+ for ( int i = 0, len = params.length; i < len; i++ )
+ {
+ paramTypes[i] = params[i].getClass();
+ }
+
+ try
+ {
+ Method method = getMethod( targetClass, methodName, paramTypes );
+
+ if ( method == null )
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append( "Singleton-producing method named \'" + methodName
+ + "\' not found with specified parameter classes: " );
+
+ for ( int i = 0; i < paramTypes.length; i++ )
+ {
+ buffer.append( paramTypes[i].getName() );
+ buffer.append( ',' );
+ }
+
+ buffer.setLength( buffer.length() - 1 );
+
+ throw new ReflectorException( buffer.toString() );
+ }
+
+ return method.invoke( null, params );
+ }
+ catch ( InvocationTargetException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ catch ( IllegalAccessException ex )
+ {
+ throw new ReflectorException( ex );
+ }
+ }
+
+ /**
+ * Return the constructor, checking the cache first and storing in cache if
+ * not already there..
+ *
+ * @param targetClass
+ * The class to get the constructor from
+ * @param params
+ * The classes of the parameters which the constructor should
+ * match.
+ * @return the Constructor object that matches.
+ * @throws ReflectorException
+ * In case we can't retrieve the proper constructor.
+ */
+ public Constructor getConstructor( Class targetClass, Class[] params )
+ throws ReflectorException
+ {
+ Map constructorMap = getConstructorMap( targetClass );
+
+ StringBuffer key = new StringBuffer( 200 );
+
+ key.append( "(" );
+
+ for ( int i = 0, len = params.length; i < len; i++ )
+ {
+ key.append( params[i].getName() );
+ key.append( "," );
+ }
+
+ if ( params.length > 0 )
+ {
+ key.setLength( key.length() - 1 );
+ }
+
+ key.append( ")" );
+
+ Constructor constructor = null;
+
+ String paramKey = key.toString();
+
+ synchronized ( paramKey.intern() )
+ {
+ constructor = (Constructor) constructorMap.get( paramKey );
+
+ if ( constructor == null )
+ {
+ Constructor[] cands = targetClass.getConstructors();
+
+ for ( int i = 0, len = cands.length; i < len; i++ )
+ {
+ Class[] types = cands[i].getParameterTypes();
+
+ if ( params.length != types.length )
+ {
+ continue;
+ }
+
+ for ( int j = 0, len2 = params.length; j < len2; j++ )
+ {
+ if ( !types[j].isAssignableFrom( params[j] ) )
+ {
+ continue;
+ }
+ }
+
+ // we got it, so store it!
+ constructor = cands[i];
+ constructorMap.put( paramKey, constructor );
+ }
+ }
+ }
+
+ if ( constructor == null )
+ {
+ throw new ReflectorException( "Error retrieving constructor object for: " + targetClass.getName()
+ + paramKey );
+ }
+
+ return constructor;
+ }
+
+ public Object getObjectProperty( Object target, String propertyName )
+ throws ReflectorException
+ {
+ Object returnValue = null;
+
+ if ( propertyName == null || propertyName.trim().length() < 1 )
+ {
+ throw new ReflectorException( "Cannot retrieve value for empty property." );
+ }
+
+ String beanAccessor = "get" + Character.toUpperCase( propertyName.charAt( 0 ) );
+ if ( propertyName.trim().length() > 1 )
+ {
+ beanAccessor += propertyName.substring( 1 ).trim();
+ }
+
+ Class targetClass = target.getClass();
+ Class[] emptyParams = {};
+
+ Method method = _getMethod( targetClass, beanAccessor, emptyParams );
+ if ( method == null )
+ {
+ method = _getMethod( targetClass, propertyName, emptyParams );
+ }
+ if ( method != null )
+ {
+ try
+ {
+ returnValue = method.invoke( target, new Object[] {} );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new ReflectorException( "Error retrieving property \'" + propertyName + "\' from \'"
+ + targetClass + "\'", e );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new ReflectorException( "Error retrieving property \'" + propertyName + "\' from \'"
+ + targetClass + "\'", e );
+ }
+ }
+
+ if ( method != null )
+ {
+ try
+ {
+ returnValue = method.invoke( target, new Object[] {} );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new ReflectorException( "Error retrieving property \'" + propertyName + "\' from \'"
+ + targetClass + "\'", e );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new ReflectorException( "Error retrieving property \'" + propertyName + "\' from \'"
+ + targetClass + "\'", e );
+ }
+ }
+ else
+ {
+ returnValue = getField( target, propertyName, true );
+ if ( method == null && returnValue == null )
+ {
+ // TODO: Check if exception is the right action! Field exists, but contains null
+ throw new ReflectorException( "Neither method: \'" + propertyName + "\' nor bean accessor: \'"
+ + beanAccessor + "\' can be found for class: \'" + targetClass
+ + "\', and retrieval of field: \'" + propertyName + "\' returned null as value." );
+ }
+ }
+
+ return returnValue;
+ }
+
+ /**
+ * Return the method, checking the cache first and storing in cache if not
+ * already there..
+ *
+ * @param targetClass
+ * The class to get the method from
+ * @param params
+ * The classes of the parameters which the method should match.
+ * @return the Method object that matches.
+ * @throws ReflectorException
+ * In case we can't retrieve the proper method.
+ */
+ public Method getMethod( Class targetClass, String methodName, Class[] params )
+ throws ReflectorException
+ {
+ Method method = _getMethod( targetClass, methodName, params );
+
+ if ( method == null )
+ {
+ throw new ReflectorException( "Method: \'" + methodName + "\' not found in class: \'" + targetClass
+ + "\'" );
+ }
+
+ return method;
+ }
+
+ private Method _getMethod( Class targetClass, String methodName, Class[] params )
+ throws ReflectorException
+ {
+ Map methodMap = getMethodMap( targetClass, methodName );
+
+ StringBuffer key = new StringBuffer( 200 );
+
+ key.append( "(" );
+
+ for ( int i = 0, len = params.length; i < len; i++ )
+ {
+ key.append( params[i].getName() );
+ key.append( "," );
+ }
+
+ key.append( ")" );
+
+ Method method = null;
+
+ String paramKey = key.toString();
+
+ synchronized ( paramKey.intern() )
+ {
+ method = (Method) methodMap.get( paramKey );
+
+ if ( method == null )
+ {
+ Method[] cands = targetClass.getMethods();
+
+ for ( int i = 0, len = cands.length; i < len; i++ )
+ {
+ String name = cands[i].getName();
+
+ if ( !methodName.equals( name ) )
+ {
+ continue;
+ }
+
+ Class[] types = cands[i].getParameterTypes();
+
+ if ( params.length != types.length )
+ {
+ continue;
+ }
+
+ for ( int j = 0, len2 = params.length; j < len2; j++ )
+ {
+ if ( !types[j].isAssignableFrom( params[j] ) )
+ {
+ continue;
+ }
+ }
+
+ // we got it, so store it!
+ method = cands[i];
+ methodMap.put( paramKey, method );
+ }
+ }
+ }
+
+ return method;
+ }
+
+ /**
+ * Retrieve the cache of constructors for the specified class.
+ *
+ * @param theClass
+ * the class to lookup.
+ * @return The cache of constructors.
+ * @throws ReflectorException
+ * in case of a lookup error.
+ */
+ private Map getConstructorMap( Class theClass )
+ throws ReflectorException
+ {
+ return getMethodMap( theClass, CONSTRUCTOR_METHOD_NAME );
+ }
+
+ /**
+ * Retrieve the cache of methods for the specified class and method name.
+ *
+ * @param theClass
+ * the class to lookup.
+ * @param methodName
+ * The name of the method to lookup.
+ * @return The cache of constructors.
+ * @throws ReflectorException
+ * in case of a lookup error.
+ */
+ private Map getMethodMap( Class theClass, String methodName )
+ throws ReflectorException
+ {
+ Map methodMap = null;
+
+ if ( theClass == null )
+ {
+ return null;
+ }
+
+ String className = theClass.getName();
+
+ synchronized ( className.intern() )
+ {
+ Map classMethods = (Map) classMaps.get( className );
+
+ if ( classMethods == null )
+ {
+ classMethods = new HashMap();
+ methodMap = new HashMap();
+ classMethods.put( methodName, methodMap );
+
+ classMaps.put( className, classMethods );
+ }
+ else
+ {
+ String key = className + "::" + methodName;
+
+ synchronized ( key.intern() )
+ {
+ methodMap = (Map) classMethods.get( methodName );
+
+ if ( methodMap == null )
+ {
+ methodMap = new HashMap();
+ classMethods.put( methodName, methodMap );
+ }
+ }
+ }
+ }
+
+ return methodMap;
+ }
+}
\ No newline at end of file
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/Reflector.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,71 @@
+package org.apache.geronimo.system.plugin.plexus.util.reflection;
+
+/*
+ * Copyright The Codehaus 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.
+ */
+
+/**
+ * Exception indicating that an error has occurred while instantiating a class
+ * with the Reflector class. This exception is meant to put a more user-friendly
+ * face on the myriad other exceptions throws during reflective object creation.
+ *
+ * @author John Casey
+ */
+public class ReflectorException
+ extends Exception
+{
+ /**
+ * Create a new ReflectorException.
+ */
+ public ReflectorException()
+ {
+ }
+
+ /**
+ * Create a new ReflectorException with the specified message.
+ *
+ * @param msg
+ * The message.
+ */
+ public ReflectorException( String msg )
+ {
+ super( msg );
+ }
+
+ /**
+ * Create a new ReflectorException with the specified root cause.
+ *
+ * @param root
+ * The root cause.
+ */
+ public ReflectorException( Throwable root )
+ {
+ super( root );
+ }
+
+ /**
+ * Create a new ReflectorException with the specified message and root
+ * cause.
+ *
+ * @param msg
+ * The message.
+ * @param root
+ * The root cause.
+ */
+ public ReflectorException( String msg, Throwable root )
+ {
+ super( msg, root );
+ }
+}
\ No newline at end of file
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/reflection/ReflectorException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,43 @@
+package org.apache.geronimo.system.plugin.plexus.util.xml;
+
+/*
+ * Copyright 2007 The Codehaus 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.
+ */
+
+import java.io.PrintWriter;
+import java.io.Writer;
+
+/**
+ * @version $Id: CompactXMLWriter.java 8010 2009-01-07 12:59:50Z vsiveton $
+ */
+public class CompactXMLWriter
+ extends PrettyPrintXMLWriter
+{
+
+ public CompactXMLWriter( PrintWriter writer )
+ {
+ super( writer );
+ }
+
+ public CompactXMLWriter( Writer writer )
+ {
+ super( writer );
+ }
+
+ protected void endOfLine()
+ {
+ // override parent: don't write anything at end of line
+ }
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/CompactXMLWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,485 @@
+package org.apache.geronimo.system.plugin.plexus.util.xml;
+
+/*
+ * Copyright 2008 The Codehaus 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.
+ */
+
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.LinkedList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.geronimo.system.plugin.plexus.util.StringUtils;
+
+/**
+ * Implementation of XMLWriter which emits nicely formatted documents.
+ *
+ * @version $Id: PrettyPrintXMLWriter.java 8046 2009-01-12 23:39:16Z vsiveton $
+ */
+public class PrettyPrintXMLWriter
+ implements XMLWriter
+{
+ /** Line separator ("\n" on UNIX) */
+ protected static final String LS = System.getProperty( "line.separator" );
+
+ private PrintWriter writer;
+
+ private LinkedList elementStack = new LinkedList();
+
+ private boolean tagInProgress;
+
+ private int depth;
+
+ private String lineIndenter;
+
+ private String lineSeparator;
+
+ private String encoding;
+
+ private String docType;
+
+ private boolean readyForNewLine;
+
+ private boolean tagIsEmpty;
+
+ /**
+ * @param writer not null
+ * @param lineIndenter could be null, but the normal way is some spaces.
+ */
+ public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter )
+ {
+ this( writer, lineIndenter, null, null );
+ }
+
+ /**
+ * @param writer not null
+ * @param lineIndenter could be null, but the normal way is some spaces.
+ */
+ public PrettyPrintXMLWriter( Writer writer, String lineIndenter )
+ {
+ this( new PrintWriter( writer ), lineIndenter );
+ }
+
+ /**
+ * @param writer not null
+ */
+ public PrettyPrintXMLWriter( PrintWriter writer )
+ {
+ this( writer, null, null );
+ }
+
+ /**
+ * @param writer not null
+ */
+ public PrettyPrintXMLWriter( Writer writer )
+ {
+ this( new PrintWriter( writer ) );
+ }
+
+ /**
+ * @param writer not null
+ * @param lineIndenter could be null, but the normal way is some spaces.
+ * @param encoding could be null or invalid.
+ * @param doctype could be null.
+ */
+ public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String encoding, String doctype )
+ {
+ this( writer, lineIndenter, LS, encoding, doctype );
+ }
+
+ /**
+ * @param writer not null
+ * @param lineIndenter could be null, but the normal way is some spaces.
+ * @param encoding could be null or invalid.
+ * @param doctype could be null.
+ */
+ public PrettyPrintXMLWriter( Writer writer, String lineIndenter, String encoding, String doctype )
+ {
+ this( new PrintWriter( writer ), lineIndenter, encoding, doctype );
+ }
+
+ /**
+ * @param writer not null
+ * @param encoding could be null or invalid.
+ * @param doctype could be null.
+ */
+ public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
+ {
+ this( writer, " ", encoding, doctype );
+ }
+
+ /**
+ * @param writer not null
+ * @param encoding could be null or invalid.
+ * @param doctype could be null.
+ */
+ public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype )
+ {
+ this( new PrintWriter( writer ), encoding, doctype );
+ }
+
+ /**
+ * @param writer not null
+ * @param lineIndenter could be null, but the normal way is some spaces.
+ * @param lineSeparator could be null, but the normal way is valid line separator ("\n" on UNIX).
+ * @param encoding could be null or invalid.
+ * @param doctype could be null.
+ */
+ public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String lineSeparator, String encoding, String doctype )
+ {
+ setWriter( writer );
+
+ setLineIndenter( lineIndenter );
+
+ setLineSeparator( lineSeparator );
+
+ setEncoding( encoding );
+
+ setDocType( doctype );
+
+ if ( doctype != null || encoding != null )
+ {
+ writeDocumentHeaders();
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void startElement( String name )
+ {
+ tagIsEmpty = false;
+
+ finishTag();
+
+ write( "<" );
+
+ write( name );
+
+ elementStack.addLast( name );
+
+ tagInProgress = true;
+
+ setDepth( getDepth() + 1 );
+
+ readyForNewLine = true;
+
+ tagIsEmpty = true;
+ }
+
+ /** {@inheritDoc} */
+ public void writeText( String text )
+ {
+ writeText( text, true );
+ }
+
+ /** {@inheritDoc} */
+ public void writeMarkup( String text )
+ {
+ writeText( text, false );
+ }
+
+ private void writeText( String text, boolean escapeXml )
+ {
+ readyForNewLine = false;
+
+ tagIsEmpty = false;
+
+ finishTag();
+
+ if ( escapeXml )
+ {
+ text = escapeXml( text );
+ }
+
+ write( StringUtils.unifyLineSeparators( text, lineSeparator ) );
+ }
+
+ private static String escapeXml( String text )
+ {
+ text = text.replaceAll( "&", "&" );
+
+ text = text.replaceAll( "<", "<" );
+
+ text = text.replaceAll( ">", ">" );
+
+ text = text.replaceAll( "\"", """ );
+
+ text = text.replaceAll( "\'", "'" );
+
+ return text;
+ }
+
+ private static String escapeXmlAttribute( String text )
+ {
+ text = escapeXml( text );
+
+ // Windows
+ text = text.replaceAll( "\r\n", " " );
+
+ Pattern pattern = Pattern.compile( "([\000-\037])" );
+ Matcher m = pattern.matcher( text );
+ StringBuffer b = new StringBuffer();
+ while ( m.find() )
+ {
+ m = m.appendReplacement( b, "&#" + Integer.toString( m.group( 1 ).charAt( 0 ) ) + ";" );
+ }
+ m.appendTail( b );
+
+ return b.toString();
+ }
+
+ /** {@inheritDoc} */
+ public void addAttribute( String key, String value )
+ {
+ write( " " );
+
+ write( key );
+
+ write( "=\"" );
+
+ write( escapeXmlAttribute( value ) );
+
+ write( "\"" );
+ }
+
+ /** {@inheritDoc} */
+ public void endElement()
+ {
+ setDepth( getDepth() - 1 );
+
+ if ( tagIsEmpty )
+ {
+ write( "/" );
+
+ readyForNewLine = false;
+
+ finishTag();
+
+ elementStack.removeLast();
+ }
+ else
+ {
+ finishTag();
+
+ write( "</" + elementStack.removeLast() + ">" );
+ }
+
+ readyForNewLine = true;
+ }
+
+ /**
+ * Write a string to the underlying writer
+ * @param str
+ */
+ private void write( String str )
+ {
+ getWriter().write( str );
+ }
+
+ private void finishTag()
+ {
+ if ( tagInProgress )
+ {
+ write( ">" );
+ }
+
+ tagInProgress = false;
+
+ if ( readyForNewLine )
+ {
+ endOfLine();
+ }
+ readyForNewLine = false;
+
+ tagIsEmpty = false;
+ }
+
+ /**
+ * Get the string used as line indenter
+ *
+ * @return the line indenter
+ */
+ protected String getLineIndenter()
+ {
+ return lineIndenter;
+ }
+
+ /**
+ * Set the string used as line indenter
+ *
+ * @param lineIndenter new line indenter, could be null, but the normal way is some spaces.
+ */
+ protected void setLineIndenter( String lineIndenter )
+ {
+ this.lineIndenter = lineIndenter;
+ }
+
+ /**
+ * Get the string used as line separator or LS if not set.
+ *
+ * @return the line separator
+ * @see #LS
+ */
+ protected String getLineSeparator()
+ {
+ return lineSeparator;
+ }
+
+ /**
+ * Set the string used as line separator
+ *
+ * @param lineSeparator new line separator, could be null but the normal way is valid line separator
+ * ("\n" on UNIX).
+ */
+ protected void setLineSeparator( String lineSeparator )
+ {
+ this.lineSeparator = lineSeparator;
+ }
+
+ /**
+ * Write the end of line character (using specified line separator)
+ * and start new line with indentation
+ *
+ * @see #getLineIndenter()
+ * @see #getLineSeparator()
+ */
+ protected void endOfLine()
+ {
+ write( getLineSeparator() );
+
+ for ( int i = 0; i < getDepth(); i++ )
+ {
+ write( getLineIndenter() );
+ }
+ }
+
+ private void writeDocumentHeaders()
+ {
+ write( "<?xml version=\"1.0\"" );
+
+ if ( getEncoding() != null )
+ {
+ write( " encoding=\"" + getEncoding() + "\"" );
+ }
+
+ write( "?>" );
+
+ endOfLine();
+
+ if ( getDocType() != null )
+ {
+ write( "<!DOCTYPE " );
+
+ write( getDocType() );
+
+ write( ">" );
+
+ endOfLine();
+ }
+ }
+
+ /**
+ * Set the underlying writer
+ *
+ * @param writer not null writer
+ */
+ protected void setWriter( PrintWriter writer )
+ {
+ if ( writer == null )
+ {
+ throw new IllegalArgumentException( "writer could not be null");
+ }
+
+ this.writer = writer;
+ }
+
+ /**
+ * Get the underlying writer
+ *
+ * @return the underlying writer
+ */
+ protected PrintWriter getWriter()
+ {
+ return writer;
+ }
+
+ /**
+ * Set the depth in the xml indentation
+ *
+ * @param depth new depth
+ */
+ protected void setDepth( int depth )
+ {
+ this.depth = depth;
+ }
+
+ /**
+ * Get the current depth in the xml indentation
+ *
+ * @return the current depth
+ */
+ protected int getDepth()
+ {
+ return depth;
+ }
+
+ /**
+ * Set the encoding in the xml
+ *
+ * @param encoding new encoding
+ */
+ protected void setEncoding( String encoding )
+ {
+ this.encoding = encoding;
+ }
+
+ /**
+ * Get the current encoding in the xml
+ *
+ * @return the current encoding
+ */
+ protected String getEncoding()
+ {
+ return encoding;
+ }
+
+ /**
+ * Set the docType in the xml
+ *
+ * @param docType new docType
+ */
+ protected void setDocType( String docType )
+ {
+ this.docType = docType;
+ }
+
+ /**
+ * Get the docType in the xml
+ *
+ * @return the current docType
+ */
+ protected String getDocType()
+ {
+ return docType;
+ }
+
+ /**
+ * @return the current elementStack;
+ */
+ protected LinkedList getElementStack()
+ {
+ return elementStack;
+ }
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/PrettyPrintXMLWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,128 @@
+package org.apache.geronimo.system.plugin.plexus.util.xml;
+
+/*
+ * Copyright The Codehaus 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.
+ */
+
+import org.apache.geronimo.system.plugin.plexus.util.xml.pull.XmlSerializer;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Stack;
+
+/**
+ * Write to an MXSerializer.
+ *
+ * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
+ * @version $Id: SerializerXMLWriter.java 8010 2009-01-07 12:59:50Z vsiveton $
+ */
+public class SerializerXMLWriter
+ implements XMLWriter
+{
+ private final XmlSerializer serializer;
+
+ private final String namespace;
+
+ private final Stack elements = new Stack();
+
+ private List exceptions;
+
+ public SerializerXMLWriter( String namespace, XmlSerializer serializer )
+ {
+ this.serializer = serializer;
+ this.namespace = namespace;
+ }
+
+ public void startElement( String name )
+ {
+ try
+ {
+ serializer.startTag( namespace, name );
+ elements.push( name );
+ }
+ catch ( IOException e )
+ {
+ storeException( e );
+ }
+ }
+
+ public void addAttribute( String key, String value )
+ {
+ try
+ {
+ serializer.attribute( namespace, key, value );
+ }
+ catch ( IOException e )
+ {
+ storeException( e );
+ }
+ }
+
+ public void writeText( String text )
+ {
+ try
+ {
+ serializer.text( text );
+ }
+ catch ( IOException e )
+ {
+ storeException( e );
+ }
+ }
+
+ public void writeMarkup( String text )
+ {
+ try
+ {
+ serializer.cdsect( text );
+ }
+ catch ( IOException e )
+ {
+ storeException( e );
+ }
+ }
+
+ public void endElement()
+ {
+ try
+ {
+ serializer.endTag( namespace, (String) elements.pop() );
+ }
+ catch ( IOException e )
+ {
+ storeException( e );
+ }
+ }
+
+ /**
+ * @todo Maybe the interface should allow IOExceptions on each?
+ */
+ private void storeException( IOException e )
+ {
+ if ( exceptions == null )
+ {
+ exceptions = new ArrayList();
+ }
+ exceptions.add( e );
+ }
+
+ public List getExceptions()
+ {
+ return exceptions == null ? Collections.EMPTY_LIST : exceptions;
+ }
+
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/SerializerXMLWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,34 @@
+package org.apache.geronimo.system.plugin.plexus.util.xml;
+
+/*
+ * Copyright 2007 The Codehaus 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.
+ */
+
+/**
+ * @version $Id: XMLWriter.java 8010 2009-01-07 12:59:50Z vsiveton $
+ */
+public interface XMLWriter
+{
+ void startElement( String name );
+
+ void addAttribute( String key, String value );
+
+ void writeText( String text );
+
+ void writeMarkup( String text );
+
+ void endElement();
+
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XMLWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,786 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * 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.geronimo.system.plugin.plexus.util.xml;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.HttpURLConnection;
+import java.util.Locale;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.text.MessageFormat;
+
+/**
+ * Character stream that handles (or at least attemtps to) all the necessary Voodo to figure out the charset encoding of
+ * the XML document within the stream.
+ * <p>
+ * IMPORTANT: This class is not related in any way to the org.xml.sax.XMLReader. This one IS a character stream.
+ * <p>
+ * All this has to be done without consuming characters from the stream, if not the XML parser will not recognized the
+ * document as a valid XML. This is not 100% true, but it's close enough (UTF-8 BOM is not handled by all parsers right
+ * now, XmlReader handles it and things work in all parsers).
+ * <p>
+ * The XmlReader class handles the charset encoding of XML documents in Files, raw streams and HTTP streams by offering
+ * a wide set of constructors.
+ * <P>
+ * By default the charset encoding detection is lenient, the constructor with the lenient flag can be used for an script
+ * (following HTTP MIME and XML specifications). All this is nicely explained by Mark Pilgrim in his blog, <a
+ * href="http://diveintomark.org/archives/2004/02/13/xml-media-types"> Determining the character encoding of a feed</a>.
+ * <p>
+ *
+ * @author Alejandro Abdelnur
+ * @version revision 1.17 taken on 26/06/2007 from Rome (see https://rome.dev.java.net/source/browse/rome/src/java/com/sun/syndication/io/XmlReader.java)
+ * @deprecated use XmlStreamReader
+ * @since 1.4.3
+ */
+public class XmlReader extends Reader
+{
+ private static final int BUFFER_SIZE = 4096;
+
+ private static final String UTF_8 = "UTF-8";
+
+ private static final String US_ASCII = "US-ASCII";
+
+ private static final String UTF_16BE = "UTF-16BE";
+
+ private static final String UTF_16LE = "UTF-16LE";
+
+ private static final String UTF_16 = "UTF-16";
+
+ private static final String EBCDIC = "CP1047";
+
+ private static String _staticDefaultEncoding = null;
+
+ private Reader _reader;
+
+ private String _encoding;
+
+ private String _defaultEncoding;
+
+ /**
+ * Sets the default encoding to use if none is set in HTTP content-type, XML prolog and the rules based on
+ * content-type are not adequate. <p/> If it is set to NULL the content-type based rules are used. <p/> By default
+ * it is NULL. <p/>
+ *
+ * @param encoding
+ * charset encoding to default to.
+ */
+ public static void setDefaultEncoding( String encoding )
+ {
+ _staticDefaultEncoding = encoding;
+ }
+
+ /**
+ * Returns the default encoding to use if none is set in HTTP content-type, XML prolog and the rules based on
+ * content-type are not adequate. <p/> If it is NULL the content-type based rules are used. <p/>
+ *
+ * @return the default encoding to use.
+ */
+ public static String getDefaultEncoding()
+ {
+ return _staticDefaultEncoding;
+ }
+
+ /**
+ * Creates a Reader for a File.
+ * <p>
+ * It looks for the UTF-8 BOM first, if none sniffs the XML prolog charset, if this is also missing defaults to
+ * UTF-8.
+ * <p>
+ * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
+ * <p>
+ *
+ * @param file
+ * File to create a Reader from.
+ * @throws IOException
+ * thrown if there is a problem reading the file.
+ *
+ */
+ public XmlReader( File file ) throws IOException
+ {
+ this( new FileInputStream( file ) );
+ }
+
+ /**
+ * Creates a Reader for a raw InputStream.
+ * <p>
+ * It follows the same logic used for files.
+ * <p>
+ * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
+ * <p>
+ *
+ * @param is
+ * InputStream to create a Reader from.
+ * @throws IOException
+ * thrown if there is a problem reading the stream.
+ *
+ */
+ public XmlReader( InputStream is ) throws IOException
+ {
+ this( is, true );
+ }
+
+ /**
+ * Creates a Reader for a raw InputStream.
+ * <p>
+ * It follows the same logic used for files.
+ * <p>
+ * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
+ * following:
+ * <p>
+ * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
+ * <p>
+ * Else if the XML prolog had a charset encoding that encoding is used.
+ * <p>
+ * Else if the content type had a charset encoding that encoding is used.
+ * <p>
+ * Else 'UTF-8' is used.
+ * <p>
+ * If lenient detection is indicated an XmlStreamReaderException is never thrown.
+ * <p>
+ *
+ * @param is
+ * InputStream to create a Reader from.
+ * @param lenient
+ * indicates if the charset encoding detection should be relaxed.
+ * @throws IOException
+ * thrown if there is a problem reading the stream.
+ * @throws XmlStreamReaderException
+ * thrown if the charset encoding could not be determined according to the specs.
+ *
+ */
+ public XmlReader( InputStream is, boolean lenient ) throws IOException, XmlStreamReaderException
+ {
+ _defaultEncoding = _staticDefaultEncoding;
+ try
+ {
+ doRawStream( is, lenient );
+ }
+ catch ( XmlStreamReaderException ex )
+ {
+ if ( !lenient )
+ {
+ throw ex;
+ }
+ else
+ {
+ doLenientDetection( null, ex );
+ }
+ }
+ }
+
+ /**
+ * Creates a Reader using the InputStream of a URL.
+ * <p>
+ * If the URL is not of type HTTP and there is not 'content-type' header in the fetched data it uses the same logic
+ * used for Files.
+ * <p>
+ * If the URL is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for
+ * an InputStream with content-type.
+ * <p>
+ * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
+ * <p>
+ *
+ * @param url
+ * URL to create a Reader from.
+ * @throws IOException
+ * thrown if there is a problem reading the stream of the URL.
+ *
+ */
+ public XmlReader( URL url ) throws IOException
+ {
+ this( url.openConnection() );
+ }
+
+ /**
+ * Creates a Reader using the InputStream of a URLConnection.
+ * <p>
+ * If the URLConnection is not of type HttpURLConnection and there is not 'content-type' header in the fetched data
+ * it uses the same logic used for files.
+ * <p>
+ * If the URLConnection is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic
+ * used for an InputStream with content-type.
+ * <p>
+ * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
+ * <p>
+ *
+ * @param conn
+ * URLConnection to create a Reader from.
+ * @throws IOException
+ * thrown if there is a problem reading the stream of the URLConnection.
+ *
+ */
+ public XmlReader( URLConnection conn ) throws IOException
+ {
+ _defaultEncoding = _staticDefaultEncoding;
+ boolean lenient = true;
+ if ( conn instanceof HttpURLConnection )
+ {
+ try
+ {
+ doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
+ }
+ catch ( XmlStreamReaderException ex )
+ {
+ doLenientDetection( conn.getContentType(), ex );
+ }
+ }
+ else if ( conn.getContentType() != null )
+ {
+ try
+ {
+ doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
+ }
+ catch ( XmlStreamReaderException ex )
+ {
+ doLenientDetection( conn.getContentType(), ex );
+ }
+ }
+ else
+ {
+ try
+ {
+ doRawStream( conn.getInputStream(), lenient );
+ }
+ catch ( XmlStreamReaderException ex )
+ {
+ doLenientDetection( null, ex );
+ }
+ }
+ }
+
+ /**
+ * Creates a Reader using an InputStream an the associated content-type header.
+ * <p>
+ * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
+ * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
+ * encoding mandated by the content-type MIME type.
+ * <p>
+ * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
+ * <p>
+ *
+ * @param is
+ * InputStream to create the reader from.
+ * @param httpContentType
+ * content-type header to use for the resolution of the charset encoding.
+ * @throws IOException
+ * thrown if there is a problem reading the file.
+ *
+ */
+ public XmlReader( InputStream is, String httpContentType ) throws IOException
+ {
+ this( is, httpContentType, true );
+ }
+
+ /**
+ * Creates a Reader using an InputStream an the associated content-type header. This constructor is lenient
+ * regarding the encoding detection.
+ * <p>
+ * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
+ * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
+ * encoding mandated by the content-type MIME type.
+ * <p>
+ * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
+ * following:
+ * <p>
+ * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
+ * <p>
+ * Else if the XML prolog had a charset encoding that encoding is used.
+ * <p>
+ * Else if the content type had a charset encoding that encoding is used.
+ * <p>
+ * Else 'UTF-8' is used.
+ * <p>
+ * If lenient detection is indicated an XmlStreamReaderException is never thrown.
+ * <p>
+ *
+ * @param is
+ * InputStream to create the reader from.
+ * @param httpContentType
+ * content-type header to use for the resolution of the charset encoding.
+ * @param lenient
+ * indicates if the charset encoding detection should be relaxed.
+ * @throws IOException
+ * thrown if there is a problem reading the file.
+ * @throws XmlStreamReaderException
+ * thrown if the charset encoding could not be determined according to the specs.
+ *
+ */
+ public XmlReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding )
+ throws IOException, XmlStreamReaderException
+ {
+ _defaultEncoding = ( defaultEncoding == null ) ? _staticDefaultEncoding : defaultEncoding;
+ try
+ {
+ doHttpStream( is, httpContentType, lenient );
+ }
+ catch ( XmlStreamReaderException ex )
+ {
+ if ( !lenient )
+ {
+ throw ex;
+ }
+ else
+ {
+ doLenientDetection( httpContentType, ex );
+ }
+ }
+ }
+
+ /**
+ * Creates a Reader using an InputStream an the associated content-type header. This constructor is lenient
+ * regarding the encoding detection.
+ * <p>
+ * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
+ * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
+ * encoding mandated by the content-type MIME type.
+ * <p>
+ * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
+ * following:
+ * <p>
+ * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
+ * <p>
+ * Else if the XML prolog had a charset encoding that encoding is used.
+ * <p>
+ * Else if the content type had a charset encoding that encoding is used.
+ * <p>
+ * Else 'UTF-8' is used.
+ * <p>
+ * If lenient detection is indicated an XmlStreamReaderException is never thrown.
+ * <p>
+ *
+ * @param is
+ * InputStream to create the reader from.
+ * @param httpContentType
+ * content-type header to use for the resolution of the charset encoding.
+ * @param lenient
+ * indicates if the charset encoding detection should be relaxed.
+ * @throws IOException
+ * thrown if there is a problem reading the file.
+ * @throws XmlStreamReaderException
+ * thrown if the charset encoding could not be determined according to the specs.
+ *
+ */
+ public XmlReader( InputStream is, String httpContentType, boolean lenient ) throws IOException, XmlStreamReaderException
+ {
+ this( is, httpContentType, lenient, null );
+ }
+
+ private void doLenientDetection( String httpContentType, XmlStreamReaderException ex ) throws IOException
+ {
+ if ( httpContentType != null )
+ {
+ if ( httpContentType.startsWith( "text/html" ) )
+ {
+ httpContentType = httpContentType.substring( "text/html".length() );
+ httpContentType = "text/xml" + httpContentType;
+ try
+ {
+ doHttpStream( ex.getInputStream(), httpContentType, true );
+ ex = null;
+ }
+ catch ( XmlStreamReaderException ex2 )
+ {
+ ex = ex2;
+ }
+ }
+ }
+ if ( ex != null )
+ {
+ String encoding = ex.getXmlEncoding();
+ if ( encoding == null )
+ {
+ encoding = ex.getContentTypeEncoding();
+ }
+ if ( encoding == null )
+ {
+ encoding = ( _defaultEncoding == null ) ? UTF_8 : _defaultEncoding;
+ }
+ prepareReader( ex.getInputStream(), encoding );
+ }
+ }
+
+ /**
+ * Returns the charset encoding of the XmlReader.
+ * <p>
+ *
+ * @return charset encoding.
+ *
+ */
+ public String getEncoding()
+ {
+ return _encoding;
+ }
+
+ public int read( char[] buf, int offset, int len ) throws IOException
+ {
+ return _reader.read( buf, offset, len );
+ }
+
+ /**
+ * Closes the XmlReader stream.
+ * <p>
+ *
+ * @throws IOException
+ * thrown if there was a problem closing the stream.
+ *
+ */
+ public void close() throws IOException
+ {
+ _reader.close();
+ }
+
+ private void doRawStream( InputStream is, boolean lenient ) throws IOException
+ {
+ BufferedInputStream pis = new BufferedInputStream( is, BUFFER_SIZE );
+ String bomEnc = getBOMEncoding( pis );
+ String xmlGuessEnc = getXMLGuessEncoding( pis );
+ String xmlEnc = getXmlProlog( pis, xmlGuessEnc );
+ String encoding = calculateRawEncoding( bomEnc, xmlGuessEnc, xmlEnc, pis );
+ prepareReader( pis, encoding );
+ }
+
+ private void doHttpStream( InputStream is, String httpContentType, boolean lenient ) throws IOException
+ {
+ BufferedInputStream pis = new BufferedInputStream( is, BUFFER_SIZE );
+ String cTMime = getContentTypeMime( httpContentType );
+ String cTEnc = getContentTypeEncoding( httpContentType );
+ String bomEnc = getBOMEncoding( pis );
+ String xmlGuessEnc = getXMLGuessEncoding( pis );
+ String xmlEnc = getXmlProlog( pis, xmlGuessEnc );
+ String encoding = calculateHttpEncoding( cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, pis, lenient );
+ prepareReader( pis, encoding );
+ }
+
+ private void prepareReader( InputStream is, String encoding ) throws IOException
+ {
+ _reader = new InputStreamReader( is, encoding );
+ _encoding = encoding;
+ }
+
+ // InputStream is passed for XmlStreamReaderException creation only
+ private String calculateRawEncoding( String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is )
+ throws IOException
+ {
+ String encoding;
+ if ( bomEnc == null )
+ {
+ if ( xmlGuessEnc == null || xmlEnc == null )
+ {
+ encoding = ( _defaultEncoding == null ) ? UTF_8 : _defaultEncoding;
+ }
+ else if ( xmlEnc.equals( UTF_16 ) && ( xmlGuessEnc.equals( UTF_16BE ) || xmlGuessEnc.equals( UTF_16LE ) ) )
+ {
+ encoding = xmlGuessEnc;
+ }
+ else
+ {
+ encoding = xmlEnc;
+ }
+ }
+ else if ( bomEnc.equals( UTF_8 ) )
+ {
+ if ( xmlGuessEnc != null && !xmlGuessEnc.equals( UTF_8 ) )
+ {
+ throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
+ xmlGuessEnc, xmlEnc, is );
+ }
+ if ( xmlEnc != null && !xmlEnc.equals( UTF_8 ) )
+ {
+ throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
+ xmlGuessEnc, xmlEnc, is );
+ }
+ encoding = UTF_8;
+ }
+ else if ( bomEnc.equals( UTF_16BE ) || bomEnc.equals( UTF_16LE ) )
+ {
+ if ( xmlGuessEnc != null && !xmlGuessEnc.equals( bomEnc ) )
+ {
+ throw new IOException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ) );
+ }
+ if ( xmlEnc != null && !xmlEnc.equals( UTF_16 ) && !xmlEnc.equals( bomEnc ) )
+ {
+ throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
+ xmlGuessEnc, xmlEnc, is );
+ }
+ encoding = bomEnc;
+ }
+ else
+ {
+ throw new XmlStreamReaderException( RAW_EX_2.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
+ xmlGuessEnc, xmlEnc, is );
+ }
+ return encoding;
+ }
+
+ // InputStream is passed for XmlStreamReaderException creation only
+ private String calculateHttpEncoding( String cTMime, String cTEnc, String bomEnc, String xmlGuessEnc,
+ String xmlEnc, InputStream is, boolean lenient ) throws IOException
+ {
+ String encoding;
+ if ( lenient & xmlEnc != null )
+ {
+ encoding = xmlEnc;
+ }
+ else
+ {
+ boolean appXml = isAppXml( cTMime );
+ boolean textXml = isTextXml( cTMime );
+ if ( appXml || textXml )
+ {
+ if ( cTEnc == null )
+ {
+ if ( appXml )
+ {
+ encoding = calculateRawEncoding( bomEnc, xmlGuessEnc, xmlEnc, is );
+ }
+ else
+ {
+ encoding = ( _defaultEncoding == null ) ? US_ASCII : _defaultEncoding;
+ }
+ }
+ else if ( bomEnc != null && ( cTEnc.equals( UTF_16BE ) || cTEnc.equals( UTF_16LE ) ) )
+ {
+ throw new XmlStreamReaderException( HTTP_EX_1.format( new Object[] { cTMime, cTEnc, bomEnc, xmlGuessEnc,
+ xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
+ }
+ else if ( cTEnc.equals( UTF_16 ) )
+ {
+ if ( bomEnc != null && bomEnc.startsWith( UTF_16 ) )
+ {
+ encoding = bomEnc;
+ }
+ else
+ {
+ throw new XmlStreamReaderException( HTTP_EX_2.format( new Object[] { cTMime, cTEnc, bomEnc,
+ xmlGuessEnc, xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
+ }
+ }
+ else
+ {
+ encoding = cTEnc;
+ }
+ }
+ else
+ {
+ throw new XmlStreamReaderException( HTTP_EX_3.format( new Object[] { cTMime, cTEnc, bomEnc, xmlGuessEnc,
+ xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
+ }
+ }
+ return encoding;
+ }
+
+ // returns MIME type or NULL if httpContentType is NULL
+ private static String getContentTypeMime( String httpContentType )
+ {
+ String mime = null;
+ if ( httpContentType != null )
+ {
+ int i = httpContentType.indexOf( ";" );
+ mime = ( ( i == -1 ) ? httpContentType : httpContentType.substring( 0, i ) ).trim();
+ }
+ return mime;
+ }
+
+ private static final Pattern CHARSET_PATTERN = Pattern.compile( "charset=([.[^; ]]*)" );
+
+ // returns charset parameter value, NULL if not present, NULL if httpContentType is NULL
+ private static String getContentTypeEncoding( String httpContentType )
+ {
+ String encoding = null;
+ if ( httpContentType != null )
+ {
+ int i = httpContentType.indexOf( ";" );
+ if ( i > -1 )
+ {
+ String postMime = httpContentType.substring( i + 1 );
+ Matcher m = CHARSET_PATTERN.matcher( postMime );
+ encoding = ( m.find() ) ? m.group( 1 ) : null;
+ encoding = ( encoding != null ) ? encoding.toUpperCase( Locale.ENGLISH ) : null;
+ }
+ }
+ return encoding;
+ }
+
+ // returns the BOM in the stream, NULL if not present,
+ // if there was BOM the in the stream it is consumed
+ private static String getBOMEncoding( BufferedInputStream is ) throws IOException
+ {
+ String encoding = null;
+ int[] bytes = new int[3];
+ is.mark( 3 );
+ bytes[0] = is.read();
+ bytes[1] = is.read();
+ bytes[2] = is.read();
+
+ if ( bytes[0] == 0xFE && bytes[1] == 0xFF )
+ {
+ encoding = UTF_16BE;
+ is.reset();
+ is.read();
+ is.read();
+ }
+ else if ( bytes[0] == 0xFF && bytes[1] == 0xFE )
+ {
+ encoding = UTF_16LE;
+ is.reset();
+ is.read();
+ is.read();
+ }
+ else if ( bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF )
+ {
+ encoding = UTF_8;
+ }
+ else
+ {
+ is.reset();
+ }
+ return encoding;
+ }
+
+ // returns the best guess for the encoding by looking the first bytes of the stream, '<?'
+ private static String getXMLGuessEncoding( BufferedInputStream is ) throws IOException
+ {
+ String encoding = null;
+ int[] bytes = new int[4];
+ is.mark( 4 );
+ bytes[0] = is.read();
+ bytes[1] = is.read();
+ bytes[2] = is.read();
+ bytes[3] = is.read();
+ is.reset();
+
+ if ( bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F )
+ {
+ encoding = UTF_16BE;
+ }
+ else if ( bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00 )
+ {
+ encoding = UTF_16LE;
+ }
+ else if ( bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D )
+ {
+ encoding = UTF_8;
+ }
+ else if ( bytes[0] == 0x4C && bytes[1] == 0x6F && bytes[2] == 0xA7 && bytes[3] == 0x94 )
+ {
+ encoding = EBCDIC;
+ }
+ return encoding;
+ }
+
+ static final Pattern ENCODING_PATTERN =
+ Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE );
+
+ // returns the encoding declared in the <?xml encoding=...?>, NULL if none
+ private static String getXmlProlog( BufferedInputStream is, String guessedEnc ) throws IOException
+ {
+ String encoding = null;
+ if ( guessedEnc != null )
+ {
+ byte[] bytes = new byte[BUFFER_SIZE];
+ is.mark( BUFFER_SIZE );
+ int offset = 0;
+ int max = BUFFER_SIZE;
+ int c = is.read( bytes, offset, max );
+ int firstGT = -1;
+ String xmlProlog = null;
+ while ( c != -1 && firstGT == -1 && offset < BUFFER_SIZE )
+ {
+ offset += c;
+ max -= c;
+ c = is.read( bytes, offset, max );
+ xmlProlog = new String( bytes, 0, offset, guessedEnc );
+ firstGT = xmlProlog.indexOf( '>' );
+ }
+ if ( firstGT == -1 )
+ {
+ if ( c == -1 )
+ {
+ throw new IOException( "Unexpected end of XML stream" );
+ }
+ else
+ {
+ throw new IOException( "XML prolog or ROOT element not found on first " + offset + " bytes" );
+ }
+ }
+ int bytesRead = offset;
+ if ( bytesRead > 0 )
+ {
+ is.reset();
+ BufferedReader bReader = new BufferedReader( new StringReader( xmlProlog.substring( 0, firstGT + 1 ) ) );
+ StringBuffer prolog = new StringBuffer();
+ String line = bReader.readLine();
+ while ( line != null )
+ {
+ prolog.append( line );
+ line = bReader.readLine();
+ }
+ Matcher m = ENCODING_PATTERN.matcher( prolog );
+ if ( m.find() )
+ {
+ encoding = m.group( 1 ).toUpperCase( Locale.ENGLISH );
+ encoding = encoding.substring( 1, encoding.length() - 1 );
+ }
+ }
+ }
+ return encoding;
+ }
+
+ // indicates if the MIME type belongs to the APPLICATION XML family
+ private static boolean isAppXml( String mime )
+ {
+ return mime != null
+ && ( mime.equals( "application/xml" ) || mime.equals( "application/xml-dtd" )
+ || mime.equals( "application/xml-external-parsed-entity" ) || ( mime.startsWith( "application/" ) && mime.endsWith( "+xml" ) ) );
+ }
+
+ // indicates if the MIME type belongs to the TEXT XML family
+ private static boolean isTextXml( String mime )
+ {
+ return mime != null
+ && ( mime.equals( "text/xml" ) || mime.equals( "text/xml-external-parsed-entity" ) || ( mime.startsWith( "text/" ) && mime.endsWith( "+xml" ) ) );
+ }
+
+ private static final MessageFormat RAW_EX_1 =
+ new MessageFormat( "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch" );
+
+ private static final MessageFormat RAW_EX_2 =
+ new MessageFormat( "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM" );
+
+ private static final MessageFormat HTTP_EX_1 =
+ new MessageFormat(
+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL" );
+
+ private static final MessageFormat HTTP_EX_2 =
+ new MessageFormat(
+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch" );
+
+ private static final MessageFormat HTTP_EX_3 =
+ new MessageFormat(
+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME" );
+
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReader.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java?rev=821961&view=auto
==============================================================================
--- geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java (added)
+++ geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java Mon Oct 5 18:54:50 2009
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc.
+ *
+ * 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.geronimo.system.plugin.plexus.util.xml;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+/**
+ * The XmlReaderException is thrown by the XmlReader constructors if the charset encoding can not be determined
+ * according to the XML 1.0 specification and RFC 3023.
+ * <p>
+ * The exception returns the unconsumed InputStream to allow the application to do an alternate processing with the
+ * stream. Note that the original InputStream given to the XmlReader cannot be used as that one has been already read.
+ * <p>
+ *
+ * @author Alejandro Abdelnur
+ * @version revision 1.1 taken on 26/06/2007 from Rome (see https://rome.dev.java.net/source/browse/rome/src/java/com/sun/syndication/io/XmlReaderException.java)
+ */
+public class XmlReaderException extends IOException
+{
+ private String _bomEncoding;
+
+ private String _xmlGuessEncoding;
+
+ private String _xmlEncoding;
+
+ private String _contentTypeMime;
+
+ private String _contentTypeEncoding;
+
+ private InputStream _is;
+
+ /**
+ * Creates an exception instance if the charset encoding could not be determined.
+ * <p>
+ * Instances of this exception are thrown by the XmlReader.
+ * <p>
+ *
+ * @param msg
+ * message describing the reason for the exception.
+ * @param bomEnc
+ * BOM encoding.
+ * @param xmlGuessEnc
+ * XML guess encoding.
+ * @param xmlEnc
+ * XML prolog encoding.
+ * @param is
+ * the unconsumed InputStream.
+ *
+ */
+ public XmlReaderException( String msg, String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is )
+ {
+ this( msg, null, null, bomEnc, xmlGuessEnc, xmlEnc, is );
+ }
+
+ /**
+ * Creates an exception instance if the charset encoding could not be determined.
+ * <p>
+ * Instances of this exception are thrown by the XmlReader.
+ * <p>
+ *
+ * @param msg
+ * message describing the reason for the exception.
+ * @param ctMime
+ * MIME type in the content-type.
+ * @param ctEnc
+ * encoding in the content-type.
+ * @param bomEnc
+ * BOM encoding.
+ * @param xmlGuessEnc
+ * XML guess encoding.
+ * @param xmlEnc
+ * XML prolog encoding.
+ * @param is
+ * the unconsumed InputStream.
+ *
+ */
+ public XmlReaderException( String msg, String ctMime, String ctEnc, String bomEnc, String xmlGuessEnc,
+ String xmlEnc, InputStream is )
+ {
+ super( msg );
+ _contentTypeMime = ctMime;
+ _contentTypeEncoding = ctEnc;
+ _bomEncoding = bomEnc;
+ _xmlGuessEncoding = xmlGuessEnc;
+ _xmlEncoding = xmlEnc;
+ _is = is;
+ }
+
+ /**
+ * Returns the BOM encoding found in the InputStream.
+ * <p>
+ *
+ * @return the BOM encoding, null if none.
+ *
+ */
+ public String getBomEncoding()
+ {
+ return _bomEncoding;
+ }
+
+ /**
+ * Returns the encoding guess based on the first bytes of the InputStream.
+ * <p>
+ *
+ * @return the encoding guess, null if it couldn't be guessed.
+ *
+ */
+ public String getXmlGuessEncoding()
+ {
+ return _xmlGuessEncoding;
+ }
+
+ /**
+ * Returns the encoding found in the XML prolog of the InputStream.
+ * <p>
+ *
+ * @return the encoding of the XML prolog, null if none.
+ *
+ */
+ public String getXmlEncoding()
+ {
+ return _xmlEncoding;
+ }
+
+ /**
+ * Returns the MIME type in the content-type used to attempt determining the encoding.
+ * <p>
+ *
+ * @return the MIME type in the content-type, null if there was not content-type or the encoding detection did not
+ * involve HTTP.
+ *
+ */
+ public String getContentTypeMime()
+ {
+ return _contentTypeMime;
+ }
+
+ /**
+ * Returns the encoding in the content-type used to attempt determining the encoding.
+ * <p>
+ *
+ * @return the encoding in the content-type, null if there was not content-type, no encoding in it or the encoding
+ * detection did not involve HTTP.
+ *
+ */
+ public String getContentTypeEncoding()
+ {
+ return _contentTypeEncoding;
+ }
+
+ /**
+ * Returns the unconsumed InputStream to allow the application to do an alternate encoding detection on the
+ * InputStream.
+ * <p>
+ *
+ * @return the unconsumed InputStream.
+ *
+ */
+ public InputStream getInputStream()
+ {
+ return _is;
+ }
+}
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/djencks/osgi/framework/modules/geronimo-plugin/src/main/java/org/apache/geronimo/system/plugin/plexus/util/xml/XmlReaderException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain