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.render;
019:
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.io.Writer;
023: import java.util.Collection;
024: import java.util.Map;
025:
026: import de.finix.contelligent.CallData;
027: import de.finix.contelligent.content.TextContent;
028: import de.finix.contelligent.exception.ContelligentException;
029: import de.finix.contelligent.logging.LoggingService;
030:
031: public class XMLRenderer implements Renderer {
032: final static org.apache.log4j.Logger log = LoggingService
033: .getLogger(XMLRenderer.class);
034:
035: public final static String MODE = "mode";
036:
037: public final static String MODE_RAW = "raw";
038:
039: private TextContent content;
040:
041: private Collection sensitiveCategories;
042:
043: public XMLRenderer(TextContent content) {
044: this .content = content;
045: this .sensitiveCategories = content.getSensitiveCategories();
046: }
047:
048: public ParameterDescription[] getParameterDescription() {
049: return new ParameterDescription[0];
050: }
051:
052: public Collection getSensitiveCategories() {
053: return sensitiveCategories;
054: }
055:
056: public void render(Writer writer, Map parameterMap,
057: CallData callData) throws ContelligentException,
058: IOException {
059:
060: String renderMode = null;
061:
062: if (parameterMap != null && parameterMap.containsKey(MODE)) {
063: renderMode = ((String[]) parameterMap.get(MODE))[0];
064: if (renderMode.equals(MODE_RAW)) {
065: String stringContent = content.getText(callData);
066: if (stringContent != null && stringContent.length() > 0) {
067: writer.write(stringContent);
068: }
069: return;
070: }
071: }
072:
073: String stringContent = getTruncatedContent(callData);
074:
075: if (stringContent != null && stringContent.length() > 0) {
076: writer.write(stringContent);
077: }
078: }
079:
080: public String getTruncatedContent(CallData callData) {
081:
082: StringBuffer buffer = new StringBuffer();
083: String ret = "";
084:
085: try {
086: int current;
087: String stringContent = content.getText(callData);
088: StringReader sr = new StringReader(stringContent);
089: boolean writeOn = true;
090:
091: current = sr.read();
092:
093: while (current != -1) {
094:
095: if (current == '?') {
096: current = sr.read();
097: if (current == '>') {
098: current = sr.read();
099: writeOn = true;
100: } else {
101: buffer.append('?');
102: }
103: }
104:
105: if (current == '<') {
106: current = sr.read();
107: if (current == '?') {
108: writeOn = false;
109: } else {
110: buffer.append('<');
111: }
112: }
113:
114: if ((writeOn) && (current != -1))
115: buffer.append((char) current);
116:
117: if (current != -1)
118: current = sr.read();
119:
120: }
121: ret = buffer.toString();
122: } catch (IOException e) {
123: if (log.isDebugEnabled()) {
124: log.debug("EXC truncating XML Header:");
125: e.printStackTrace();
126: } else
127: log.error("EXC truncating XML Header:" + e.toString());
128: } catch (ContelligentException e) {
129: if (log.isDebugEnabled()) {
130: log.debug("CEXC truncating XML Header:");
131: e.printStackTrace();
132: } else
133: log.error("CEXC truncating XML Header:" + e.toString());
134: }
135:
136: return ret;
137: }
138:
139: }
|