MapSerializer.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.serializer;
00020 
00021 import java.util.Iterator;
00022 import java.util.Map;
00023 
00024 import org.apache.xmlrpc.common.TypeFactory;
00025 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00026 import org.xml.sax.ContentHandler;
00027 import org.xml.sax.SAXException;
00028 
00029 
00032 public class MapSerializer extends TypeSerializerImpl {
00035     public static final String STRUCT_TAG = "struct";
00036 
00039     public static final String MEMBER_TAG = "member";
00040 
00043     public static final String NAME_TAG = "name";
00044 
00045     private final XmlRpcStreamConfig config;
00046         private final TypeFactory typeFactory;
00047 
00052         public MapSerializer(TypeFactory pTypeFactory, XmlRpcStreamConfig pConfig) {
00053                 typeFactory = pTypeFactory;
00054                 config = pConfig;
00055         }
00056 
00057     protected void writeEntry(ContentHandler pHandler, Object pKey, Object pValue) throws SAXException {
00058                 pHandler.startElement("", MEMBER_TAG, MEMBER_TAG, ZERO_ATTRIBUTES);
00059                 pHandler.startElement("", NAME_TAG, NAME_TAG, ZERO_ATTRIBUTES);
00060                 if (config.isEnabledForExtensions()  &&  !(pKey instanceof String)) {
00061                     writeValue(pHandler, pKey);
00062         } else {
00063             String key = pKey.toString();
00064             pHandler.characters(key.toCharArray(), 0, key.length());
00065         }
00066                 pHandler.endElement("", NAME_TAG, NAME_TAG);
00067                 writeValue(pHandler, pValue);
00068                 pHandler.endElement("", MEMBER_TAG, MEMBER_TAG);
00069         }
00070 
00071     private void writeValue(ContentHandler pHandler, Object pValue)
00072             throws SAXException {
00073         TypeSerializer ts = typeFactory.getSerializer(config, pValue);
00074                 if (ts == null) {
00075                         throw new SAXException("Unsupported Java type: " + pValue.getClass().getName());
00076                 }
00077                 ts.write(pHandler, pValue);
00078     }
00079 
00080     protected void writeData(ContentHandler pHandler, Object pData) throws SAXException {
00081                 Map map = (Map) pData;
00082                 for (Iterator iter = map.entrySet().iterator();  iter.hasNext();  ) {
00083                         Map.Entry entry = (Map.Entry) iter.next();
00084                         writeEntry(pHandler, entry.getKey(), entry.getValue());
00085                 }
00086         }
00087 
00088     public void write(final ContentHandler pHandler, Object pObject) throws SAXException {
00089                 pHandler.startElement("", VALUE_TAG, VALUE_TAG, ZERO_ATTRIBUTES);
00090                 pHandler.startElement("", STRUCT_TAG, STRUCT_TAG, ZERO_ATTRIBUTES);
00091                 writeData(pHandler, pObject);
00092                 pHandler.endElement("", STRUCT_TAG, STRUCT_TAG);
00093                 pHandler.endElement("", VALUE_TAG, VALUE_TAG);
00094         }
00095 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49