You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by dk...@apache.org on 2019/10/31 14:48:41 UTC

[ws-axiom] 04/25: Use the datatypes library to process boolean attributes in SOAP headers.

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch datatypes
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git

commit b6b20c19f058c449595c53d28e13c0d615c766c2
Author: Andreas Veithen <ve...@apache.org>
AuthorDate: Tue Nov 17 19:45:12 2015 +0000

    Use the datatypes library to process boolean attributes in SOAP headers.
---
 .../impl/common/AxiomSOAPHeaderBlockSupport.aj     | 21 ++++----
 .../axiom/soap/impl/intf/SOAP11BooleanType.java    | 46 ++++++++++++++++
 .../apache/axiom/soap/impl/intf/SOAPHelper.java    | 50 +++++------------
 .../apache/axiom/datatype/xsd/XSBooleanType.java   | 25 +++++++++
 .../axiom/datatype/xsd/XSBooleanTypeImpl.java      | 62 ++++++++++++++++++++++
 5 files changed, 157 insertions(+), 47 deletions(-)

diff --git a/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/common/AxiomSOAPHeaderBlockSupport.aj b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/common/AxiomSOAPHeaderBlockSupport.aj
index bd16428..16db7fb 100644
--- a/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/common/AxiomSOAPHeaderBlockSupport.aj
+++ b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/common/AxiomSOAPHeaderBlockSupport.aj
@@ -18,6 +18,8 @@
  */
 package org.apache.axiom.soap.impl.common;
 
+import java.text.ParseException;
+
 import javax.xml.namespace.QName;
 
 import org.apache.axiom.core.ClonePolicy;
