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 org.jibx.runtime.IXMLReader;
032: import org.jibx.runtime.IXMLWriter;
033: import org.jibx.runtime.JiBXException;
034: import org.jibx.runtime.impl.UnmarshallingContext;
035:
036: /**
037: * <p>Base implementation for custom marshaller/unmarshallers to any document
038: * model representation. This class just provides a few basic operations that
039: * are used by the representation-specific subclasses.</p>
040: *
041: * @author Dennis M. Sosnoski
042: * @version 1.0
043: */
044:
045: public class DocumentModelMapperBase {
046: /** Fixed XML namespace. */
047: public static final String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
048:
049: /** Fixed XML namespace namespace. */
050: public static final String XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
051:
052: /** Writer for direct output as XML. */
053: protected IXMLWriter m_xmlWriter;
054:
055: /** Context being used for unmarshalling. */
056: protected UnmarshallingContext m_unmarshalContext;
057:
058: /**
059: * Get namespace URI for index.
060: *
061: * @param index namespace index to look up
062: * @return uri namespace URI at index position
063: */
064:
065: protected String getNamespaceUri(int index) {
066: String[] uris = m_xmlWriter.getNamespaces();
067: if (index < uris.length) {
068: return uris[index];
069: } else {
070: index -= uris.length;
071: String[][] uriss = m_xmlWriter.getExtensionNamespaces();
072: if (uriss != null) {
073: for (int i = 0; i < uriss.length; i++) {
074: uris = uriss[i];
075: if (index < uris.length) {
076: return uris[index];
077: } else {
078: index -= uris.length;
079: }
080: }
081: }
082: }
083: return null;
084: }
085:
086: /**
087: * Get next namespace index.
088: *
089: * @return next namespace index
090: */
091:
092: protected int getNextNamespaceIndex() {
093: int count = m_xmlWriter.getNamespaces().length;
094: String[][] uriss = m_xmlWriter.getExtensionNamespaces();
095: if (uriss != null) {
096: for (int i = 0; i < uriss.length; i++) {
097: count += uriss[i].length;
098: }
099: }
100: return count;
101: }
102:
103: /**
104: * Accumulate text content. This consolidates consecutive text and entities
105: * to a single string.
106: *
107: * @return consolidated text string
108: * @exception JiBXException on error in unmarshalling
109: */
110:
111: protected String accumulateText() throws JiBXException {
112: String text = m_unmarshalContext.getText();
113: StringBuffer buff = null;
114: while (true) {
115: int cev = m_unmarshalContext.nextToken();
116: if (cev == IXMLReader.TEXT
117: || (cev == IXMLReader.ENTITY_REF && m_unmarshalContext
118: .getText() != null)) {
119: if (buff == null) {
120: buff = new StringBuffer(text);
121: }
122: buff.append(m_unmarshalContext.getText());
123: } else {
124: break;
125: }
126: }
127: if (buff == null) {
128: return text;
129: } else {
130: return buff.toString();
131: }
132: }
133:
134: /**
135: * Check if a character is a space character.
136: *
137: * @param chr character to be checked
138: * @return <code>true</code> if whitespace, <code>false</code> if not
139: */
140:
141: protected boolean isWhitespace(char chr) {
142: if (chr <= 0x20) {
143: return chr == 0x20 || chr == 0x09 || chr == 0x0A
144: || chr == 0x0D;
145: } else {
146: return false;
147: }
148: }
149: }
|