OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3498 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 30 Jul 2009 03:08:43 +0000 (03:08 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 30 Jul 2009 03:08:43 +0000 (03:08 +0000)
13 files changed:
src/main/java/org/xerial/core/XerialErrorCode.java
src/main/java/org/xerial/core/XerialException.java
src/main/java/org/xerial/json/JSONErrorCode.java
src/main/java/org/xerial/json/JSONException.java
src/main/java/org/xerial/util/bean/BeanErrorCode.java
src/main/java/org/xerial/util/opt/ArgumentItem.java
src/main/java/org/xerial/util/opt/OptionItem.java
src/main/java/org/xerial/util/opt/OptionParser.java
src/main/java/org/xerial/util/opt/OptionParserException.java
src/main/java/org/xerial/util/opt/OptionSetter.java
src/main/java/org/xerial/util/opt/OptionSetterViaField.java
src/main/java/org/xerial/util/opt/OptionSetterViaMethod.java
src/main/java/org/xerial/util/xml/XMLErrorCode.java

index c2f8d20..e8272bf 100644 (file)
@@ -53,7 +53,7 @@ public enum XerialErrorCode implements ErrorCode {
     INVALID_TOKEN, PARSE_ERROR,
 
     // general exception
-    INHERITED, ;
+    INHERITED, HAS_AN_EXTENDED_ERROR_CODE;
 
     private final String description;
 
@@ -73,8 +73,4 @@ public enum XerialErrorCode implements ErrorCode {
         return null;
     }
 
-    public Enum< ? > getCode() {
-        return this;
-    }
-
 }
index 1f45ba0..1d32a1d 100644 (file)
@@ -39,6 +39,7 @@ import org.xerial.json.JSONErrorCode;
  * 
  */
 public class XerialException extends Exception {
+
     private static final long serialVersionUID = 1L;
 
     private final ErrorCode errorCode;
@@ -46,7 +47,6 @@ public class XerialException extends Exception {
     public XerialException(XerialException e) {
         super(e.getErrorMessage());
         this.errorCode = e.errorCode;
-
     }
 
     public XerialException(ErrorCode errorCode) {
@@ -69,15 +69,25 @@ public class XerialException extends Exception {
         this.errorCode = errorCode;
     }
 
-    /**
-     * Get the error code of this exception
-     * 
-     * @return
-     */
     public ErrorCode getErrorCode() {
         return errorCode;
     }
 
+    private boolean hasXerialErrorCode() {
+        return XerialErrorCode.class.isInstance(errorCode);
+    }
+
+    public XerialErrorCode getXerialErrorCode() {
+        if (hasXerialErrorCode())
+            return XerialErrorCode.class.cast(errorCode);
+        else
+            return XerialErrorCode.HAS_AN_EXTENDED_ERROR_CODE;
+    }
+
+    public <T extends Enum<T>> T getErrorCodeOfTheType(Class<T> errorCodeType) {
+        return errorCodeType.cast(errorCode);
+    }
+
     @Override
     public String getMessage() {
         return ExceptionHelper.getMessage(errorCode, super.getMessage());
index bef13ca..571a331 100644 (file)
@@ -61,8 +61,4 @@ public enum JSONErrorCode implements ErrorCode {
         return description;
     }
 
-    public Enum< ? > getCode() {
-        return this;
-    }
-
 }
index 06ca89d..d821f19 100644 (file)
@@ -32,20 +32,17 @@ import org.xerial.core.XerialException;
  * @author leo\r
  * \r
  */\r
-public class JSONException extends XerialException\r
-{\r
+public class JSONException extends XerialException {\r
     private static final long serialVersionUID = 1L;\r
 \r
     /**\r
      * @param errorCode\r
      */\r
-    public JSONException(JSONErrorCode errorCode)\r
-    {\r
+    public JSONException(JSONErrorCode errorCode) {\r
         super(errorCode);\r
     }\r
 \r
-    public JSONException(JSONErrorCode errorCode, String message)\r
-    {\r
+    public JSONException(JSONErrorCode errorCode, String message) {\r
         super(errorCode, message);\r
     }\r
 \r
@@ -54,8 +51,7 @@ public class JSONException extends XerialException
      * @param message\r
      * @param e\r
      */\r
-    public JSONException(JSONErrorCode errorCode, String message, Throwable e)\r
-    {\r
+    public JSONException(JSONErrorCode errorCode, String message, Throwable e) {\r
         super(errorCode, message, e);\r
     }\r
 \r
@@ -63,8 +59,7 @@ public class JSONException extends XerialException
      * @param errorCode\r
      * @param e\r
      */\r
-    public JSONException(JSONErrorCode errorCode, Throwable e)\r
-    {\r
+    public JSONException(JSONErrorCode errorCode, Throwable e) {\r
         super(errorCode);\r
     }\r
 \r
@@ -73,8 +68,7 @@ public class JSONException extends XerialException
      * \r
      * @return the error code\r
      */\r
-    public JSONErrorCode getErrorCode()\r
-    {\r
+    public JSONErrorCode getErrorCode() {\r
         return (JSONErrorCode) super.getErrorCode();\r
     }\r
 }\r
index d7dacc8..f6e9ac3 100644 (file)
@@ -72,8 +72,4 @@ public enum BeanErrorCode implements ErrorCode {
         return description;
     }
 
-    public Enum< ? > getCode() {
-        return this;
-    }
-
 }
index 6a62a14..1655dbb 100644 (file)
@@ -27,6 +27,7 @@ package org.xerial.util.opt;
 import java.lang.reflect.Field;\r
 import java.lang.reflect.Method;\r
 \r
+import org.xerial.core.XerialException;\r
 import org.xerial.util.Range;\r
 import org.xerial.util.bean.TypeInfo;\r
 \r
@@ -36,13 +37,11 @@ import org.xerial.util.bean.TypeInfo;
  * @author leo\r
  * \r
  */\r
-public class ArgumentItem\r
-{\r
-    private final Argument     argumentDescriptor;\r
+public class ArgumentItem {\r
+    private final Argument argumentDescriptor;\r
     private final OptionSetter argumentSetter;\r
 \r
-    public ArgumentItem(Method setter)\r
-    {\r
+    public ArgumentItem(Method setter) {\r
         argumentDescriptor = setter.getAnnotation(Argument.class);\r
         if (argumentDescriptor == null)\r
             throw new IllegalArgumentException(setter + " is not for an option argument");\r
@@ -50,8 +49,7 @@ public class ArgumentItem
         argumentSetter = new OptionSetterViaMethod(setter);\r
     }\r
 \r
-    public ArgumentItem(Field field)\r
-    {\r
+    public ArgumentItem(Field field) {\r
         argumentDescriptor = field.getAnnotation(Argument.class);\r
         if (argumentDescriptor == null)\r
             throw new IllegalArgumentException(field + " is not for an option argument");\r
@@ -60,10 +58,8 @@ public class ArgumentItem
     }\r
 \r
     @Override\r
-    public boolean equals(Object obj)\r
-    {\r
-        if (obj instanceof ArgumentItem)\r
-        {\r
+    public boolean equals(Object obj) {\r
+        if (obj instanceof ArgumentItem) {\r
             ArgumentItem other = (ArgumentItem) obj;\r
             return argumentDescriptor.equals(other.argumentDescriptor);\r
         }\r
@@ -72,52 +68,43 @@ public class ArgumentItem
     }\r
 \r
     @Override\r
-    public int hashCode()\r
-    {\r
+    public int hashCode() {\r
         return argumentDescriptor.hashCode();\r
     }\r
 \r
-    public boolean takesMultipleArguments()\r
-    {\r
+    public boolean takesMultipleArguments() {\r
         return TypeInfo.isCollection(argumentSetter.getOptionDataType());\r
     }\r
 \r
-    public static Range getRangeOf(ArgumentItem arg)\r
-    {\r
+    public static Range getRangeOf(ArgumentItem arg) {\r
         int start = arg.getArgumentDescriptor().index();\r
         int end = arg.takesMultipleArguments() ? Integer.MAX_VALUE : start;\r
         return new Range(start, end);\r
     }\r
 \r
-    public Range getRange()\r
-    {\r
+    public Range getRange() {\r
         return getRangeOf(this);\r
     }\r
 \r
-    public void set(Object bean, Object value) throws OptionParserException\r
-    {\r
+    public void set(Object bean, Object value) throws XerialException {\r
         argumentSetter.setOption(bean, value);\r
     }\r
 \r
-    public Argument getArgumentDescriptor()\r
-    {\r
+    public Argument getArgumentDescriptor() {\r
         return argumentDescriptor;\r
     }\r
 \r
-    public void initialize(Object optionHolder) throws OptionParserException\r
-    {\r
+    public void initialize(Object optionHolder) throws OptionParserException {\r
         argumentSetter.initialize(optionHolder);\r
     }\r
 \r
     @Override\r
-    public String toString()\r
-    {\r
-        return String.format("name=%s, index=%s, required=%s", argumentDescriptor.name(), getRange().toString(),\r
-                argumentDescriptor.required());\r
+    public String toString() {\r
+        return String.format("name=%s, index=%s, required=%s", argumentDescriptor.name(),\r
+                getRange().toString(), argumentDescriptor.required());\r
     }\r
 \r
-    public String getArgumentName()\r
-    {\r
+    public String getArgumentName() {\r
         String name = argumentDescriptor.name();\r
         if (name == null || name.length() <= 0)\r
             name = argumentSetter.getParameterName();\r
index 45b6d33..144b94a 100644 (file)
@@ -27,6 +27,7 @@ package org.xerial.util.opt;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
+import org.xerial.core.XerialException;
 import org.xerial.util.bean.TypeInfo;
 
 /**
@@ -92,20 +93,9 @@ public class OptionItem {
         return TypeInfo.isCollection(optionSetter.getOptionDataType());
     }
 
-    public void setOption(Object bean, String value) throws OptionParserException {
+    public void setOption(Object bean, String value) throws XerialException {
         optionSetter.setOption(bean, value);
 
-        //        Class< ? > optionType = optionSetter.getOptionDataType();
-        //        try
-        //        {
-        //            Object convertedValue = TypeConverter.convertType(optionType, value);
-        //            optionSetter.setOption(bean, convertedValue);
-        //        }
-        //        catch (BeanException e)
-        //        {
-        //            throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e);
-        //        }
-
     }
 
     public void initialize(Object optionHolder) throws OptionParserException {
index be7be3a..5782b2b 100644 (file)
@@ -33,6 +33,7 @@ import java.util.List;
 
 import org.xerial.core.XerialError;
 import org.xerial.core.XerialErrorCode;
+import org.xerial.core.XerialException;
 import org.xerial.util.bean.BeanErrorCode;
 import org.xerial.util.bean.BeanException;
 import org.xerial.util.bean.TypeInfo;
@@ -175,7 +176,7 @@ public class OptionParser {
                         throw new OptionParserException(XerialErrorCode.SYNTAX_ERROR,
                                 "parameter value is required for --" + longOptionName);
 
-                    optionItem.setOption(optionHolder, "true");
+                    setOption(optionItem, "true");
 
                     if (!optionItem.takesMultipleArguments()
                             && activatedOption.contains(optionItem.getOption()))
@@ -199,7 +200,7 @@ public class OptionParser {
                                 "syntax error --" + longOptionName);
                     }
 
-                    optionItem.setOption(optionHolder, value);
+                    setOption(optionItem, value);
                     if (!optionItem.takesMultipleArguments()
                             && activatedOption.contains(optionItem.getOption()))
                         throw new OptionParserException(XerialErrorCode.DUPLICATE_OPTION,
@@ -255,7 +256,14 @@ public class OptionParser {
                                 "unused argument: " + currentArg);
                 }
 
-                argItem.set(optionHolder, currentArg);
+                try {
+                    argItem.set(optionHolder, currentArg);
+                }
+                catch (XerialException e) {
+                    throw new OptionParserException(XerialErrorCode.INVALID_ARGUMENT, e
+                            .getMessage());
+                }
+
                 if (!argItem.takesMultipleArguments()
                         && activatedArgument.contains(argItem.getArgumentDescriptor()))
                     throw new OptionParserException(XerialErrorCode.DUPLICATE_OPTION, argItem
@@ -280,18 +288,19 @@ public class OptionParser {
         try {
             item.setOption(optionHolder, value);
         }
-        catch (OptionParserException e) {
+        catch (XerialException e) {
             if (BeanErrorCode.class.isInstance(e.getErrorCode())) {
                 BeanErrorCode be = BeanErrorCode.class.cast(e.getErrorCode());
                 switch (be) {
                 case InvalidFormat:
                     throw new OptionParserException(XerialErrorCode.INVALID_ARGUMENT, String
                             .format("cannot set %s to %s", value, item.toString()));
+                default:
+                    throw new OptionParserException(e.getErrorCode(), e.getMessage());
                 }
-
             }
-
-            throw e;
+            else
+                throw new OptionParserException(e.getErrorCode(), e.getMessage());
         }
     }
 
index 0a57045..2c41fac 100644 (file)
@@ -40,6 +40,10 @@ public class OptionParserException extends XerialException {
      */
     private static final long serialVersionUID = 1L;
 
+    public OptionParserException(XerialException e) {
+        super(e);
+    }
+
     public OptionParserException(ErrorCode errorCode, String message, Throwable cause) {
         super(errorCode, message, cause);
     }
index 13b739e..69fe9cf 100644 (file)
 //--------------------------------------
 package org.xerial.util.opt;
 
+import org.xerial.core.XerialException;
+
 /**
  * @author leo
  * 
  */
-public interface OptionSetter
-{
-    void setOption(Object bean, Object convertedValue) throws OptionParserException;
+public interface OptionSetter {
+    void setOption(Object bean, Object convertedValue) throws XerialException;
 
     Class< ? > getOptionDataType();
 
index 86c627d..e065760 100644 (file)
@@ -36,53 +36,36 @@ import org.xerial.util.reflect.ReflectionUtil;
  * @author leo
  * 
  */
-public class OptionSetterViaField implements OptionSetter
-{
+public class OptionSetterViaField implements OptionSetter {
     private final Field field;
 
-    public OptionSetterViaField(Field field)
-    {
+    public OptionSetterViaField(Field field) {
         this.field = field;
     }
 
-    public Class< ? > getOptionDataType()
-    {
+    public Class< ? > getOptionDataType() {
         return field.getType();
     }
 
-    public void setOption(Object bean, Object value) throws OptionParserException
-    {
-        try
-        {
-            ReflectionUtil.setFieldValue(bean, field, value);
-        }
-        catch (XerialException e)
-        {
-            throw new OptionParserException(e.getErrorCode(), e.getMessage());
-        }
-
+    public void setOption(Object bean, Object value) throws XerialException {
+        ReflectionUtil.setFieldValue(bean, field, value);
     }
 
-    public boolean takesArgument()
-    {
+    public boolean takesArgument() {
         Class< ? > type = getOptionDataType();
         return !TypeInfo.isBoolean(type);
     }
 
-    public void initialize(Object bean) throws OptionParserException
-    {
-        try
-        {
+    public void initialize(Object bean) throws OptionParserException {
+        try {
             ReflectionUtil.initializeCollectionField(bean, field);
         }
-        catch (XerialException e)
-        {
-            throw new OptionParserException(e.getErrorCode(), e.getMessage());
+        catch (XerialException e) {
+            throw new OptionParserException(e);
         }
     }
 
-    public String getParameterName()
-    {
+    public String getParameterName() {
         return field.getName();
     }
 
index dc7046d..6d51255 100644 (file)
@@ -28,6 +28,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;\r
 import java.lang.reflect.Type;\r
 \r
+import org.xerial.core.XerialException;\r
 import org.xerial.util.bean.TypeInfo;\r
 import org.xerial.util.reflect.ReflectionUtil;\r
 \r
@@ -37,43 +38,35 @@ import org.xerial.util.reflect.ReflectionUtil;
  * @author leo\r
  * \r
  */\r
-class OptionSetterViaMethod implements OptionSetter\r
-{\r
+class OptionSetterViaMethod implements OptionSetter {\r
     private Method m;\r
 \r
-    public OptionSetterViaMethod(Method m)\r
-    {\r
+    public OptionSetterViaMethod(Method m) {\r
         this.m = m;\r
     }\r
 \r
-    public void setOption(Object bean, Object value) throws OptionParserException\r
-    {\r
+    public void setOption(Object bean, Object value) throws XerialException {\r
         ReflectionUtil.setValue(bean, m, value);\r
     }\r
 \r
-    public Class< ? > getOptionDataType()\r
-    {\r
+    public Class< ? > getOptionDataType() {\r
         Type t = m.getParameterTypes()[0];\r
-        if (ParameterizedType.class.isInstance(t))\r
-        {\r
+        if (ParameterizedType.class.isInstance(t)) {\r
             return (Class< ? >) ((ParameterizedType) t).getRawType();\r
         }\r
         else\r
             return (Class< ? >) t;\r
     }\r
 \r
-    public boolean takesArgument()\r
-    {\r
+    public boolean takesArgument() {\r
         return !TypeInfo.isBoolean(getOptionDataType());\r
     }\r
 \r
-    public void initialize(Object bean) throws OptionParserException\r
-    {\r
+    public void initialize(Object bean) throws OptionParserException {\r
     // do nothing\r
     }\r
 \r
-    public String getParameterName()\r
-    {\r
+    public String getParameterName() {\r
         return m.getName();\r
     }\r
 \r
index 778de36..b7f1401 100644 (file)
@@ -54,8 +54,4 @@ public enum XMLErrorCode implements ErrorCode {
         return description;
     }
 
-    public Enum< ? > getCode() {
-        return this;
-    }
-
 }