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.apache.ws.commons.schema.constants.Constants;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Attr;
27  
28  import java.util.Iterator;
29  import java.util.Map;
30  
31  /**
32  
33   */
34  public class DefaultExtensionSerializer implements ExtensionSerializer{
35  
36      /**
37       * serialize the given element
38       *
39       * @param schemaObject - Parent schema element
40       * @param classOfType  - the class of the object to be serialized
41       * @param node - The DOM Node that is the parent of the serialzation
42       */
43      public void serialize(XmlSchemaObject schemaObject, Class classOfType, Node node) {
44          // serialization is somewhat tricky in most cases hence this default serializer will
45          // do the exact reverse of the deserializer - look for any plain 'as is' items
46          // and attach them to the parent node.
47          // we just attach the raw node either to the meta map of
48          // elements or the attributes
49          Map metaInfoMap = schemaObject.getMetaInfoMap();
50          Document parentDoc = node.getOwnerDocument();
51          if (metaInfoMap.containsKey(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES)){
52              Map attribMap  = (Map)metaInfoMap.get(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES);
53             for(Iterator it = attribMap.values().iterator();it.hasNext();){
54                  if (node.getNodeType()==Node.ELEMENT_NODE){
55                      ((Element)node).setAttributeNodeNS((Attr)parentDoc.importNode((Node)it.next(),true));
56                  }
57  
58              }
59          }
60  
61          if (metaInfoMap.containsKey(Constants.MetaDataConstants.EXTERNAL_ELEMENTS)){
62              Map elementMap  = (Map)metaInfoMap.get(Constants.MetaDataConstants.EXTERNAL_ELEMENTS);
63              for(Iterator it = elementMap.values().iterator();it.hasNext();){
64                  node.appendChild(
65                          parentDoc.importNode((Node)it.next(),true));
66              }
67          }
68  
69      }
70  }