001: /*
002: Copyright (c) 2004, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.extras;
030:
031: import java.io.IOException;
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: import org.jibx.runtime.IMarshaller;
037: import org.jibx.runtime.IMarshallingContext;
038: import org.jibx.runtime.IUnmarshaller;
039: import org.jibx.runtime.IUnmarshallingContext;
040: import org.jibx.runtime.IXMLReader;
041: import org.jibx.runtime.JiBXException;
042: import org.jibx.runtime.impl.UnmarshallingContext;
043: import org.w3c.dom.Node;
044:
045: /**
046: * <p>Custom content list marshaller/unmarshaller to DOM representation. This
047: * allows you to mix data binding and document model representations for XML
048: * within the same application. You simply use this marshaller/unmarshaller with
049: * a linked object type that implements <code>java.util.List</code> (the actual
050: * runtime type - the declared type is ignored and can be anything). When
051: * unmarshalling it will create an instance of <code>java.util.ArrayList</code>
052: * if a list is not passed in and any content is present, then return all the
053: * content up to the close tag for the enclosing element in the list. When
054: * marshalling, it will simply write out any content directly.</p>
055: *
056: * @author Dennis M. Sosnoski
057: * @version 1.0
058: */
059:
060: public class DomListMapper extends DomMapperBase implements
061: IMarshaller, IUnmarshaller {
062: /**
063: * Default constructor.
064: *
065: * @throws JiBXException on configuration error
066: */
067: public DomListMapper() throws JiBXException {
068: super ();
069: }
070:
071: /* (non-Javadoc)
072: * @see org.jibx.runtime.IMarshaller#isExtension(int)
073: */
074:
075: public boolean isExtension(int index) {
076: return false;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
081: * org.jibx.runtime.IMarshallingContext)
082: */
083:
084: public void marshal(Object obj, IMarshallingContext ictx)
085: throws JiBXException {
086:
087: // make sure the parameters are as expected
088: if (!(obj instanceof List)) {
089: throw new JiBXException(
090: "Mapped object not a java.util.List");
091: } else {
092: try {
093:
094: // marshal content list with no indentation
095: m_xmlWriter = ictx.getXmlWriter();
096: int indent = ictx.getIndent();
097: ictx.setIndent(-1);
098: m_defaultNamespaceURI = null;
099: for (Iterator iter = ((List) obj).iterator(); iter
100: .hasNext();) {
101: Object item = iter.next();
102: if (item instanceof Node) {
103: marshalNode((Node) item);
104: } else {
105: throw new JiBXException(
106: "List item is not an org.w3c.dom.Node");
107: }
108: }
109: ictx.setIndent(indent);
110:
111: } catch (IOException e) {
112: throw new JiBXException("Error writing to document", e);
113: }
114: }
115: }
116:
117: /* (non-Javadoc)
118: * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
119: */
120:
121: public boolean isPresent(IUnmarshallingContext ctx)
122: throws JiBXException {
123: if (!(ctx instanceof UnmarshallingContext)) {
124: throw new JiBXException(
125: "Unmarshalling context not of expected type");
126: } else {
127: return ((UnmarshallingContext) ctx).currentEvent() != IXMLReader.END_TAG;
128: }
129: }
130:
131: /* (non-Javadoc)
132: * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
133: * org.jibx.runtime.IUnmarshallingContext)
134: */
135:
136: public Object unmarshal(Object obj, IUnmarshallingContext ictx)
137: throws JiBXException {
138:
139: // verify the entry conditions
140: boolean created = false;
141: List list = null;
142: if (obj == null) {
143: list = new ArrayList();
144: created = true;
145: } else if (obj instanceof List) {
146: list = (List) obj;
147: } else {
148: throw new JiBXException(
149: "Supplied object is not a java.util.List");
150: }
151: if (!(ictx instanceof UnmarshallingContext)) {
152: throw new JiBXException(
153: "Unmarshalling context not of expected type");
154: }
155:
156: // unmarshal content to document model
157: m_unmarshalContext = (UnmarshallingContext) ictx;
158: try {
159: Node node;
160: while ((node = unmarshalNode()) != null) {
161: list.add(node);
162: }
163: if (created && list.isEmpty()) {
164: return null;
165: } else {
166: return list;
167: }
168: } catch (IOException e) {
169: throw new JiBXException("Error reading from document", e);
170: }
171: }
172: }
|