001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util.xml;
019:
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import org.xml.sax.helpers.AttributesImpl;
024:
025: import de.zeigermann.xml.XMLWriter;
026: import de.zeigermann.xml.simpleImporter.SimpleImportHandler;
027: import de.zeigermann.xml.simpleImporter.SimplePath;
028:
029: public class DumpImportHandler implements SimpleImportHandler {
030:
031: private static Logger logger = Logger
032: .getLogger(DumpImportHandler.class.getName());
033:
034: private Level logLevel;
035:
036: private int dumpStyle;
037:
038: private StringBuffer logBuffer = null;
039:
040: public final static int XML_DUMP_STYLE = 1;
041:
042: public final static int DETAILED_DUMP_STYLE = 2;
043:
044: public DumpImportHandler(Level logLevel, int dumpStyle,
045: boolean logIntoBuffer) {
046: this .logLevel = logLevel;
047: this .dumpStyle = dumpStyle;
048: if (logIntoBuffer) {
049: logBuffer = new StringBuffer();
050: }
051: }
052:
053: public DumpImportHandler(Level logLevel, boolean logIntoBuffer) {
054: this (logLevel, XML_DUMP_STYLE, logIntoBuffer);
055: }
056:
057: public DumpImportHandler(Level logLevel) {
058: this (logLevel, XML_DUMP_STYLE, false);
059: }
060:
061: public DumpImportHandler() {
062: this (null, XML_DUMP_STYLE, true);
063: }
064:
065: public String getDump() {
066: if (logBuffer != null) {
067: return logBuffer.toString();
068: }
069: return null;
070: }
071:
072: public void startDocument() {
073: if (dumpStyle == DETAILED_DUMP_STYLE) {
074: log("");
075: log("DOCUMENT START");
076: log("");
077: }
078: }
079:
080: public void endDocument() {
081: if (dumpStyle == DETAILED_DUMP_STYLE) {
082: log("");
083: log("DOCUMENT END");
084: log("");
085: }
086: }
087:
088: public void cData(SimplePath path, String cdata) {
089: if (dumpStyle == DETAILED_DUMP_STYLE) {
090: log("*************** MIXED ***************");
091: log(path.toString() + "/text():");
092: log("<![CDATA[" + cdata + "]]>");
093: } else if (dumpStyle == XML_DUMP_STYLE) {
094: log(cdata);
095: }
096: }
097:
098: public void startElement(SimplePath path, String name,
099: AttributesImpl attributes, String firstPCData) {
100: String startTag = XMLWriter.createStartTag(name, attributes);
101: if (dumpStyle == DETAILED_DUMP_STYLE) {
102: log("");
103: log(path.toString() + ":");
104: log(startTag);
105: if (firstPCData != null)
106: log("<![CDATA[" + firstPCData + "]]>");
107: } else if (dumpStyle == XML_DUMP_STYLE) {
108: log(startTag);
109: if (firstPCData != null)
110: log(firstPCData);
111: }
112: }
113:
114: public void endElement(SimplePath path, String name) {
115: if (dumpStyle == DETAILED_DUMP_STYLE) {
116: log("");
117: log(path.toString() + ":");
118: log("</" + name + ">");
119: } else if (dumpStyle == XML_DUMP_STYLE) {
120: log("</" + name + ">");
121: }
122: }
123:
124: private void log(String text) {
125: if (logLevel != null) {
126: logger.log(logLevel, text);
127: }
128: if (logBuffer != null) {
129: logBuffer.append(text).append('\n');
130: }
131: }
132: }
|