You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/11/28 00:18:00 UTC
svn commit: r598815 - in /incubator/abdera/java/trunk:
dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/
parser/src/main/java/org/apache/abdera/parser/stax/util/
Author: jmsnell
Date: Tue Nov 27 15:17:59 2007
New Revision: 598815
URL: http://svn.apache.org/viewvc?rev=598815&view=rev
Log:
More refactorings
Added:
incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/CharsetSniffingInputStream.java
Removed:
incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java
Modified:
incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMSniffingInputStream.java
incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionInputStream.java
incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionReader.java
Added: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/CharsetSniffingInputStream.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/CharsetSniffingInputStream.java?rev=598815&view=auto
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/CharsetSniffingInputStream.java (added)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/CharsetSniffingInputStream.java Tue Nov 27 15:17:59 2007
@@ -0,0 +1,113 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. 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. For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.i18n.io;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Will attempt to autodetect the character encoding from the stream
+ * This will preserve the BOM if it exists
+ */
+public class CharsetSniffingInputStream
+ extends FilterInputStream {
+
+ private static final byte[] UTF32be = new byte[] {0x00,0x00,0xFFFFFFFE,0xFFFFFFFF};
+ private static final byte[] UTF32le = new byte[] {0xFFFFFFFF,0xFFFFFFFE,0x00,0x00};
+ private static final byte[] INVALID1 = new byte[] {0xFFFFFFFE,0xFFFFFFFF,0x00,0x00};
+ private static final byte[] INVALID2 = new byte[] {0x00,0x00,0xFFFFFFFF,0xFFFFFFFE};
+ private static final byte[] UTF16be = new byte[] {0xFFFFFFFE,0xFFFFFFFF};
+ private static final byte[] UTF16le = new byte[] {0xFFFFFFFF,0xFFFFFFFE};
+ private static final byte[] UTF8 = new byte[] {0xFFFFFFEF,0xFFFFFFBB,0xFFFFFFBF};
+ private static final byte[] UTF32be2 = new byte[] {0x00,0x00,0x00,0x3C};
+ private static final byte[] UTF32le2 = new byte[] {0x3C,0x00,0x00,0x00};
+ private static final byte[] UTF16be2 = new byte[] {0x00,0x3C,0x00,0x3F};
+ private static final byte[] UTF16le2 = new byte[] {0x3C,0x00,0x3F,0x00};
+
+ protected String encoding;
+ protected boolean bomset = false;
+
+ public CharsetSniffingInputStream(InputStream in) {
+ super(
+ !(in instanceof PeekAheadInputStream) ?
+ new PeekAheadInputStream(in,4) : in);
+ try {
+ encoding = detectEncoding();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public boolean isBomSet() {
+ return bomset;
+ }
+
+ public String getEncoding() {
+ return encoding;
+ }
+
+ protected PeekAheadInputStream getInternal() {
+ return (PeekAheadInputStream)in;
+ }
+
+ private static boolean equals(byte[] a1, int len, byte[] a2) {
+ for (int n = 0, i = 0; n < len; n++, i++) {
+ if (a1[n] != a2[i]) return false;
+ }
+ return true;
+ }
+
+ protected String detectEncoding() throws IOException {
+ PeekAheadInputStream pin = (PeekAheadInputStream) this.in;
+ byte[] bom = new byte[4];
+ pin.peek(bom);
+ String charset = null;
+ if (equals(bom,4,UTF32be)) {
+ bomset = true;
+ return "utf-32";
+ } else if (equals(bom,4,UTF32le)) {
+ bomset = true;
+ return "utf-32";
+ } else if ((equals(bom,4,INVALID1)) ||
+ (equals(bom,4,INVALID2))) {
+ bomset = true;
+ return null;
+ } else if (equals(bom,2,UTF16be)) {
+ bomset = true;
+ return "utf-16";
+ } else if (equals(bom,2,UTF16le)) {
+ bomset = true;
+ return "utf-16";
+ } else if (equals(bom,3,UTF8)) {
+ bomset = true;
+ return "utf-8";
+ } else if (equals(bom,4,UTF32be2)) {
+ charset = "utf-32be";
+ } else if (equals(bom,4,UTF32le2)) {
+ charset = "utf-32le";
+ } else if (equals(bom,4,UTF16be2)) {
+ charset = "utf-16be";
+ } else if (equals(bom,4,UTF16le2)) {
+ charset = "utf-16le";
+ }
+ bomset = false;
+ return charset;
+ }
+
+}
Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMSniffingInputStream.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMSniffingInputStream.java?rev=598815&r1=598814&r2=598815&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMSniffingInputStream.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMSniffingInputStream.java Tue Nov 27 15:17:59 2007
@@ -17,93 +17,29 @@
*/
package org.apache.abdera.parser.stax.util;
-import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
+import org.apache.abdera.i18n.io.CharsetSniffingInputStream;
+import org.apache.abdera.i18n.io.PeekAheadInputStream;
+
/**
* Will attempt to autodetect the character encoding from the stream
- * This will preserve the BOM if it exists
+ * This will preserve the BOM if it exists.
*/
public class FOMSniffingInputStream
- extends FilterInputStream {
+ extends CharsetSniffingInputStream {
- private static byte[] UTF32be = new byte[] {0x00,0x00,0xFFFFFFFE,0xFFFFFFFF};
- private static byte[] UTF32le = new byte[] {0xFFFFFFFF,0xFFFFFFFE,0x00,0x00};
- private static byte[] INVALID1 = new byte[] {0xFFFFFFFE,0xFFFFFFFF,0x00,0x00};
- private static byte[] INVALID2 = new byte[] {0x00,0x00,0xFFFFFFFF,0xFFFFFFFE};
- private static byte[] UTF16be = new byte[] {0xFFFFFFFE,0xFFFFFFFF};
- private static byte[] UTF16le = new byte[] {0xFFFFFFFF,0xFFFFFFFE};
- private static byte[] UTF8 = new byte[] {0xFFFFFFEF,0xFFFFFFBB,0xFFFFFFBF};
- private static byte[] UTF32be2 = new byte[] {0x00,0x00,0x00,0x3C};
- private static byte[] UTF32le2 = new byte[] {0x3C,0x00,0x00,0x00};
- private static byte[] UTF16be2 = new byte[] {0x00,0x3C,0x00,0x3F};
- private static byte[] UTF16le2 = new byte[] {0x3C,0x00,0x3F,0x00};
-
- private String encoding = null;
- private boolean bomset = false;
-
public FOMSniffingInputStream(InputStream in) {
- super(new PeekAheadInputStream(in,4));
- try {
- encoding = detectEncoding();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ super(in);
}
- public boolean isBomSet() {
- return bomset;
- }
-
- public String getEncoding() {
- return encoding;
- }
-
- private boolean equals(byte[] a1, int len, byte[] a2) {
- for (int n = 0, i = 0; n < len; n++, i++) {
- if (a1[n] != a2[i]) return false;
- }
- return true;
- }
-
- private String detectEncoding() throws IOException {
- PeekAheadInputStream pin = (PeekAheadInputStream) this.in;
- byte[] bom = new byte[4];
- pin.peek(bom);
- String charset = null;
- if (equals(bom,4,UTF32be)) {
- bomset = true;
- return "utf-32";
- } else if (equals(bom,4,UTF32le)) {
- bomset = true;
- return "utf-32";
- } else if ((equals(bom,4,INVALID1)) ||
- (equals(bom,4,INVALID2))) {
- bomset = true;
- return null;
- } else if (equals(bom,2,UTF16be)) {
- bomset = true;
- return "utf-16";
- } else if (equals(bom,2,UTF16le)) {
- bomset = true;
- return "utf-16";
- } else if (equals(bom,3,UTF8)) {
- bomset = true;
- return "utf-8";
- } else if (equals(bom,4,UTF32be2)) {
- charset = "utf-32be";
- } else if (equals(bom,4,UTF32le2)) {
- charset = "utf-32le";
- } else if (equals(bom,4,UTF16be2)) {
- charset = "utf-16be";
- } else if (equals(bom,4,UTF16le2)) {
- charset = "utf-16le";
- }
- bomset = false;
+ protected String detectEncoding() throws IOException {
+ String charset = super.detectEncoding();
+ PeekAheadInputStream pin = getInternal();
try {
byte[] p = new byte[200];
pin.peek(p);
Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionInputStream.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionInputStream.java?rev=598815&r1=598814&r2=598815&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionInputStream.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionInputStream.java Tue Nov 27 15:17:59 2007
@@ -23,6 +23,7 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
+import org.apache.abdera.i18n.io.PeekAheadInputStream;
/**
* Will attempt to autodetect the character encoding from the stream
Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionReader.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionReader.java?rev=598815&r1=598814&r2=598815&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionReader.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlVersionReader.java Tue Nov 27 15:17:59 2007
@@ -51,7 +51,6 @@
String v = xmlreader.getVersion();
if (v != null) version = v;
unread(p,0,r);
-
} catch (Exception e) {}
return version;
}