001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.xml;
034:
035: import org.xml.sax.Attributes;
036: import org.xml.sax.ContentHandler;
037: import org.xml.sax.SAXException;
038: import org.xml.sax.helpers.AttributesImpl;
039:
040: import sun.misc.BASE64Encoder;
041:
042: import java.io.IOException;
043: import java.io.InputStream;
044:
045: import java.util.Hashtable;
046: import java.util.Iterator;
047: import java.util.Set;
048:
049: /**
050: * LibreSource
051: *
052: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
053: * href="http://www.inria.fr">INRIA Lorraine</a>
054: */
055: public class XmlDumpHelper {
056: public static String nsu = ""; // the namespace
057: public static Attributes attributes = new AttributesImpl();
058: private static Hashtable handlers = new Hashtable();
059:
060: public static Attributes getEmptyAttributesSet() {
061: return attributes;
062: }
063:
064: public static void startDocument(String prefix, String rootTag,
065: Attributes attr, ContentHandler handler)
066: throws SAXException {
067: handlers.put(handler, new Integer(0));
068: handler.startDocument();
069: beginElement(prefix, rootTag, attr, handler);
070: handlers.put(handler, new Integer(1));
071: }
072:
073: public static void newLine(ContentHandler handler)
074: throws SAXException {
075: handler.ignorableWhitespace("\n".toCharArray(), 0, "\n"
076: .length());
077: }
078:
079: private static void indent(ContentHandler handler)
080: throws SAXException {
081: int n = ((Integer) handlers.get(handler)).intValue();
082:
083: if (n != 0) {
084: newLine(handler);
085:
086: for (int i = 0; i < n; i++) {
087: handler.ignorableWhitespace("\t".toCharArray(), 0, "\t"
088: .length());
089: }
090: }
091: }
092:
093: public static void endDocument(String prefix, String rootTag,
094: ContentHandler handler) throws SAXException {
095: endElement(prefix, rootTag, handler);
096: handler.endDocument();
097: handlers.remove(handler);
098: }
099:
100: public static void endElement(String prefix, String tagname,
101: ContentHandler handler) throws SAXException {
102: int n = ((Integer) handlers.get(handler)).intValue();
103: handlers.put(handler, new Integer(n - 1));
104: indent(handler);
105: handler.endElement(nsu, tagname, prefix + ":" + tagname);
106: }
107:
108: public static void outputElementWithContent(String prefix,
109: String tagname, String content, Attributes attr,
110: ContentHandler handler) throws SAXException {
111: int n = ((Integer) handlers.get(handler)).intValue();
112: indent(handler);
113: handler
114: .startElement(nsu, tagname, prefix + ":" + tagname,
115: attr);
116: handlers.put(handler, new Integer(n + 1));
117: indent(handler);
118:
119: if (content != null) {
120: handler.characters(content.toCharArray(), 0, content
121: .length());
122: }
123:
124: handlers.put(handler, new Integer(n));
125: indent(handler);
126: handler.endElement(nsu, tagname, prefix + ":" + tagname);
127: }
128:
129: public static void outputElementWithContent(String prefix,
130: String tagname, InputStream is, Attributes attr,
131: ContentHandler handler) throws SAXException, IOException {
132: int n = ((Integer) handlers.get(handler)).intValue();
133: indent(handler);
134: handler
135: .startElement(nsu, tagname, prefix + ":" + tagname,
136: attr);
137: handlers.put(handler, new Integer(n + 1));
138: indent(handler);
139:
140: if (is != null) {
141: byte[] data = new byte[300072];
142: int byteCount = 0;
143:
144: while ((byteCount = is.read(data, 0, 300072)) > -1) {
145: byte[] toEncode = new byte[byteCount];
146: System.arraycopy(data, 0, toEncode, 0, byteCount);
147:
148: char[] encoded = new BASE64Encoder().encode(toEncode)
149: .toCharArray();
150: handler.characters(encoded, 0, encoded.length);
151: }
152: }
153:
154: handlers.put(handler, new Integer(n));
155: indent(handler);
156: handler.endElement(nsu, tagname, prefix + ":" + tagname);
157: }
158:
159: public static void outputEmptyElement(String prefix,
160: String tagname, Attributes attr, ContentHandler handler)
161: throws SAXException {
162: indent(handler);
163: handler
164: .startElement(nsu, tagname, prefix + ":" + tagname,
165: attr);
166: handler.endElement(nsu, tagname, prefix + ":" + tagname);
167: }
168:
169: public static Attributes generateAttributesSet(
170: XmlDumpAttributes attrList) {
171: Attributes attributes = new AttributesImpl();
172: Set keySet = attrList.keySet();
173:
174: for (Iterator i = keySet.iterator(); i.hasNext();) {
175: String key = (String) i.next();
176: String value = attrList.get(key).toString();
177: ((AttributesImpl) attributes).addAttribute("", "", key, "",
178: value);
179: }
180:
181: return attributes;
182: }
183:
184: public static void beginElement(String prefix, String tagname,
185: Attributes attr, ContentHandler handler)
186: throws SAXException {
187: indent(handler);
188: handler
189: .startElement(nsu, tagname, prefix + ":" + tagname,
190: attr);
191:
192: int n = ((Integer) handlers.get(handler)).intValue();
193: handlers.put(handler, new Integer(n + 1));
194: }
195: }
|