You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/04/07 19:24:44 UTC
svn commit: r645622 [2/2] - in
/commons/proper/configuration/branches/configuration2_experimental/src:
main/java/org/apache/commons/configuration2/
main/java/org/apache/commons/configuration2/beanutils/
main/java/org/apache/commons/configuration2/conve...
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Java Net IP address converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class InetAddressConverter implements TypeConverter<InetAddress>
+{
+ private static final TypeConverter instance = new InetAddressConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public InetAddress convert(Object value, Object... params)
+ {
+ if (value instanceof String)
+ {
+ try
+ {
+ return InetAddress.getByName((String) value);
+ }
+ catch (UnknownHostException e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a InetAddress", e);
+ }
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a InetAddress");
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InetAddressConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Integer converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class IntegerConverter extends NumberConverter<Integer>
+{
+ private static final TypeConverter instance = new IntegerConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public Integer convert(Object value, Object... params) throws ConversionException
+ {
+ Number n = toNumber(value, Integer.class);
+ if (n instanceof Integer)
+ {
+ return (Integer) n;
+ }
+ else
+ {
+ return new Integer(n.intValue());
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/IntegerConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Email address converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class InternetAddressConverter implements TypeConverter<InternetAddress>
+{
+ private static final TypeConverter instance = new InternetAddressConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public InternetAddress convert(Object value, Object... params)
+ {
+ if (value instanceof String)
+ {
+ try
+ {
+ return new InternetAddress((String) value);
+ }
+ catch (Exception e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a InternetAddress", e);
+ }
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a InternetAddress");
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/InternetAddressConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import java.util.Locale;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Locale converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class LocaleConverter implements TypeConverter<Locale>
+{
+ private static final TypeConverter instance = new LocaleConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public Locale convert(Object value, Object... params)
+ {
+ if (value instanceof String)
+ {
+ String[] elements = ((String) value).split("_");
+ int size = elements.length;
+
+ if (size >= 1 && ((elements[0]).length() == 2 || (elements[0]).length() == 0))
+ {
+ String language = elements[0];
+ String country = (size >= 2) ? elements[1] : "";
+ String variant = (size >= 3) ? elements[2] : "";
+
+ return new Locale(language, country, variant);
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a Locale");
+ }
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a Locale");
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LocaleConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Long converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class LongConverter extends NumberConverter<Long>
+{
+ private static final TypeConverter instance = new LongConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public Long convert(Object value, Object... params) throws ConversionException
+ {
+ Number n = toNumber(value, Long.class);
+ if (n instanceof Long)
+ {
+ return (Long) n;
+ }
+ else
+ {
+ return new Long(n.longValue());
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/LongConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import java.math.BigInteger;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Base converter for numbers.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+abstract class NumberConverter<T extends Number> implements TypeConverter<T>
+{
+ /** Constant for the prefix of hex numbers.*/
+ private static final String HEX_PREFIX = "0x";
+
+ /** Constant for the radix of hex numbers.*/
+ private static final int HEX_RADIX = 16;
+
+ /**
+ * Tries to convert the specified object into a number object. This method
+ * is used by the conversion methods for number types. Note that the return
+ * value is not in always of the specified target class, but only if a new
+ * object has to be created.
+ *
+ * @param value the value to be converted (must not be <b>null</b>)
+ * @param targetClass the target class of the conversion (must be derived
+ * from <code>java.lang.Number</code>)
+ * @return the converted number
+ * @throws org.apache.commons.configuration2.ConversionException if the object cannot be converted
+ */
+ protected Number toNumber(Object value, Class<? extends Number> targetClass) throws ConversionException
+ {
+ if (value instanceof Number)
+ {
+ return (Number) value;
+ }
+ else
+ {
+ String str = value.toString();
+ if (str.startsWith(HEX_PREFIX))
+ {
+ try
+ {
+ return new BigInteger(str.substring(HEX_PREFIX.length()), HEX_RADIX);
+ }
+ catch (NumberFormatException nex)
+ {
+ throw new ConversionException("Could not convert " + str
+ + " to " + targetClass.getName()
+ + "! Invalid hex number.", nex);
+ }
+ }
+
+ try
+ {
+ Constructor<? extends Number> constr = targetClass.getConstructor(String.class);
+ return (Number) constr.newInstance(str);
+ }
+ catch (InvocationTargetException itex)
+ {
+ throw new ConversionException("Could not convert " + str
+ + " to " + targetClass.getName(), itex.getTargetException());
+ }
+ catch (Exception ex)
+ {
+ // Treat all possible exceptions the same way
+ throw new ConversionException("Conversion error when trying to convert " + str
+ + " to " + targetClass.getName(), ex);
+ }
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Short converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class ShortConverter extends NumberConverter<Short>
+{
+ private static final TypeConverter instance = new ShortConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public Short convert(Object value, Object... params) throws ConversionException
+ {
+ Number n = toNumber(value, Short.class);
+ if (n instanceof Short)
+ {
+ return (Short) n;
+ }
+ else
+ {
+ return new Short(n.shortValue());
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/ShortConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * Converts a value into a single type.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+interface TypeConverter<T>
+{
+ /**
+ * Converts the specified value to the desired type.
+ *
+ * @param value
+ * @param params
+ */
+ T convert(Object value, Object... params) throws ConversionException;
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/TypeConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java Mon Apr 7 10:24:32 2008
@@ -0,0 +1,71 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.configuration2.converter;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.commons.configuration2.ConversionException;
+
+/**
+ * URL converter.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ * @since 2.0
+ */
+class URLConverter implements TypeConverter<URL>
+{
+ private static final TypeConverter instance = new URLConverter();
+
+ public static TypeConverter getInstance()
+ {
+ return instance;
+ }
+
+ public URL convert(Object value, Object... params)
+ {
+ if (value instanceof String)
+ {
+ try
+ {
+ return new URL((String) value);
+ }
+ catch (MalformedURLException e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to an URL", e);
+ }
+ }
+ else if (value instanceof URI)
+ {
+ try
+ {
+ return ((URI) value).toURL();
+ }
+ catch (MalformedURLException e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to an URL", e);
+ }
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to an URL");
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/URLConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/package.html
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/package.html?rev=645622&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/package.html (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/package.html Mon Apr 7 10:24:32 2008
@@ -0,0 +1,29 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<!--
+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.
+-->
+<html>
+<body>
+
+<p>
+Package for classes related to the conversion of properties types.
+</p>
+<p>
+<font size="-2">$Id$</font>
+</p>
+
+</body>
+</html>
Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java?rev=645622&r1=645621&r2=645622&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java Mon Apr 7 10:24:32 2008
@@ -25,6 +25,9 @@
import java.util.Iterator;
import java.util.List;
+import org.apache.commons.configuration2.converter.Converter;
+import org.apache.commons.configuration2.converter.DefaultPropertyConverter;
+
import junit.framework.TestCase;
/**
@@ -44,6 +47,8 @@
/** An enumeration object used for testing conversions with enums.*/
private static ElementType ENUM_OBJECT = ElementType.METHOD;
+ private static Converter converter = new DefaultPropertyConverter();
+
public void testSplit()
{
String s = "abc, xyz , 123";
@@ -293,9 +298,9 @@
public void testToNumberDirect()
{
Integer i = new Integer(42);
- assertSame("Wrong integer", i, PropertyConverter.toNumber(i, Integer.class));
+ assertSame("Wrong integer", i, converter.convert(Integer.class, i));
BigDecimal d = new BigDecimal("3.1415");
- assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d, Integer.class));
+ assertSame("Wrong BigDecimal", d, converter.convert(Number.class, d));
}
/**
@@ -304,8 +309,8 @@
*/
public void testToNumberFromString()
{
- assertEquals("Incorrect Integer value", new Integer(42), PropertyConverter.toNumber("42", Integer.class));
- assertEquals("Incorrect Short value", new Short((short) 10), PropertyConverter.toNumber(new StringBuilder("10"), Short.class));
+ assertEquals("Incorrect Integer value", new Integer(42), converter.convert(Integer.class, "42"));
+ assertEquals("Incorrect Short value", new Short((short) 10), converter.convert(Short.class, new StringBuilder("10")));
}
/**
@@ -314,7 +319,7 @@
*/
public void testToNumberFromHexString()
{
- Number n = PropertyConverter.toNumber("0x10", Integer.class);
+ Number n = converter.convert(Integer.class, "0x10");
assertEquals("Incorrect Integer value", 16, n.intValue());
}
@@ -326,7 +331,7 @@
{
try
{
- PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
+ converter.convert(Integer.class, "0xNotAHexValue");
fail("Could convert invalid hex value!");
}
catch (ConversionException cex)
@@ -343,7 +348,7 @@
{
try
{
- PropertyConverter.toNumber("Not a number", Byte.class);
+ converter.convert(Byte.class, "Not a number");
fail("Could convert invalid String!");
}
catch (ConversionException cex)
@@ -354,19 +359,19 @@
public void testToEnumFromEnum()
{
- assertEquals(ENUM_OBJECT, PropertyConverter.toEnum(ENUM_OBJECT, ENUM_CLASS));
+ assertEquals(ENUM_OBJECT, converter.convert(ENUM_CLASS, ENUM_OBJECT));
}
public void testToEnumFromString()
{
- assertEquals(ENUM_OBJECT, PropertyConverter.toEnum("METHOD", ENUM_CLASS));
+ assertEquals(ENUM_OBJECT, converter.convert(ENUM_CLASS, "METHOD"));
}
public void testToEnumFromInvalidString()
{
try
{
- PropertyConverter.toEnum("FOO", ENUM_CLASS);
+ converter.convert(ENUM_CLASS, "FOO");
fail("Could convert invalid String!");
}
catch (ConversionException e)
@@ -377,14 +382,14 @@
public void testToEnumFromNumber()
{
- assertEquals(ENUM_OBJECT, PropertyConverter.toEnum(new Integer(2), ENUM_CLASS));
+ assertEquals(ENUM_OBJECT, converter.convert(ENUM_CLASS, new Integer(2)));
}
public void testToEnumFromInvalidNumber()
{
try
{
- PropertyConverter.toEnum(new Integer(-1), ENUM_CLASS);
+ converter.convert(ENUM_CLASS, new Integer(-1));
fail("Could convert invalid number!");
}
catch (ConversionException e)