001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.collab.util;
043:
044: import java.io.*;
045: import java.net.*;
046: import java.util.*;
047: import javax.swing.text.*;
048: import javax.swing.text.html.*;
049:
050: /**
051: *
052: */
053: class TextConverterCallback extends HTMLEditorKit.ParserCallback {
054: String text = "";
055: private StringBuffer buffer = new StringBuffer();
056: private String link;
057:
058: public TextConverterCallback() {
059: }
060:
061: public void flush() {
062: text = buffer.toString();
063: buffer = new StringBuffer();
064: }
065:
066: public void handleText(char[] data, int pos) {
067: if (link == null) { //we are not inside a link tag
068: if (buffer.equals(""))
069: buffer.append(" ");
070: buffer.append(data);
071: } else {
072: String linkprompt = new String(data);
073: if (linkprompt.equals(link))
074: link = null;
075: buffer.append(linkprompt);
076: }
077: }
078:
079: public void handleComment(char[] data, int pos) {
080:
081: }
082:
083: public void handleStartTag(HTML.Tag t, MutableAttributeSet a,
084: int pos) {
085: if (t == HTML.Tag.P)
086: buffer.append("\r\n");
087: else if (t == HTML.Tag.A) {
088: link = (String) a.getAttribute(HTML.Attribute.HREF);
089: }
090: }
091:
092: public void handleEndTag(HTML.Tag t, int pos) {
093: if (t == HTML.Tag.H1 || t == HTML.Tag.H2 || t == HTML.Tag.H3
094: || t == HTML.Tag.H4 || t == HTML.Tag.H5
095: || t == HTML.Tag.H6) {
096: buffer.append("\r\n");
097: }
098: if (t == HTML.Tag.A) {
099: if (link != null) {
100: buffer.append("[");
101: buffer.append(link);
102: buffer.append("]");
103: link = null;
104: }
105: }
106:
107: }
108:
109: public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a,
110: int pos) {
111: if (t == HTML.Tag.BR)
112: buffer.append("\r\n");
113: }
114:
115: public void handleError(String errorMsg, int pos) {
116: /*buffer = new StringBuffer("Error parsing HTML\r\n");
117: buffer.append(errorMsg);*/
118:
119: }
120: }
121:
122: public class HTMLConverter extends HTMLDocumentLoader {
123:
124: HTMLEditorKit.ParserCallback c;
125:
126: public String convertToText(String html) {
127: c = new TextConverterCallback();
128: try {
129: loadDocument(html);
130: return ((TextConverterCallback) c).text;
131: } catch (Exception e) {
132: return "Error Parsing HTML";
133: }
134: }
135:
136: public synchronized HTMLEditorKit.ParserCallback getParserCallback(
137: HTMLDocument doc) {
138: return c;
139: }
140:
141: }
|