KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > sunday > marshalling > DefaultAttributeMarshaller


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.sunday.marshalling;
23
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import org.jboss.xb.binding.Constants;
28 import org.jboss.xb.binding.JBossXBRuntimeException;
29 import org.jboss.xb.binding.SimpleTypeBindings;
30 import org.jboss.xb.binding.Util;
31 import org.jboss.xb.binding.introspection.FieldInfo;
32 import org.jboss.xb.binding.metadata.PropertyMetaData;
33 import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
34 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
35 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
36
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 1958 $</tt>
41  */

42 public class DefaultAttributeMarshaller
43    extends AbstractAttributeMarshaller
44 {
45    public static final DefaultAttributeMarshaller INSTANCE = new DefaultAttributeMarshaller();
46    
47    public Object JavaDoc getValue(MarshallingContext ctx)
48    {
49       Object JavaDoc owner = ctx.peek();
50       SchemaBinding schema = ctx.getSchemaBinding();
51       AttributeBinding binding = ctx.getAttributeBinding();
52       QName JavaDoc qName = binding.getQName();
53
54       String JavaDoc fieldName = null;
55       PropertyMetaData propertyMetaData = binding.getPropertyMetaData();
56       if(propertyMetaData != null)
57       {
58          fieldName = propertyMetaData.getName();
59       }
60
61       if(fieldName == null)
62       {
63          fieldName =
64             Util.xmlNameToFieldName(qName.getLocalPart(), schema.isIgnoreLowLine());
65       }
66
67       FieldInfo fieldInfo = FieldInfo.getFieldInfo(
68          owner.getClass(), fieldName, binding.getRequired() && !schema.isIgnoreUnresolvedFieldOrClass()
69       );
70       Object JavaDoc value = null;
71       if(fieldInfo != null)
72       {
73          value = fieldInfo.getValue(owner);
74       }
75
76       return value;
77    }
78
79    public String JavaDoc marshalValue(MarshallingContext ctx, Object JavaDoc value)
80    {
81       if(value == null)
82       {
83          return null;
84       }
85
86       String JavaDoc marshalled;
87
88       AttributeBinding binding = ctx.getAttributeBinding();
89       TypeBinding attrType = binding.getType();
90
91       if(attrType.getItemType() != null)
92       {
93          TypeBinding itemType = attrType.getItemType();
94          if(Constants.NS_XML_SCHEMA.equals(itemType.getQName().getNamespaceURI()))
95          {
96             List JavaDoc list;
97             if(value instanceof List JavaDoc)
98             {
99                list = (List JavaDoc)value;
100             }
101             else if(value.getClass().isArray())
102             {
103                list = Arrays.asList((Object JavaDoc[])value);
104             }
105             else
106             {
107                throw new JBossXBRuntimeException("Expected value for list type is an array or " +
108                   List JavaDoc.class.getName() +
109                   " but got: " +
110                   value
111                );
112             }
113
114             if(Constants.QNAME_QNAME.getLocalPart().equals(itemType.getQName().getLocalPart()))
115             {
116                String JavaDoc attrLocal = binding.getQName().getLocalPart();
117                for(int listInd = 0; listInd < list.size(); ++listInd)
118                {
119                   QName JavaDoc item = (QName JavaDoc)list.get(listInd);
120                   String JavaDoc itemNs = item.getNamespaceURI();
121                   if(itemNs != null && itemNs.length() > 0)
122                   {
123                      String JavaDoc itemPrefix = ctx.getPrefix(itemNs);
124                      if(itemPrefix == null)
125                      {
126                         itemPrefix = item.getPrefix();
127                         if(itemPrefix == null || itemPrefix.length() == 0)
128                         {
129                            itemPrefix = attrLocal + listInd;
130                         }
131                         ctx.declareNamespace(itemPrefix, itemNs);
132                      }
133
134                      if(!itemPrefix.equals(item.getPrefix()))
135                      {
136                         item = new QName JavaDoc(item.getNamespaceURI(), item.getLocalPart(), itemPrefix);
137                         list.set(listInd, item);
138                      }
139                   }
140                }
141             }
142
143             marshalled = SimpleTypeBindings.marshalList(itemType.getQName().getLocalPart(), list, null);
144          }
145          else
146          {
147             throw new JBossXBRuntimeException("Marshalling of list types with item types not from " +
148                Constants.NS_XML_SCHEMA + " is not supported."
149             );
150          }
151       }
152       else if(attrType.getLexicalPattern() != null &&
153          attrType.getBaseType() != null &&
154          Constants.QNAME_BOOLEAN.equals(attrType.getBaseType().getQName()))
155       {
156          String JavaDoc item = (String JavaDoc)attrType.getLexicalPattern().get(0);
157          if(item.indexOf('0') != -1 && item.indexOf('1') != -1)
158          {
159             marshalled = ((Boolean JavaDoc)value).booleanValue() ? "1" : "0";
160          }
161          else
162          {
163             marshalled = ((Boolean JavaDoc)value).booleanValue() ? "true" : "false";
164          }
165       }
166       else if(Constants.QNAME_QNAME.equals(attrType.getQName()))
167       {
168          boolean removePrefix = false;
169          String JavaDoc prefix = null;
170          String JavaDoc ns = ((QName JavaDoc)value).getNamespaceURI();
171          if(ns != null && ns.length() > 0)
172          {
173             prefix = ctx.getPrefix(ns);
174             if(prefix == null)
175             {
176                prefix = ((QName JavaDoc)value).getPrefix();
177                if(prefix == null || prefix.length() == 0)
178                {
179                   prefix = "ns_" + ((QName JavaDoc)value).getLocalPart();
180                }
181                ctx.declareNamespace(prefix, ns);
182             }
183             ctx.getNamespaceContext().addPrefixMapping(prefix, ns);
184             removePrefix = true;
185          }
186
187          marshalled = SimpleTypeBindings.marshalQName((QName JavaDoc)value, ctx.getNamespaceContext());
188
189          if(removePrefix)
190          {
191             ctx.getNamespaceContext().removePrefixMapping(prefix);
192          }
193       }
194       else
195       {
196          marshalled = value.toString();
197       }
198
199       return marshalled;
200    }
201
202 }
203
Popular Tags