001: /* CVS ID: $Id: XMLMessagePart.java,v 1.1.1.1 2002/10/02 18:42:54 wastl Exp $ */
002: package net.wastl.webmail.xml;
003:
004: import java.util.*;
005:
006: import org.w3c.dom.*;
007:
008: /*
009: * XMLMessagePart.java
010: *
011: * Created: Tue Apr 18 14:08:56 2000
012: *
013: * Copyright (C) 2000 Sebastian Schaffert
014: *
015: * This program is free software; you can redistribute it and/or
016: * modify it under the terms of the GNU General Public License
017: * as published by the Free Software Foundation; either version 2
018: * of the License, or (at your option) any later version.
019: *
020: * This program is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * You should have received a copy of the GNU General Public License
026: * along with this program; if not, write to the Free Software
027: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
028: *
029: */
030:
031: /**
032: * A message part object for an XML message
033: */
034: public class XMLMessagePart {
035:
036: protected Document root;
037: protected Element part;
038:
039: /**
040: * Create a new part for the given root document.
041: * Creates the necessary Element.
042: */
043: public XMLMessagePart(Document root) {
044: this .part = root.createElement("PART");
045: this .root = root;
046: }
047:
048: /**
049: * Return a new part for a given part element
050: */
051: public XMLMessagePart(Element part) {
052: this .part = part;
053: this .root = part.getOwnerDocument();
054: }
055:
056: public Element getPartElement() {
057: return part;
058: }
059:
060: public void setAttribute(String key, String value) {
061: part.setAttribute(key, value);
062: }
063:
064: public String getAttribute(String key) {
065: return part.getAttribute(key);
066: }
067:
068: public void quoteContent() {
069: NodeList nl = part.getChildNodes();
070: StringBuffer text = new StringBuffer();
071: for (int i = 0; i < nl.getLength(); i++) {
072: Element elem = (Element) nl.item(i);
073: if (elem.getNodeName().equals("CONTENT")) {
074: String value = XMLCommon.getElementTextValue(elem);
075: StringTokenizer tok = new StringTokenizer(value, "\n");
076: while (tok.hasMoreTokens()) {
077: text.append("> ").append(tok.nextToken()).append(
078: "\n");
079: }
080: }
081: }
082: removeAllContent();
083:
084: addContent(text.toString(), 0);
085: }
086:
087: /**
088: * This method is designed for content that already is in DOM format, like HTML
089: * messages.
090: */
091: public void addContent(Document content) {
092: Element content_elem = root.createElement("CONTENT");
093: content_elem.setAttribute("quotelevel", "0");
094:
095: /* Find all <BODY> elements and add the child nodes to the content */
096: for (int count = 0; count < 2; count++) {
097: NodeList nl = content.getDocumentElement()
098: .getElementsByTagName(count == 0 ? "BODY" : "body");
099: System.err.println("While parsing HTML content: Found "
100: + nl.getLength() + " body elements");
101: for (int i = 0; i < nl.getLength(); i++) {
102: NodeList nl2 = nl.item(i).getChildNodes();
103: System.err.println("While parsing HTML content: Found "
104: + nl2.getLength() + " child elements");
105: for (int j = 0; j < nl2.getLength(); j++) {
106: System.err.println("Element: " + j);
107: content_elem.appendChild(XMLCommon.importNode(root,
108: nl2.item(j), true));
109: }
110: }
111: }
112:
113: part.appendChild(content_elem);
114:
115: //XMLCommon.debugXML(root);
116: }
117:
118: public void addContent(String content, int quotelevel) {
119: Element content_elem = root.createElement("CONTENT");
120: content_elem.setAttribute("quotelevel", quotelevel + "");
121: XMLCommon.setElementTextValue(content_elem, content, true);
122: part.appendChild(content_elem);
123: }
124:
125: public void insertContent(String content, int quotelevel) {
126: Element content_elem = root.createElement("CONTENT");
127: content_elem.setAttribute("quotelevel", quotelevel + "");
128: XMLCommon.setElementTextValue(content_elem, content, true);
129: Node first = part.getFirstChild();
130: part.insertBefore(content_elem, first);
131: }
132:
133: public void addJavaScript(String content) {
134: Element javascript_elem = root.createElement("JAVASCRIPT");
135: XMLCommon.setElementTextValue(javascript_elem, content, true);
136: part.appendChild(javascript_elem);
137: }
138:
139: public void removeAllContent() {
140: XMLCommon.genericRemoveAll(part, "CONTENT");
141: }
142:
143: public XMLMessagePart createPart(String type) {
144: XMLMessagePart newpart = new XMLMessagePart(root);
145: newpart.setAttribute("type", type);
146: appendPart(newpart);
147: return newpart;
148: }
149:
150: public void insertPart(XMLMessagePart childpart) {
151: Node first = part.getFirstChild();
152: part.insertBefore(childpart.getPartElement(), first);
153: }
154:
155: public void appendPart(XMLMessagePart childpart) {
156: part.appendChild(childpart.getPartElement());
157: }
158:
159: public Enumeration getParts() {
160: // Sucking NodeList needs a Vector to store Elements that will be removed!
161: Vector v = new Vector();
162: NodeList parts = part.getChildNodes();
163: for (int j = 0; j < parts.getLength(); j++) {
164: Element elem = (Element) parts.item(j);
165: if (elem.getTagName().equals("PART")) {
166: v.addElement(new XMLMessagePart(elem));
167: }
168: }
169: return v.elements();
170: }
171:
172: public void removePart(XMLMessagePart childpart) {
173: part.removeChild(childpart.getPartElement());
174: }
175:
176: public void removeAllParts() {
177: XMLCommon.genericRemoveAll(part, "PART");
178: }
179:
180: } // XMLMessagePart
|