KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > extras > DocumentModelMapperBase


1 /*
2 Copyright (c) 2004, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.extras;
30
31 import org.jibx.runtime.IXMLWriter;
32 import org.jibx.runtime.JiBXException;
33 import org.jibx.runtime.impl.UnmarshallingContext;
34
35 /**
36  * <p>Base implementation for custom marshaller/unmarshallers to any document
37  * model representation. This class just provides a few basic operations that
38  * are used by the representation-specific subclasses.</p>
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class DocumentModelMapperBase
45 {
46     /** Fixed XML namespace. */
47     public static final String JavaDoc XML_NAMESPACE =
48         "http://www.w3.org/XML/1998/namespace";
49     
50     /** Fixed XML namespace namespace. */
51     public static final String JavaDoc XMLNS_NAMESPACE =
52         "http://www.w3.org/2000/xmlns/";
53         
54     /** Writer for direct output as XML. */
55     protected IXMLWriter m_xmlWriter;
56     
57     /** Context being used for unmarshalling. */
58     protected UnmarshallingContext m_unmarshalContext;
59     
60     /**
61      * Get namespace URI for index.
62      *
63      * @param index namespace index to look up
64      * @return uri namespace URI at index position
65      */

66     
67     protected String JavaDoc getNamespaceUri(int index) {
68         String JavaDoc[] uris = m_xmlWriter.getNamespaces();
69         if (index < uris.length) {
70             return uris[index];
71         } else {
72             index -= uris.length;
73             String JavaDoc[][] uriss = m_xmlWriter.getExtensionNamespaces();
74             if (uriss != null) {
75                 for (int i = 0; i < uriss.length; i++) {
76                     uris = uriss[i];
77                     if (index < uris.length) {
78                         return uris[index];
79                     } else {
80                         index -= uris.length;
81                     }
82                 }
83             }
84         }
85         return null;
86     }
87     
88     /**
89      * Get next namespace index.
90      *
91      * @return next namespace index
92      */

93     
94     protected int getNextNamespaceIndex() {
95         int count = m_xmlWriter.getNamespaces().length;
96         String JavaDoc[][] uriss = m_xmlWriter.getExtensionNamespaces();
97         if (uriss != null) {
98             for (int i = 0; i < uriss.length; i++) {
99                 count += uriss[i].length;
100             }
101         }
102         return count;
103     }
104     
105     /**
106      * Accumulate text content. This consolidates consecutive text and entities
107      * to a single string.
108      *
109      * @return consolidated text string
110      * @exception JiBXException on error in unmarshalling
111      */

112     
113     protected String JavaDoc accumulateText() throws JiBXException {
114         String JavaDoc text = m_unmarshalContext.getText();
115         StringBuffer JavaDoc buff = null;
116         while (true) {
117             int cev = m_unmarshalContext.nextToken();
118             if (cev == UnmarshallingContext.TEXT ||
119                 (cev == UnmarshallingContext.ENTITY_REF &&
120                 m_unmarshalContext.getText() != null)) {
121                 if (buff == null) {
122                     buff = new StringBuffer JavaDoc(text);
123                 }
124                 buff.append(m_unmarshalContext.getText());
125             } else {
126                 break;
127             }
128         }
129         if (buff == null) {
130             return text;
131         } else {
132             return buff.toString();
133         }
134     }
135     
136     /**
137      * Check if a character is a space character.
138      *
139      * @param chr character to be checked
140      * @return <code>true</code> if whitespace, <code>false</code> if not
141      */

142     
143     protected boolean isWhitespace(char chr) {
144         if (chr <= 0x20) {
145             return chr == 0x20 || chr == 0x09 || chr == 0x0A || chr == 0x0D;
146         } else {
147             return false;
148         }
149     }
150 }
Popular Tags