File: Indenter.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (112 lines) | stat: -rw-r--r-- 3,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.jclark.xsl.sax;

import org.xml.sax.*;
import java.io.OutputStream;
import java.io.IOException;

public class Indenter implements OutputDocumentHandler, CommentHandler, RawCharactersHandler {
  private DocumentHandler documentHandler = null;
  private CommentHandler commentHandler = null;
  private RawCharactersHandler rawCharactersHandler;
  private char[] newline = new char[]{'\n'};

  static private final byte IN_PCDATA_CHUNK = 0;
  static private final byte JUST_HAD_START_TAG = 1;
  static private final byte OTHER = 2;

  private byte state = IN_PCDATA_CHUNK;

  public Indenter(DocumentHandler handler,
		  RawCharactersHandler rawCharactersHandler) {
    this.documentHandler = handler;
    if (handler instanceof CommentHandler)
      this.commentHandler =(CommentHandler)handler;
    else
      this.commentHandler = null;
    this.rawCharactersHandler = rawCharactersHandler;
  }

  public DocumentHandler init(Destination dest, AttributeList atts)
    throws IOException, SAXException {
    if (documentHandler instanceof OutputDocumentHandler)
      documentHandler = ((OutputDocumentHandler)documentHandler).init(dest, atts);
    return this;
  }

  public void setDocumentLocator(Locator locator) {
    documentHandler.setDocumentLocator(locator);
  }

  public void startDocument()
    throws SAXException {
    documentHandler.startDocument();
  }

  public void endDocument()
    throws SAXException {
    maybeNewline();
    documentHandler.endDocument();
  }

  public void startElement(String name, AttributeList atts)
    throws SAXException {
    maybeNewline();
    state = JUST_HAD_START_TAG;
    documentHandler.startElement(name, atts);
  }

  public void endElement(String name)
    throws SAXException {
    if (state == JUST_HAD_START_TAG)
      state = OTHER;
    else
      maybeNewline();
    documentHandler.endElement(name);
  }

  public void characters(char ch[], int start, int length)
    throws SAXException {
    if (length > 0) {
      documentHandler.characters(ch, start, length);
      state = IN_PCDATA_CHUNK;
    }
  }

  public void ignorableWhitespace(char ch[], int start, int length)
    throws SAXException {
    if (length > 0) {
      documentHandler.ignorableWhitespace(ch, start, length);
      state = IN_PCDATA_CHUNK;
    }
    
  }

  public void rawCharacters(String chars) throws SAXException {
    if (chars.length() > 0) {
      rawCharactersHandler.rawCharacters(chars);
      state = IN_PCDATA_CHUNK;
    }
  }

  private final void maybeNewline() throws SAXException {
    if (state != IN_PCDATA_CHUNK) {
      documentHandler.characters(newline, 0, 1);
      newline[0] = '\n';
    }
    state = OTHER;
  }

  public void processingInstruction(String target, String data)
    throws SAXException {
    maybeNewline();
    documentHandler.processingInstruction(target, data);
  }

  public void comment(String contents)
    throws SAXException {
    if (commentHandler != null) {
      maybeNewline();
      commentHandler.comment(contents);
    }
  }
}