Author: cschneider
Date: Fri Sep 16 07:54:53 2011
New Revision: 1171430
URL: http://svn.apache.org/viewvc?rev=1171430&view=rev
Log:
Moving some helper methods from converter to util and adding deprecated stubs
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
Fri Sep 16 07:54:53 2011
@@ -32,7 +32,6 @@ import org.apache.camel.Expression;
import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.Message;
import org.apache.camel.Processor;
-import org.apache.camel.converter.IOConverter;
import org.apache.camel.impl.ScheduledPollEndpoint;
import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
import org.apache.camel.spi.BrowsableEndpoint;
@@ -40,6 +39,7 @@ import org.apache.camel.spi.FactoryFinde
import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.spi.Language;
import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ServiceHelper;
import org.apache.camel.util.StringHelper;
@@ -360,7 +360,7 @@ public abstract class GenericFileEndpoin
}
public void setCharset(String charset) {
- IOConverter.validateCharset(charset);
+ IOHelper.validateCharset(charset);
this.charset = charset;
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Fri Sep 16 07:54:53 2011
@@ -40,13 +40,11 @@ import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
-import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -86,7 +84,7 @@ public final class IOConverter {
@Converter
public static BufferedReader toReader(File file, Exchange exchange) throws
IOException {
- return new BufferedReader(new EncodingFileReader(file,
getCharsetName(exchange)));
+ return new BufferedReader(new EncodingFileReader(file,
IOHelper.getCharsetName(exchange)));
}
@Converter
@@ -109,7 +107,7 @@ public final class IOConverter {
@Converter
public static BufferedWriter toWriter(File file, Exchange exchange) throws
IOException {
- return new BufferedWriter(new EncodingFileWriter(file,
getCharsetName(exchange)));
+ return new BufferedWriter(new EncodingFileWriter(file,
IOHelper.getCharsetName(exchange)));
}
/**
@@ -122,7 +120,7 @@ public final class IOConverter {
@Converter
public static Reader toReader(InputStream in, Exchange exchange) throws
IOException {
- return new InputStreamReader(in, getCharsetName(exchange));
+ return new InputStreamReader(in, IOHelper.getCharsetName(exchange));
}
/**
@@ -135,7 +133,7 @@ public final class IOConverter {
@Converter
public static Writer toWriter(OutputStream out, Exchange exchange) throws
IOException {
- return new OutputStreamWriter(out, getCharsetName(exchange));
+ return new OutputStreamWriter(out, IOHelper.getCharsetName(exchange));
}
@Converter
@@ -153,7 +151,7 @@ public final class IOConverter {
@Converter
public static InputStream toInputStream(String text, Exchange exchange)
throws IOException {
- return toInputStream(text.getBytes(getCharsetName(exchange)));
+ return toInputStream(text.getBytes(IOHelper.getCharsetName(exchange)));
}
/**
@@ -179,7 +177,7 @@ public final class IOConverter {
@Converter
public static String toString(byte[] data, Exchange exchange) throws
IOException {
- return new String(data, getCharsetName(exchange));
+ return new String(data, IOHelper.getCharsetName(exchange));
}
/**
@@ -296,7 +294,7 @@ public final class IOConverter {
@Converter
public static byte[] toByteArray(String value, Exchange exchange) throws
IOException {
- return value != null ? value.getBytes(getCharsetName(exchange)) : null;
+ return value != null ?
value.getBytes(IOHelper.getCharsetName(exchange)) : null;
}
/**
@@ -361,7 +359,7 @@ public final class IOConverter {
@Converter
public static String toString(ByteArrayOutputStream os, Exchange exchange)
throws IOException {
- return os.toString(getCharsetName(exchange));
+ return os.toString(IOHelper.getCharsetName(exchange));
}
@Converter
@@ -369,10 +367,6 @@ public final class IOConverter {
return new ByteArrayInputStream(os.toByteArray());
}
- public static String getCharsetName(Exchange exchange) {
- return getCharsetName(exchange, true);
- }
-
/**
* Gets the charset name if set as property {@link Exchange#CHARSET_NAME}.
*
@@ -380,24 +374,11 @@ public final class IOConverter {
* @param useDefault should we fallback and use JVM default charset if no
property existed?
* @return the charset, or <tt>null</tt> if no found
*/
+ @Deprecated
public static String getCharsetName(Exchange exchange, boolean useDefault)
{
- if (exchange != null) {
- String charsetName = exchange.getProperty(Exchange.CHARSET_NAME,
String.class);
- if (charsetName != null) {
- return IOConverter.normalizeCharset(charsetName);
- }
- }
- if (useDefault) {
- return getDefaultCharsetName();
- } else {
- return null;
- }
- }
-
- public static String getDefaultCharsetName() {
- return
ObjectHelper.getSystemProperty(Exchange.DEFAULT_CHARSET_PROPERTY, "UTF-8");
+ return IOHelper.getCharsetName(exchange);
}
-
+
/**
* Encoding-aware file reader.
*/
@@ -429,33 +410,18 @@ public final class IOConverter {
}
}
-
+
/**
* This method will take off the quotes and double quotes of the charset
*/
+ @Deprecated
public static String normalizeCharset(String charset) {
- if (charset != null) {
- String answer = charset.trim();
- if (answer.startsWith("'") || answer.startsWith("\"")) {
- answer = answer.substring(1);
- }
- if (answer.endsWith("'") || answer.endsWith("\"")) {
- answer = answer.substring(0, answer.length() - 1);
- }
- return answer.trim();
- } else {
- return null;
- }
+ return IOHelper.normalizeCharset(charset);
}
+ @Deprecated
public static void validateCharset(String charset) throws
UnsupportedCharsetException {
- if (charset != null) {
- if (Charset.isSupported(charset)) {
- Charset.forName(charset);
- return;
- }
- }
- throw new UnsupportedCharsetException(charset);
+ IOHelper.validateCharset(charset);
}
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java
Fri Sep 16 07:54:53 2011
@@ -33,7 +33,7 @@ import javax.xml.transform.Source;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
-import org.apache.camel.converter.IOConverter;
+import org.apache.camel.util.IOHelper;
/**
* A converter of StAX objects
@@ -47,7 +47,7 @@ public class StaxConverter {
@Converter
public XMLEventWriter createXMLEventWriter(OutputStream out, Exchange
exchange) throws XMLStreamException {
- return getOutputFactory().createXMLEventWriter(out,
IOConverter.getCharsetName(exchange));
+ return getOutputFactory().createXMLEventWriter(out,
IOHelper.getCharsetName(exchange));
}
@Converter
@@ -62,7 +62,7 @@ public class StaxConverter {
@Converter
public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream,
Exchange exchange) throws XMLStreamException {
- return getOutputFactory().createXMLStreamWriter(outputStream,
IOConverter.getCharsetName(exchange));
+ return getOutputFactory().createXMLStreamWriter(outputStream,
IOHelper.getCharsetName(exchange));
}
@Converter
@@ -85,7 +85,7 @@ public class StaxConverter {
@Converter
public XMLStreamReader createXMLStreamReader(InputStream in, Exchange
exchange) throws XMLStreamException {
- return getInputFactory().createXMLStreamReader(in,
IOConverter.getCharsetName(exchange));
+ return getInputFactory().createXMLStreamReader(in,
IOHelper.getCharsetName(exchange));
}
@Converter
@@ -108,7 +108,7 @@ public class StaxConverter {
@Converter
public XMLEventReader createXMLEventReader(InputStream in, Exchange
exchange) throws XMLStreamException {
- return getInputFactory().createXMLEventReader(in,
IOConverter.getCharsetName(exchange));
+ return getInputFactory().createXMLEventReader(in,
IOHelper.getCharsetName(exchange));
}
@Converter
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
Fri Sep 16 07:54:53 2011
@@ -19,7 +19,7 @@ package org.apache.camel.processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
-import org.apache.camel.converter.IOConverter;
+import org.apache.camel.util.IOHelper;
/**
* A processor which converts the payload of the input message to be of the
given type
@@ -50,7 +50,7 @@ public class ConvertBodyProcessor implem
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
if (charset != null) {
- exchange.setProperty(Exchange.CHARSET_NAME,
IOConverter.normalizeCharset(charset));
+ exchange.setProperty(Exchange.CHARSET_NAME,
IOHelper.normalizeCharset(charset));
}
// only convert if the is a body
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/IOHelper.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
Fri Sep 16 07:54:53 2011
@@ -22,7 +22,9 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -166,4 +168,60 @@ public final class IOHelper {
close(closeable, null, LOG);
}
+ public static void validateCharset(String charset) throws
UnsupportedCharsetException {
+ if (charset != null) {
+ if (Charset.isSupported(charset)) {
+ Charset.forName(charset);
+ return;
+ }
+ }
+ throw new UnsupportedCharsetException(charset);
+ }
+
+ /**
+ * This method will take off the quotes and double quotes of the charset
+ */
+ public static String normalizeCharset(String charset) {
+ if (charset != null) {
+ String answer = charset.trim();
+ if (answer.startsWith("'") || answer.startsWith("\"")) {
+ answer = answer.substring(1);
+ }
+ if (answer.endsWith("'") || answer.endsWith("\"")) {
+ answer = answer.substring(0, answer.length() - 1);
+ }
+ return answer.trim();
+ } else {
+ return null;
+ }
+ }
+
+ public static String getCharsetName(Exchange exchange) {
+ return getCharsetName(exchange, true);
+ }
+
+ /**
+ * Gets the charset name if set as property {@link Exchange#CHARSET_NAME}.
+ *
+ * @param exchange the exchange
+ * @param useDefault should we fallback and use JVM default charset if no
property existed?
+ * @return the charset, or <tt>null</tt> if no found
+ */
+ public static String getCharsetName(Exchange exchange, boolean useDefault)
{
+ if (exchange != null) {
+ String charsetName = exchange.getProperty(Exchange.CHARSET_NAME,
String.class);
+ if (charsetName != null) {
+ return IOHelper.normalizeCharset(charsetName);
+ }
+ }
+ if (useDefault) {
+ return getDefaultCharsetName();
+ } else {
+ return null;
+ }
+ }
+
+ private static String getDefaultCharsetName() {
+ return
ObjectHelper.getSystemProperty(Exchange.DEFAULT_CHARSET_PROPERTY, "UTF-8");
+ }
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java
Fri Sep 16 07:54:53 2011
@@ -192,12 +192,5 @@ public class IOConverterTest extends Con
assertNotNull(data);
assertEquals("Hello World",
context.getTypeConverter().convertTo(String.class, data));
}
-
- public void testCharsetNormalize() throws Exception {
- assertEquals("UTF-8", IOConverter.normalizeCharset("'UTF-8'"));
- assertEquals("UTF-8", IOConverter.normalizeCharset("\"UTF-8\""));
- assertEquals("UTF-8", IOConverter.normalizeCharset("\"UTF-8 \""));
- assertEquals("UTF-8", IOConverter.normalizeCharset("\' UTF-8\'"));
- }
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java?rev=1171430&r1=1171429&r2=1171430&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
Fri Sep 16 07:54:53 2011
@@ -56,4 +56,11 @@ public class IOHelperTest extends TestCa
OutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os, 256);
}
+
+ public void testCharsetNormalize() throws Exception {
+ assertEquals("UTF-8", IOHelper.normalizeCharset("'UTF-8'"));
+ assertEquals("UTF-8", IOHelper.normalizeCharset("\"UTF-8\""));
+ assertEquals("UTF-8", IOHelper.normalizeCharset("\"UTF-8 \""));
+ assertEquals("UTF-8", IOHelper.normalizeCharset("\' UTF-8\'"));
+ }
}