View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements. See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership. The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License. You may obtain a copy of the License at
    9    *
    10   * http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied. See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.ws.commons.schema.extensions;
    20
    21  import org.apache.ws.commons.schema.XmlSchemaObject;
    22  import org.w3c.dom.Node;
    23
    24  import javax.xml.namespace.QName;
    25  import java.util.HashMap;
    26  import java.util.Map;
    27
    28  /**
    29   * The task of the extension serializer is to delegate the serilization of
    30   * the extensions. The extension serializers/deserializers are retained in seperate
    31   * maps and there are also two seperate references to default serializers and deserializers
    32   * which would jump into action when there are no specifically attached
    33   * serializers/deserializers
    34   */
    35  public class ExtensionRegistry {
    36
    37      /**
    38       * Maps for the storage of extension serializers /deserializers
    39       */
    40      private Map extensionSerializers = new HashMap();
    41      private Map extensionDeserializers = new HashMap();
    42
    43      /**
    44       * Default serializer and serializer
    45       */
    46      private ExtensionSerializer defaultExtensionSerializer  =new DefaultExtensionSerializer();
    47      private ExtensionDeserializer defaultExtensionDeserializer = new DefaultExtensionDeserializer();
    48
    49
    50      public ExtensionSerializer getDefaultExtensionSerializer() {
    51          return defaultExtensionSerializer;
    52      }
    53
    54      public void setDefaultExtensionSerializer(ExtensionSerializer defaultExtensionSerializer) {
    55          this.defaultExtensionSerializer = defaultExtensionSerializer;
    56      }
    57
    58      public ExtensionDeserializer getDefaultExtensionDeserializer() {
    59          return defaultExtensionDeserializer;
    60      }
    61
    62      public void setDefaultExtensionDeserializer(ExtensionDeserializer defaultExtensionDeserializer) {
    63          this.defaultExtensionDeserializer = defaultExtensionDeserializer;
    64      }
    65
    66      /**
    67       * Register a deserializer with a QName
    68       * @param name  - the QName of the element/attribute
    69       * @param deserializer - an instance of the deserializer
    70       */
    71      public void registerDeserializer(QName name,ExtensionDeserializer deserializer){
    72          extensionDeserializers.put(name,deserializer);
    73      }
    74      /**
    75       * Register a serializer with a Class
    76       * @param classOfType  - the class of the object that would be serialized
    77       * @param serializer - an instance of the deserializer
    78       */
    79      public void registerSerializer(Class classOfType,ExtensionSerializer serializer){
    80          extensionSerializers.put(classOfType,serializer);
    81      }
    82
    83
    84      /**
    85       * remove the registration for a serializer with a QName
    86       * @param name  - the QName of the element/attribute the
    87       * serializer is associated with
    88       */
    89      public void unregisterSerializer(QName name){
    90          extensionSerializers.remove(name);
    91      }
    92
    93      /**
    94       * remove the registration for a deserializer with a QName
    95       * @param classOfType  - the  the
    96       * deserializer is associated with
    97       */
    98      public void unregisterDeserializer(Class classOfType){
    99          extensionDeserializers.remove(classOfType);
    100     }
    101
    102
    103     /**
    104      * Serialize a given extension element
    105      * @param parentSchemaObject - the parent schema object. This is what
    106      * would contain the extension object, probably in side its meta information
    107      * map
    108      * @param classOfType - The class of type to be serialized
    109      * @param  node - the parent DOM Node that will ultimately be serialized. The XMLSchema
    110      * serialization mechanism is to create a DOM tree first and serialize it
    111      */
    112     public void serializeExtension(XmlSchemaObject parentSchemaObject,
    113                              Class classOfType,
    114                              Node node){
    115         Object serializerObject = extensionSerializers.get(classOfType);
    116         if (serializerObject!=null){
    117             //perform the serialization
    118             ExtensionSerializer ser = (ExtensionSerializer)serializerObject;
    119             ser.serialize(parentSchemaObject,classOfType,node);
    120         }else if (defaultExtensionSerializer!=null) {
    121             defaultExtensionSerializer.serialize(parentSchemaObject,classOfType,node);
    122         }
    123
    124
    125     }
    126
    127
    128     /**
    129      * Deserialize a given extension element
    130      * @param parentSchemaObject - the parent schema object. This is anticipated
    131      * to be created already and the relevant object would contain the extension
    132      * object, probably in side its meta information map
    133      * @param name - The qname of the element/attribute to be deserialized. This will be used to
    134      * search for the extension as well as by the deserializer if a single deserializer is
    135      * registered against a number of qnames
    136      * @param  rawNode  - the raw DOM Node read from the source. This will be the
    137      * extension element itself if for an element or extension attribute itself
    138      * in case of an attribute
    139      */
    140     public void deserializeExtension(XmlSchemaObject parentSchemaObject,
    141                              QName name,
    142                              Node rawNode){
    143         Object deserializerObject = extensionDeserializers.get(name);
    144         if (deserializerObject !=null){
    145             //perform the serialization
    146             ExtensionDeserializer deser = (ExtensionDeserializer)deserializerObject;
    147             deser.deserialize(parentSchemaObject,name,rawNode);
    148         } else if (defaultExtensionDeserializer!=null){
    149             defaultExtensionDeserializer.deserialize(parentSchemaObject,name,rawNode);
    150         }
    151     }
    152
    153 }