View Javadoc

1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.ws.commons.schema.extensions;
17  
18  import org.apache.ws.commons.schema.XmlSchemaObject;
19  import org.apache.ws.commons.schema.constants.Constants;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Node;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Attr;
24  
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  /**
29  
30   */
31  public class DefaultExtensionSerializer implements ExtensionSerializer{
32  
33      /**
34       * serialize the given element
35       *
36       * @param schemaObject - Parent schema element
37       * @param classOfType  - the class of the object to be serialized
38       * @param node - The DOM Node that is the parent of the serialzation
39       */
40      public void serialize(XmlSchemaObject schemaObject, Class classOfType, Node node) {
41          // serialization is somewhat tricky in most cases hence this default serializer will
42          // do the exact reverse of the deserializer - look for any plain 'as is' items
43          // and attach them to the parent node.
44          // we just attach the raw node either to the meta map of
45          // elements or the attributes
46          Map metaInfoMap = schemaObject.getMetaInfoMap();
47          Document parentDoc = node.getOwnerDocument();
48          if (metaInfoMap.containsKey(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES)){
49              Map attribMap  = (Map)metaInfoMap.get(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES);
50             for(Iterator it = attribMap.values().iterator();it.hasNext();){
51                  if (node.getNodeType()==Node.ELEMENT_NODE){
52                      ((Element)node).setAttributeNodeNS((Attr)parentDoc.importNode((Node)it.next(),true));
53                  }
54  
55              }
56          }
57  
58          if (metaInfoMap.containsKey(Constants.MetaDataConstants.EXTERNAL_ELEMENTS)){
59              Map elementMap  = (Map)metaInfoMap.get(Constants.MetaDataConstants.EXTERNAL_ELEMENTS);
60              for(Iterator it = elementMap.values().iterator();it.hasNext();){
61                  node.appendChild(
62                          parentDoc.importNode((Node)it.next(),true));
63              }
64          }
65  
66      }
67  }