home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.text.html;
-
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Enumeration;
- import java.util.NoSuchElementException;
- import java.util.Stack;
- import java.util.StringTokenizer;
- import java.util.Vector;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.DefaultListModel;
- import javax.swing.text.AbstractDocument;
- import javax.swing.text.AbstractWriter;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Document;
- import javax.swing.text.Element;
- import javax.swing.text.ElementIterator;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.html.HTML.Attribute;
- import javax.swing.text.html.HTML.Tag;
-
- public class HTMLWriter extends AbstractWriter {
- private Stack blockElementStack = new Stack();
- private boolean inContent = false;
- private boolean inPre = false;
- private boolean inTextArea = false;
- private boolean newlineOutputed = false;
- private boolean completeDoc;
- private Vector tags = new Vector(10);
- private boolean indentNext = false;
-
- public HTMLWriter(Writer var1, HTMLDocument var2) {
- super(var1, var2);
- this.completeDoc = true;
- }
-
- public HTMLWriter(Writer var1, HTMLDocument var2, int var3, int var4) {
- super(var1, var2, var3, var4);
- this.completeDoc = var3 == 0 && var4 == ((AbstractDocument)var2).getLength();
- }
-
- protected void closeOutUnwantedEmbeddedTags(AttributeSet var1) throws IOException {
- for(int var3 = this.tags.size() - 1; var3 >= 0; --var3) {
- HTML.Tag var2 = (HTML.Tag)this.tags.elementAt(var3);
- if (var1 == null || this.noMatchForTagInAttributes(var1, var2)) {
- this.tags.removeElementAt(var3);
- ((AbstractWriter)this).write('<');
- ((AbstractWriter)this).write('/');
- this.write(var2.toString());
- ((AbstractWriter)this).write('>');
- }
- }
-
- }
-
- protected void comment(Element var1) throws BadLocationException, IOException {
- AttributeSet var2 = var1.getAttributes();
- if (this.matchNameAttribute(var2, Tag.COMMENT)) {
- Object var3 = var2.getAttribute(Attribute.COMMENT);
- if (var3 instanceof String) {
- this.writeComment((String)var3);
- } else {
- this.writeComment((String)null);
- }
- }
-
- }
-
- protected void emptyTag(Element var1) throws BadLocationException, IOException {
- if (!this.inContent && !this.inPre) {
- ((AbstractWriter)this).indent();
- }
-
- AttributeSet var2 = var1.getAttributes();
- this.closeOutUnwantedEmbeddedTags(var2);
- this.writeEmbeddedTags(var2);
- if (this.matchNameAttribute(var2, Tag.CONTENT)) {
- this.inContent = true;
- this.text(var1);
- } else if (this.matchNameAttribute(var2, Tag.COMMENT)) {
- this.comment(var1);
- } else {
- boolean var3 = this.isBlockTag(var1.getAttributes());
- if (this.inContent && var3) {
- ((AbstractWriter)this).write('\n');
- ((AbstractWriter)this).indent();
- }
-
- ((AbstractWriter)this).write('<');
- Object var4 = var2 != null ? var2.getAttribute(StyleConstants.NameAttribute) : null;
- Object var5 = var2 != null ? var2.getAttribute(Attribute.ENDTAG) : null;
- boolean var6 = false;
- if (var4 != null && var5 != null && var5 instanceof String && ((String)var5).equals("true")) {
- var6 = true;
- ((AbstractWriter)this).write('/');
- }
-
- this.write(var1.getName());
- this.writeAttributes(var2);
- ((AbstractWriter)this).write('>');
- if (this.matchNameAttribute(var2, Tag.TITLE) && !var6) {
- Document var7 = var1.getDocument();
- String var8 = (String)var7.getProperty("title");
- this.write(var8);
- } else if (!this.inContent || var3) {
- ((AbstractWriter)this).write('\n');
- if (var3 && this.inContent) {
- ((AbstractWriter)this).indent();
- }
- }
- }
-
- }
-
- protected void endTag(Element var1) throws IOException {
- if (!this.synthesizedElement(var1)) {
- if (this.matchNameAttribute(var1.getAttributes(), Tag.PRE)) {
- this.inPre = false;
- }
-
- this.closeOutUnwantedEmbeddedTags(var1.getAttributes());
- if (this.inContent) {
- if (!this.newlineOutputed) {
- ((AbstractWriter)this).write('\n');
- }
-
- this.newlineOutputed = false;
- this.inContent = false;
- }
-
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write('<');
- ((AbstractWriter)this).write('/');
- this.write(var1.getName());
- ((AbstractWriter)this).write('>');
- ((AbstractWriter)this).write('\n');
- }
- }
-
- private boolean indentNeedsIncrementing(Element var1, Element var2) {
- if (var2.getParentElement() == var1 && !this.inPre) {
- if (this.indentNext) {
- this.indentNext = false;
- return true;
- }
-
- if (this.synthesizedElement(var2)) {
- this.indentNext = true;
- } else if (!this.synthesizedElement(var1)) {
- return true;
- }
- }
-
- return false;
- }
-
- protected boolean isBlockTag(AttributeSet var1) {
- Object var2 = var1.getAttribute(StyleConstants.NameAttribute);
- if (var2 instanceof HTML.Tag) {
- HTML.Tag var3 = (HTML.Tag)var2;
- return var3.isBlock();
- } else {
- return false;
- }
- }
-
- private boolean isFormElementWithContent(AttributeSet var1) {
- return this.matchNameAttribute(var1, Tag.TEXTAREA) || this.matchNameAttribute(var1, Tag.SELECT);
- }
-
- protected boolean matchNameAttribute(AttributeSet var1, HTML.Tag var2) {
- Object var3 = var1.getAttribute(StyleConstants.NameAttribute);
- if (var3 instanceof HTML.Tag) {
- HTML.Tag var4 = (HTML.Tag)var3;
- if (var4 == var2) {
- return true;
- }
- }
-
- return false;
- }
-
- private boolean noMatchForTagInAttributes(AttributeSet var1, HTML.Tag var2) {
- if (var1 != null) {
- Enumeration var3 = var1.getAttributeNames();
-
- while(var3.hasMoreElements()) {
- Object var4 = var3.nextElement();
- if (var4 instanceof HTML.Tag && var4 == var2) {
- return false;
- }
- }
- }
-
- return true;
- }
-
- protected void selectContent(AttributeSet var1) throws IOException {
- Object var2 = var1.getAttribute(StyleConstants.ModelAttribute);
- ((AbstractWriter)this).incrIndent();
- if (var2 instanceof OptionListModel) {
- OptionListModel var3 = (OptionListModel)var2;
- int var4 = ((DefaultListModel)var3).getSize();
-
- for(int var5 = 0; var5 < var4; ++var5) {
- Option var6 = (Option)((DefaultListModel)var3).getElementAt(var5);
- this.writeOption(var6);
- }
- } else if (var2 instanceof OptionComboBoxModel) {
- OptionComboBoxModel var7 = (OptionComboBoxModel)var2;
- int var8 = ((DefaultComboBoxModel)var7).getSize();
-
- for(int var9 = 0; var9 < var8; ++var9) {
- Option var10 = (Option)((DefaultComboBoxModel)var7).getElementAt(var9);
- this.writeOption(var10);
- }
- }
-
- ((AbstractWriter)this).decrIndent();
- }
-
- protected void startTag(Element var1) throws IOException, BadLocationException {
- if (!this.synthesizedElement(var1)) {
- if (this.matchNameAttribute(var1.getAttributes(), Tag.PRE)) {
- this.inPre = true;
- }
-
- AttributeSet var2 = var1.getAttributes();
- this.closeOutUnwantedEmbeddedTags(var2);
- if (this.inContent) {
- ((AbstractWriter)this).write('\n');
- this.inContent = false;
- this.newlineOutputed = false;
- }
-
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write('<');
- this.write(var1.getName());
- this.writeAttributes(var2);
- ((AbstractWriter)this).write('>');
- ((AbstractWriter)this).write('\n');
- if (this.matchNameAttribute(var1.getAttributes(), Tag.TEXTAREA)) {
- this.textAreaContent(var1.getAttributes());
- } else if (this.matchNameAttribute(var1.getAttributes(), Tag.SELECT)) {
- this.selectContent(var1.getAttributes());
- }
-
- }
- }
-
- protected boolean synthesizedElement(Element var1) {
- return this.matchNameAttribute(var1.getAttributes(), Tag.IMPLIED);
- }
-
- protected void text(Element var1) throws BadLocationException, IOException {
- String var2 = ((AbstractWriter)this).getText(var1);
- this.newlineOutputed = false;
- if (var2.length() > 0) {
- this.write(var2);
- if (var2.endsWith("\n")) {
- this.newlineOutputed = true;
- }
- }
-
- }
-
- protected void textAreaContent(AttributeSet var1) throws BadLocationException, IOException {
- Document var2 = (Document)var1.getAttribute(StyleConstants.ModelAttribute);
- if (var2 != null && var2.getLength() > 0) {
- String var3 = var2.getText(0, var2.getLength());
- if (var3 != null) {
- this.inTextArea = true;
- ((AbstractWriter)this).incrIndent();
- ((AbstractWriter)this).indent();
- this.write(var3);
- ((AbstractWriter)this).write('\n');
- this.inTextArea = false;
- ((AbstractWriter)this).decrIndent();
- }
- }
-
- }
-
- public void write() throws IOException, BadLocationException {
- ElementIterator var1 = ((AbstractWriter)this).getElementIterator();
- Element var2 = null;
- Object var3 = null;
-
- while((var6 = var1.next()) != null) {
- if (((AbstractWriter)this).inRange(var6)) {
- if (var2 != null) {
- if (this.indentNeedsIncrementing(var2, var6)) {
- ((AbstractWriter)this).incrIndent();
- } else if (var2.getParentElement() != var6.getParentElement()) {
- for(Element var7 = (Element)this.blockElementStack.peek(); var7 != var6.getParentElement(); var7 = (Element)this.blockElementStack.peek()) {
- this.blockElementStack.pop();
- if (!this.synthesizedElement(var7)) {
- if (!this.matchNameAttribute(var7.getAttributes(), Tag.PRE)) {
- ((AbstractWriter)this).decrIndent();
- }
-
- this.endTag(var7);
- }
- }
- } else if (var2.getParentElement() == var6.getParentElement()) {
- Element var4 = (Element)this.blockElementStack.peek();
- if (var4 == var2) {
- this.blockElementStack.pop();
- this.endTag(var4);
- }
- }
- }
-
- if (var6.isLeaf() && !this.isFormElementWithContent(var6.getAttributes())) {
- this.emptyTag(var6);
- } else {
- this.blockElementStack.push(var6);
- this.startTag(var6);
- }
-
- var2 = var6;
- }
- }
-
- this.closeOutUnwantedEmbeddedTags((AttributeSet)null);
-
- while(!this.blockElementStack.empty()) {
- var2 = (Element)this.blockElementStack.pop();
- if (!this.synthesizedElement(var2)) {
- if (!this.matchNameAttribute(var2.getAttributes(), Tag.PRE)) {
- ((AbstractWriter)this).decrIndent();
- }
-
- this.endTag(var2);
- }
- }
-
- if (this.completeDoc) {
- this.writeAdditionalComments();
- }
-
- }
-
- protected void write(String var1) throws IOException {
- if (this.inTextArea) {
- StringTokenizer var2 = new StringTokenizer(var1, "\n");
-
- try {
- while(var2.hasMoreTokens()) {
- super.write(var2.nextToken());
- if (var2.countTokens() != 0) {
- ((AbstractWriter)this).write('\n');
- ((AbstractWriter)this).indent();
- }
- }
- } catch (NoSuchElementException var3) {
- }
- } else {
- super.write(var1);
- }
-
- }
-
- void writeAdditionalComments() throws IOException {
- Object var1 = ((AbstractWriter)this).getDocument().getProperty("AdditionalComments");
- if (var1 instanceof Vector) {
- Vector var2 = (Vector)var1;
- int var3 = 0;
-
- for(int var4 = var2.size(); var3 < var4; ++var3) {
- this.writeComment(var2.elementAt(var3).toString());
- }
- }
-
- }
-
- protected void writeAttributes(AttributeSet var1) throws IOException {
- Enumeration var2 = var1.getAttributeNames();
-
- while(var2.hasMoreElements()) {
- Object var3 = var2.nextElement();
- if (!(var3 instanceof HTML.Tag) && var3 != StyleConstants.NameAttribute && var3 != Attribute.ENDTAG && var3 != StyleConstants.ModelAttribute) {
- this.write(" " + var3 + "=" + var1.getAttribute(var3));
- }
- }
-
- }
-
- void writeComment(String var1) throws IOException {
- ((AbstractWriter)this).write('<');
- ((AbstractWriter)this).write('!');
- ((AbstractWriter)this).write('-');
- ((AbstractWriter)this).write('-');
- if (var1 != null) {
- this.write(var1);
- }
-
- ((AbstractWriter)this).write('-');
- ((AbstractWriter)this).write('-');
- ((AbstractWriter)this).write('>');
- ((AbstractWriter)this).write('\n');
- }
-
- protected void writeEmbeddedTags(AttributeSet var1) throws IOException {
- Enumeration var2 = var1.getAttributeNames();
-
- while(var2.hasMoreElements()) {
- Object var3 = var2.nextElement();
- if (var3 instanceof HTML.Tag) {
- HTML.Tag var4 = (HTML.Tag)var3;
- if (var4 != Tag.FORM && !this.tags.contains(var4)) {
- ((AbstractWriter)this).write('<');
- this.write(var4.toString());
- Object var5 = var1.getAttribute(var4);
- if (var5 != null && var5 instanceof AttributeSet) {
- this.writeAttributes((AttributeSet)var5);
- }
-
- ((AbstractWriter)this).write('>');
- this.tags.addElement(var4);
- }
- }
- }
-
- }
-
- protected void writeOption(Option var1) throws IOException {
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write('<');
- this.write("option ");
- if (var1.getValue() != null) {
- this.write("value=" + var1.getValue());
- }
-
- if (var1.isSelected()) {
- this.write(" selected");
- }
-
- ((AbstractWriter)this).write('>');
- if (var1.getLabel() != null) {
- this.write(var1.getLabel());
- }
-
- ((AbstractWriter)this).write('\n');
- }
- }
-