001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.databinding.utils;
021:
022: import org.apache.axiom.om.OMAttribute;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axiom.om.OMNode;
025: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
026: import org.apache.axis2.AxisFault;
027: import org.apache.axis2.databinding.typemapping.SimpleTypeMapper;
028: import org.apache.axis2.engine.ObjectSupplier;
029:
030: import javax.xml.namespace.QName;
031: import java.util.HashMap;
032: import java.util.Iterator;
033:
034: public class MultirefHelper {
035:
036: public static final String SOAP12_REF_ATTR = "ref";
037: public static final String SOAP11_REF_ATTR = "href";
038:
039: private boolean filledTable;
040:
041: private OMElement parent;
042:
043: private HashMap objectmap = new HashMap();
044: private HashMap elementMap = new HashMap();
045: private HashMap omElementMap = new HashMap();
046:
047: public MultirefHelper(OMElement parent) {
048: this .parent = parent;
049: }
050:
051: public Object getObject(String id) {
052: return objectmap.get(id);
053: }
054:
055: public OMElement getOMElement(String id) {
056: return (OMElement) omElementMap.get(id);
057: }
058:
059: public OMElement processOMElementRef(String id) throws AxisFault {
060: if (!filledTable) {
061: readallChildElements();
062: }
063: OMElement val = (OMElement) elementMap.get(id);
064: if (val == null) {
065: throw new AxisFault("Invalid reference :" + id);
066: } else {
067: OMElement ele = processElementforRefs(val);
068: OMElement cloneele = elementClone(ele);
069: omElementMap.put(id, cloneele);
070: return cloneele;
071: }
072: }
073:
074: public OMElement processElementforRefs(OMElement elemnts)
075: throws AxisFault {
076: Iterator itr = elemnts.getChildElements();
077: while (itr.hasNext()) {
078: OMElement omElement = (OMElement) itr.next();
079: OMAttribute attri = processRefAtt(omElement);
080: if (attri != null) {
081: String ref = getAttvalue(attri);
082: OMElement tempele = getOMElement(ref);
083: if (tempele == null) {
084: tempele = processOMElementRef(ref);
085: }
086: OMElement ele2 = elementClone(tempele);
087: Iterator itrChild = ele2.getChildren();
088: while (itrChild.hasNext()) {
089: Object obj = itrChild.next();
090: if (obj instanceof OMNode) {
091: omElement.addChild((OMNode) obj);
092: }
093: }
094: }
095: }
096: return elemnts;
097: }
098:
099: private OMElement elementClone(OMElement ele) {
100: return new StAXOMBuilder(ele.getXMLStreamReader())
101: .getDocumentElement();
102: }
103:
104: public Object processRef(Class javatype, String id,
105: ObjectSupplier objectSupplier) throws AxisFault {
106: if (!filledTable) {
107: readallChildElements();
108: }
109: OMElement val = (OMElement) elementMap.get(id);
110: if (val == null) {
111: throw new AxisFault("Invalid reference :" + id);
112: } else {
113: if (SimpleTypeMapper.isSimpleType(javatype)) {
114: /**
115: * in this case OM element can not contains more child, that is no way to get
116: * the value as an exp ,
117: * <refernce id="12">
118: * <value>foo</value>
119: * </refernce>
120: * the above one is not valid , that should always be like below
121: * <refernce id="12">foo</refernce>
122: */
123: Object valObj = SimpleTypeMapper.getSimpleTypeObject(
124: javatype, val);
125: objectmap.put(id, valObj);
126: return valObj;
127: } else if (SimpleTypeMapper.isCollection(javatype)) {
128: Object valobj = SimpleTypeMapper.getArrayList(val);
129: objectmap.put(id, valobj);
130: return valobj;
131: } else {
132: Object obj = BeanUtil.deserialize(javatype, val, this ,
133: objectSupplier);
134: objectmap.put(id, obj);
135: return obj;
136: }
137: }
138: }
139:
140: private void readallChildElements() {
141: Iterator childs = parent.getChildElements();
142: while (childs.hasNext()) {
143: OMElement omElement = (OMElement) childs.next();
144: OMAttribute id = omElement.getAttribute(new QName("id"));
145: if (id != null) {
146: omElement.build();
147: elementMap.put(id.getAttributeValue(), omElement
148: .detach());
149: }
150: }
151: filledTable = true;
152: }
153:
154: public static String getAttvalue(OMAttribute omatribute) {
155: String ref;
156: ref = omatribute.getAttributeValue();
157: if (ref != null) {
158: if (ref.charAt(0) == '#') {
159: ref = ref.substring(1);
160: }
161: }
162: return ref;
163: }
164:
165: public static OMAttribute processRefAtt(OMElement omElement) {
166: OMAttribute omatribute = omElement.getAttribute(new QName(
167: SOAP11_REF_ATTR));
168: if (omatribute == null) {
169: omatribute = omElement.getAttribute(new QName(
170: SOAP12_REF_ATTR));
171: }
172: return omatribute;
173: }
174:
175: public void clean() {
176: elementMap.clear();
177: objectmap.clear();
178: }
179:
180: }
|