@@ -64,10 +66,9 @@ public aspect AxiomSOAPHeaderBlockSupport {
     private boolean AxiomSOAPHeaderBlock.getBooleanAttributeValue(String key, QName qname) {
         String literal = getAttributeValue(key, qname);
         if (literal != null) {
-            Boolean value = getSOAPHelper().parseBoolean(literal);
-            if (value != null) {
-                return value.booleanValue();
-            } else {
+            try {
+                return getSOAPHelper().getBooleanType().parse(literal);
+            } catch (ParseException ex) {
                 throw new SOAPProcessingException(
                         "Invalid value for attribute " + qname.getLocalPart() + " in header block " + getQName());
             }
@@ -82,17 +83,17 @@ public aspect AxiomSOAPHeaderBlockSupport {
     
     public final void AxiomSOAPHeaderBlock.setMustUnderstand(String mustUnderstand) throws SOAPProcessingException {
         SOAPHelper helper = getSOAPHelper();
-        Boolean value = helper.parseBoolean(mustUnderstand);
-        if (value != null) {
-            _setAttributeValue(helper.getMustUnderstandAttributeQName(), mustUnderstand);
-        } else {
+        try {
+            helper.getBooleanType().parse(mustUnderstand);
+        } catch (ParseException ex) {
             throw new SOAPProcessingException("Invalid value for mustUnderstand attribute");
         }
+        _setAttributeValue(helper.getMustUnderstandAttributeQName(), mustUnderstand);
     }
 
     public final void AxiomSOAPHeaderBlock.setMustUnderstand(boolean mustUnderstand) {
         SOAPHelper helper = getSOAPHelper();
-        _setAttributeValue(helper.getMustUnderstandAttributeQName(), helper.formatBoolean(mustUnderstand));
+        _setAttributeValue(helper.getMustUnderstandAttributeQName(), helper.getBooleanType().format(mustUnderstand));
     }
 
     public final String AxiomSOAPHeaderBlock.getRole() {
@@ -119,7 +120,7 @@ public aspect AxiomSOAPHeaderBlockSupport {
         if (attributeQName == null) {
             throw new UnsupportedOperationException("Not supported for " + helper.getSpecName());
         } else {
-            _setAttributeValue(attributeQName, helper.formatBoolean(relay));
+            _setAttributeValue(attributeQName, helper.getBooleanType().format(relay));
         }
     }
 
diff --git a/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAP11BooleanType.java b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAP11BooleanType.java
new file mode 100644
index 0000000..c2b113f
--- /dev/null
+++ b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAP11BooleanType.java
@@ -0,0 +1,46 @@
+/*
+ * 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.axiom.soap.impl.intf;
+
+import java.text.ParseException;
+
+import org.apache.axiom.datatype.AbstractInvariantType;
+import org.apache.axiom.datatype.TypeHelper;
+
+final class SOAP11BooleanType extends AbstractInvariantType<Boolean> {
+    static final SOAP11BooleanType INSTANCE = new SOAP11BooleanType();
+    
+    private SOAP11BooleanType() {}
+    
+    public Boolean parse(String literal) throws ParseException {
+        int start = TypeHelper.getStartIndex(literal);
+        int end = TypeHelper.getEndIndex(literal);
+        if (end-start == 1) {
+            switch (literal.charAt(start)) {
+                case '0': return Boolean.FALSE;
+                case '1': return Boolean.TRUE;
+            }
+        }
+        throw new ParseException("Unexpected boolean literal", start);
+    }
+
+    public String format(Boolean value) {
+        return value ? "1" : "0";
+    }
+}
diff --git a/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAPHelper.java b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAPHelper.java
index 90597b8..ebf9855 100644
--- a/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAPHelper.java
+++ b/aspects/om-aspects/src/main/java/org/apache/axiom/soap/impl/intf/SOAPHelper.java
@@ -20,6 +20,8 @@ package org.apache.axiom.soap.impl.intf;
 
 import javax.xml.namespace.QName;
 
+import org.apache.axiom.datatype.InvariantType;
+import org.apache.axiom.datatype.xsd.XSBooleanType;
 import org.apache.axiom.om.OMMetaFactory;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
@@ -47,27 +49,12 @@ public abstract class SOAPHelper {
             AxiomSOAP11FaultReason.class,
             AxiomSOAP11FaultRole.class,
             AxiomSOAP11FaultDetail.class,
-            SOAP11Constants.ATTR_ACTOR, null) {
+            SOAP11Constants.ATTR_ACTOR, null,
+            SOAP11BooleanType.INSTANCE) {
         @Override
         public SOAPFactory getSOAPFactory(OMMetaFactory metaFactory) {
             return metaFactory.getSOAP11Factory();
         }
-
-        @Override
-        public Boolean parseBoolean(String literal) {
-            if (literal.equals("1")) {
-                return Boolean.TRUE;
-            } else if (literal.equals("0")) {
-                return Boolean.FALSE;
-            } else {
-                return null;
-            }
-        }
-
-        @Override
-        public String formatBoolean(boolean value) {
-            return value ? "1" : "0";
-        }
     };
     
     public static final SOAPHelper SOAP12 = new SOAPHelper(SOAP12Version.getSingleton(), "SOAP 1.2",
@@ -80,27 +67,12 @@ public abstract class SOAPHelper {
             AxiomSOAP12FaultReason.class,
             AxiomSOAP12FaultRole.class,
             AxiomSOAP12FaultDetail.class,
-            SOAP12Constants.SOAP_ROLE, SOAP12Constants.SOAP_RELAY) {
+            SOAP12Constants.SOAP_ROLE, SOAP12Constants.SOAP_RELAY,
+            XSBooleanType.INSTANCE) {
         @Override
         public SOAPFactory getSOAPFactory(OMMetaFactory metaFactory) {
             return metaFactory.getSOAP12Factory();
         }
-
-        @Override
-        public Boolean parseBoolean(String literal) {
-            if (literal.equals("true") || literal.equals("1")) {
-                return Boolean.TRUE;
-            } else if (literal.equals("false") || literal.equals("0")) {
-                return Boolean.FALSE;
-            } else {
-                return null;
-            }
-        }
-
-        @Override
-        public String formatBoolean(boolean value) {
-            return String.valueOf(value);
-        }
     };
     
     private final SOAPVersion version;
@@ -121,6 +93,7 @@ public abstract class SOAPHelper {
     private final QName mustUnderstandAttributeQName;
     private final QName roleAttributeQName;
     private final QName relayAttributeQName;
+    private final InvariantType<Boolean> booleanType;
     
     private SOAPHelper(SOAPVersion version, String specName,
             Class<? extends AxiomSOAPEnvelope> envelopeClass,
@@ -132,7 +105,8 @@ public abstract class SOAPHelper {
             Class<? extends AxiomSOAPFaultReason> faultReasonClass,
             Class<? extends AxiomSOAPFaultRole> faultRoleClass,
             Class<? extends AxiomSOAPFaultDetail> faultDetailClass,
-            String roleAttributeLocalName, String relayAttributeLocalName) {
+            String roleAttributeLocalName, String relayAttributeLocalName,
+            InvariantType<Boolean> booleanType) {
         this.version = version;
         namespace = new OMNamespaceImpl(version.getEnvelopeURI(),
                 SOAPConstants.SOAP_DEFAULT_NAMESPACE_PREFIX);
@@ -158,6 +132,7 @@ public abstract class SOAPHelper {
                 version.getEnvelopeURI(), roleAttributeLocalName, SOAPConstants.SOAP_DEFAULT_NAMESPACE_PREFIX);
         relayAttributeQName = relayAttributeLocalName == null ? null :
             new QName(version.getEnvelopeURI(), relayAttributeLocalName, SOAPConstants.SOAP_DEFAULT_NAMESPACE_PREFIX);
+        this.booleanType = booleanType;
     }
     
     public final SOAPVersion getVersion() {
@@ -254,6 +229,7 @@ public abstract class SOAPHelper {
         return relayAttributeQName;
     }
 
-    public abstract Boolean parseBoolean(String literal);
-    public abstract String formatBoolean(boolean value);
+    public InvariantType<Boolean> getBooleanType() {
+        return booleanType;
+    }
 }
diff --git a/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanType.java b/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanType.java
new file mode 100644
index 0000000..df66daf
--- /dev/null
+++ b/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanType.java
@@ -0,0 +1,25 @@
+/*
+ * 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.axiom.datatype.xsd;
+
+import org.apache.axiom.datatype.InvariantType;
+
+public interface XSBooleanType extends InvariantType<Boolean> {
+    XSBooleanType INSTANCE = new XSBooleanTypeImpl();
+}
diff --git a/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanTypeImpl.java b/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanTypeImpl.java
new file mode 100644
index 0000000..8912274
--- /dev/null
+++ b/datatypes/src/main/java/org/apache/axiom/datatype/xsd/XSBooleanTypeImpl.java
@@ -0,0 +1,62 @@
+/*
+ * 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.axiom.datatype.xsd;
+
+import java.text.ParseException;
+
+import org.apache.axiom.datatype.AbstractInvariantType;
+import org.apache.axiom.datatype.TypeHelper;
+
+final class XSBooleanTypeImpl extends AbstractInvariantType<Boolean> implements XSBooleanType {
+    private static boolean equals(String s1, int start, String s2) {
+        for (int i=0, len=s2.length(); i<len; i++) {
+            if (s1.charAt(start+i) != s2.charAt(i)) {
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    public Boolean parse(String literal) throws ParseException {
+        int start = TypeHelper.getStartIndex(literal);
+        int end = TypeHelper.getEndIndex(literal);
+        switch (end-start) {
+            case 1:
+                switch (literal.charAt(start)) {
+                    case '0': return Boolean.FALSE;
+                    case '1': return Boolean.TRUE;
+                }
+                break;
+            case 4:
+                if (equals(literal, start, "true")) {
+                    return Boolean.TRUE;
+                }
+                break;
+            case 5:
+                if (equals(literal, start, "false")) {
+                    return Boolean.FALSE;
+                }
+        }
+        throw new ParseException("Unexpected boolean literal", start);
+    }
+
+    public String format(Boolean value) {
+        return value.toString();
+    }
+}