Author: hboutemy Date: Mon Jun 13 15:16:39 2011 New Revision: 1135135 URL: http://svn.apache.org/viewvc?rev=1135135&view=rev Log: added ReaderFactory and XmlStreamReader
Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java (with props) maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java (with props) maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java (with props) maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java (with props) maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java (with props) maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java (with props) Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java Mon Jun 13 15:16:39 2011 @@ -0,0 +1,222 @@ +package org.codehaus.plexus.util; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.nio.charset.Charset; + +import org.codehaus.plexus.util.xml.XmlStreamReader; + +/** + * Utility to create Readers from streams, with explicit encoding choice: platform default, + * XML, or specified. + * + * @author <a href="mailto:hbout...@apache.org">Hervé Boutemy</a> + * @see Charset + * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> + * @since 1.4.3 + */ +public class ReaderFactory +{ + /** + * ISO Latin Alphabet #1, also known as ISO-LATIN-1. + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String ISO_8859_1 = "ISO-8859-1"; + + /** + * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String US_ASCII = "US-ASCII"; + + /** + * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either + * order accepted on input, big-endian used on output). + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String UTF_16 = "UTF-16"; + + /** + * Sixteen-bit Unicode Transformation Format, big-endian byte order. + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String UTF_16BE = "UTF-16BE"; + + /** + * Sixteen-bit Unicode Transformation Format, little-endian byte order. + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String UTF_16LE = "UTF-16LE"; + + /** + * Eight-bit Unicode Transformation Format. + * Every implementation of the Java platform is required to support this character encoding. + * @see Charset + */ + public static final String UTF_8 = "UTF-8"; + + /** + * The <code>file.encoding</code> System Property. + */ + public static final String FILE_ENCODING = System.getProperty( "file.encoding" ); + + /** + * Create a new Reader with XML encoding detection rules. + * + * @param in not null input stream. + * @return an XML reader instance for the input stream. + * @throws IOException if any. + * @see XmlStreamReader + */ + public static XmlStreamReader newXmlReader( InputStream in ) + throws IOException + { + return new XmlStreamReader( in ); + } + + /** + * Create a new Reader with XML encoding detection rules. + * + * @param file not null file. + * @return an XML reader instance for the input file. + * @throws IOException if any. + * @see XmlStreamReader + */ + public static XmlStreamReader newXmlReader( File file ) + throws IOException + { + return new XmlStreamReader( file ); + } + + /** + * Create a new Reader with XML encoding detection rules. + * + * @param url not null url. + * @return an XML reader instance for the input url. + * @throws IOException if any. + * @see XmlStreamReader + */ + public static XmlStreamReader newXmlReader( URL url ) + throws IOException + { + return new XmlStreamReader( url ); + } + + /** + * Create a new Reader with default plaform encoding. + * + * @param in not null input stream. + * @return a reader instance for the input stream using the default platform charset. + * @see Charset#defaultCharset() + */ + public static Reader newPlatformReader( InputStream in ) + { + return new InputStreamReader( in ); + } + + /** + * Create a new Reader with default plaform encoding. + * + * @param file not null file. + * @return a reader instance for the input file using the default platform charset. + * @throws FileNotFoundException if any. + * @see Charset#defaultCharset() + */ + public static Reader newPlatformReader( File file ) + throws FileNotFoundException + { + return new FileReader( file ); + } + + /** + * Create a new Reader with default plaform encoding. + * + * @param url not null url. + * @return a reader instance for the input url using the default platform charset. + * @throws IOException if any. + * @see Charset#defaultCharset() + */ + public static Reader newPlatformReader( URL url ) + throws IOException + { + return new InputStreamReader( url.openStream() ); + } + + /** + * Create a new Reader with specified encoding. + * + * @param in not null input stream. + * @param encoding not null supported encoding. + * @return a reader instance for the input stream using the given encoding. + * @throws UnsupportedEncodingException if any. + * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> + */ + public static Reader newReader( InputStream in, String encoding ) + throws UnsupportedEncodingException + { + return new InputStreamReader( in, encoding ); + } + + /** + * Create a new Reader with specified encoding. + * + * @param file not null file. + * @param encoding not null supported encoding. + * @return a reader instance for the input file using the given encoding. + * @throws FileNotFoundException if any. + * @throws UnsupportedEncodingException if any. + * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> + */ + public static Reader newReader( File file, String encoding ) + throws FileNotFoundException, UnsupportedEncodingException + { + return new InputStreamReader( new FileInputStream(file), encoding ); + } + + /** + * Create a new Reader with specified encoding. + * + * @param url not null url. + * @param encoding not null supported encoding. + * @return a reader instance for the input url using the given encoding. + * @throws IOException if any. + * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> + */ + public static Reader newReader( URL url, String encoding ) + throws IOException + { + return new InputStreamReader( url.openStream(), encoding ); + } +} Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/ReaderFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java Mon Jun 13 15:16:39 2011 @@ -0,0 +1,121 @@ +package org.codehaus.plexus.util.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.net.URL; +import java.net.URLConnection; +import java.util.regex.Pattern; + +/** + * @deprecated use XmlStreamReader + */ +public class XmlReader + extends Reader +{ + private final org.apache.commons.io.input.XmlStreamReader reader; + + private static String _staticDefaultEncoding = null; + + public static void setDefaultEncoding( String encoding ) + { + _staticDefaultEncoding = encoding; + } + + public static String getDefaultEncoding() + { + return _staticDefaultEncoding; + } + + public XmlReader( File file ) + throws IOException + { + this( new FileInputStream( file ) ); + } + + public XmlReader( InputStream is ) + throws IOException + { + this( is, true ); + } + + public XmlReader( InputStream is, boolean lenient ) + throws IOException, XmlStreamReaderException + { + reader = new org.apache.commons.io.input.XmlStreamReader( is, lenient, _staticDefaultEncoding ); + } + + public XmlReader( URL url ) + throws IOException + { + this( url.openConnection() ); + } + + public XmlReader( URLConnection conn ) + throws IOException + { + reader = new org.apache.commons.io.input.XmlStreamReader( conn, _staticDefaultEncoding ); + } + + public XmlReader( InputStream is, String httpContentType ) + throws IOException + { + this( is, httpContentType, true ); + } + + public XmlReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding ) + throws IOException, XmlStreamReaderException + { + reader = + new org.apache.commons.io.input.XmlStreamReader( is, httpContentType, lenient, + ( defaultEncoding == null ) ? _staticDefaultEncoding + : defaultEncoding ); + } + + public XmlReader( InputStream is, String httpContentType, boolean lenient ) + throws IOException, XmlStreamReaderException + { + this( is, httpContentType, lenient, null ); + } + + public String getEncoding() + { + return reader.getEncoding(); + } + + public int read( char[] buf, int offset, int len ) + throws IOException + { + return reader.read( buf, offset, len ); + } + + public void close() + throws IOException + { + reader.close(); + } + + static final Pattern ENCODING_PATTERN = + Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE ); +} Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReader.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java Mon Jun 13 15:16:39 2011 @@ -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.codehaus.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: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlReaderException.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java Mon Jun 13 15:16:39 2011 @@ -0,0 +1,79 @@ +package org.codehaus.plexus.util.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +@Deprecated +public class XmlStreamReader + extends XmlReader +{ + public XmlStreamReader( File file ) + throws IOException + { + super( file ); + } + + public XmlStreamReader( InputStream is ) + throws IOException + { + super( is ); + } + + public XmlStreamReader( InputStream is, boolean lenient ) + throws IOException, XmlStreamReaderException + { + super( is, lenient ); + } + + public XmlStreamReader( URL url ) + throws IOException + { + super( url ); + } + + public XmlStreamReader( URLConnection conn ) + throws IOException + { + super( conn ); + } + + public XmlStreamReader( InputStream is, String httpContentType ) + throws IOException + { + super( is, httpContentType ); + } + + public XmlStreamReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding ) + throws IOException, XmlStreamReaderException + { + super( is, httpContentType, lenient, defaultEncoding ); + } + + public XmlStreamReader( InputStream is, String httpContentType, boolean lenient ) + throws IOException, XmlStreamReaderException + { + super( is, httpContentType, lenient ); + } +} Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReader.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java Mon Jun 13 15:16:39 2011 @@ -0,0 +1,84 @@ +/* + * 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.codehaus.plexus.util.xml; + +import java.io.InputStream; + +/** + * The XmlStreamReaderException is thrown by the XmlStreamReader 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 XmlStreamReader 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 XmlStreamReaderException extends XmlReaderException +{ + /** + * 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 XmlStreamReaderException( String msg, String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is ) + { + super( msg, 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 XmlStreamReaderException( String msg, String ctMime, String ctEnc, String bomEnc, String xmlGuessEnc, + String xmlEnc, InputStream is ) + { + super( msg, ctMime, ctEnc, bomEnc, xmlGuessEnc, xmlEnc, is ); + } +} Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/xml/XmlStreamReaderException.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java?rev=1135135&view=auto ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java (added) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java Mon Jun 13 15:16:39 2011 @@ -0,0 +1,226 @@ +package org.codehaus.plexus.util.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.SequenceInputStream; + +import org.codehaus.plexus.util.IOUtil; + +import junit.framework.ComparisonFailure; +import junit.framework.TestCase; + +/** + * + * @author <a href="mailto:hbout...@apache.org">Hervé Boutemy</a> + */ +public class XmlStreamReaderTest + extends TestCase +{ + /** french */ + private static final String TEXT_LATIN1 = "eacute: \u00E9"; + /** greek */ + private static final String TEXT_LATIN7 = "alpha: \u03B1"; + /** euro support */ + private static final String TEXT_LATIN15 = "euro: \u20AC"; + /** japanese */ + private static final String TEXT_EUC_JP = "hiragana A: \u3042"; + /** Unicode: support everything */ + private static final String TEXT_UNICODE = + TEXT_LATIN1 + ", " + + TEXT_LATIN7 + ", " + + TEXT_LATIN15 + ", " + + TEXT_EUC_JP; + /** see http://unicode.org/faq/utf_bom.html#BOM */ + private static final byte[] BOM_UTF8 = { (byte)0xEF, (byte)0xBB, (byte)0xBF }; + private static final byte[] BOM_UTF16BE = { (byte)0xFE, (byte)0xFF }; + private static final byte[] BOM_UTF16LE = { (byte)0xFF, (byte)0xFE }; + private static final byte[] BOM_UTF32BE = { (byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFE }; + private static final byte[] BOM_UTF32LE = { (byte)0xFF, (byte)0xFE, (byte)0x00, (byte)0x00 }; + + private static String createXmlContent( String text, String encoding ) + { + String xmlDecl = "<?xml version=\"1.0\"?>"; + if ( encoding != null ) + { + xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>"; + } + String xml = xmlDecl + "\n<text>" + text + "</text>"; + return xml; + } + + private static void checkXmlContent( String xml, String encoding ) + throws IOException + { + checkXmlContent( xml, encoding, null ); + } + + private static void checkXmlContent( String xml, String encoding, byte[] bom ) + throws IOException + { + byte[] xmlContent = xml.getBytes( encoding ); + InputStream in = new ByteArrayInputStream( xmlContent ); + + if ( bom != null ) + { + in = new SequenceInputStream( new ByteArrayInputStream( bom ), in ); + } + + XmlStreamReader reader = new XmlStreamReader( in ); + assertEquals( encoding, reader.getEncoding() ); + String result = IOUtil.toString( reader ); + assertEquals( xml, result ); + } + + private static void checkXmlStreamReader( String text, String encoding, String effectiveEncoding ) + throws IOException + { + checkXmlStreamReader( text, encoding, effectiveEncoding, null ); + } + + private static void checkXmlStreamReader( String text, String encoding ) + throws IOException + { + checkXmlStreamReader( text, encoding, encoding, null ); + } + + private static void checkXmlStreamReader( String text, String encoding, byte[] bom ) + throws IOException + { + checkXmlStreamReader( text, encoding, encoding, bom ); + } + + private static void checkXmlStreamReader( String text, String encoding, String effectiveEncoding, byte[] bom ) + throws IOException + { + String xml = createXmlContent( text, encoding ); + checkXmlContent( xml, effectiveEncoding, bom ); + } + + public void testNoXmlHeader() + throws IOException + { + String xml = "<text>text with no XML header</text>"; + checkXmlContent( xml, "UTF-8" ); + checkXmlContent( xml, "UTF-8", BOM_UTF8 ); + } + + public void testDefaultEncoding() + throws IOException + { + checkXmlStreamReader( TEXT_UNICODE, null, "UTF-8" ); + checkXmlStreamReader( TEXT_UNICODE, null, "UTF-8", BOM_UTF8 ); + } + + public void testUTF8Encoding() + throws IOException + { + checkXmlStreamReader( TEXT_UNICODE, "UTF-8" ); + checkXmlStreamReader( TEXT_UNICODE, "UTF-8", BOM_UTF8 ); + } + + public void testUTF16Encoding() + throws IOException + { + checkXmlStreamReader( TEXT_UNICODE, "UTF-16", "UTF-16BE", null ); + checkXmlStreamReader( TEXT_UNICODE, "UTF-16", "UTF-16LE", BOM_UTF16LE ); + checkXmlStreamReader( TEXT_UNICODE, "UTF-16", "UTF-16BE", BOM_UTF16BE ); + } + + public void testUTF16BEEncoding() + throws IOException + { + checkXmlStreamReader( TEXT_UNICODE, "UTF-16BE" ); + } + + public void testUTF16LEEncoding() + throws IOException + { + checkXmlStreamReader( TEXT_UNICODE, "UTF-16LE" ); + } + + public void testLatin1Encoding() + throws IOException + { + checkXmlStreamReader( TEXT_LATIN1, "ISO-8859-1" ); + } + + public void testLatin7Encoding() + throws IOException + { + checkXmlStreamReader( TEXT_LATIN7, "ISO-8859-7" ); + } + + public void testLatin15Encoding() + throws IOException + { + checkXmlStreamReader( TEXT_LATIN15, "ISO-8859-15" ); + } + + public void testEUC_JPEncoding() + throws IOException + { + checkXmlStreamReader( TEXT_EUC_JP, "EUC-JP" ); + } + + public void testEBCDICEncoding() + throws IOException + { + checkXmlStreamReader( "simple text in EBCDIC", "CP1047" ); + } + + public void testInappropriateEncoding() + throws IOException + { + try + { + checkXmlStreamReader( TEXT_UNICODE, "ISO-8859-2" ); + fail( "Check should have failed, since some characters are not available in the specified encoding" ); + } + catch ( ComparisonFailure cf ) + { + // expected failure, since the encoding does not contain some characters + } + } + + public void testEncodingAttribute() + throws IOException + { + String xml = "<?xml version='1.0' encoding='US-ASCII'?><element encoding='attribute value'/>"; + checkXmlContent( xml, "US-ASCII" ); + + xml = "<?xml version='1.0' encoding = 'US-ASCII' ?><element encoding='attribute value'/>"; + checkXmlContent( xml, "US-ASCII" ); + + xml = "<?xml version='1.0'?><element encoding='attribute value'/>"; + checkXmlContent( xml, "UTF-8" ); + + xml = "<?xml\nversion='1.0'\nencoding\n=\n'US-ASCII'\n?>\n<element encoding='attribute value'/>"; + checkXmlContent( xml, "US-ASCII" ); + + xml = "<?xml\nversion='1.0'\n?>\n<element encoding='attribute value'/>"; + checkXmlContent( xml, "UTF-8" ); + + xml = "<element encoding='attribute value'/>"; + checkXmlContent( xml, "UTF-8" ); + } +} Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain