1 | /*
|
---|
2 | * 10/23/2011
|
---|
3 | *
|
---|
4 | * XmlFoldParser.java - Fold parser for XML.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.folding;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 | import javax.swing.text.BadLocationException;
|
---|
14 |
|
---|
15 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
---|
16 | import org.fife.ui.rsyntaxtextarea.Token;
|
---|
17 |
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Fold parser for XML. Any tags that span more than one line, as well as
|
---|
21 | * comment regions spanning more than one line, are identified as foldable
|
---|
22 | * regions.
|
---|
23 | *
|
---|
24 | * @author Robert Futrell
|
---|
25 | * @version 1.0
|
---|
26 | */
|
---|
27 | public class XmlFoldParser implements FoldParser {
|
---|
28 |
|
---|
29 | private static final char[] MARKUP_CLOSING_TAG_START = { '<', '/' };
|
---|
30 | private static final char[] MARKUP_SHORT_TAG_END = { '/', '>' };
|
---|
31 | private static final char[] MLC_END = { '-', '-', '>' };
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * {@inheritDoc}
|
---|
36 | */
|
---|
37 | public List getFolds(RSyntaxTextArea textArea) {
|
---|
38 |
|
---|
39 | List folds = new ArrayList();
|
---|
40 |
|
---|
41 | Fold currentFold = null;
|
---|
42 | int lineCount = textArea.getLineCount();
|
---|
43 | boolean inMLC = false;
|
---|
44 | int mlcStart = 0;
|
---|
45 |
|
---|
46 | try {
|
---|
47 |
|
---|
48 | for (int line=0; line<lineCount; line++) {
|
---|
49 |
|
---|
50 | Token t = textArea.getTokenListForLine(line);
|
---|
51 | while (t!=null && t.isPaintable()) {
|
---|
52 |
|
---|
53 | if (t.isComment()) {
|
---|
54 |
|
---|
55 | // Continuing an MLC from a previous line
|
---|
56 | if (inMLC) {
|
---|
57 | // Found the end of the MLC starting on a previous line...
|
---|
58 | if (t.endsWith(MLC_END)) {
|
---|
59 | int mlcEnd = t.offset + t.textCount - 1;
|
---|
60 | if (currentFold==null) {
|
---|
61 | currentFold = new Fold(FoldType.COMMENT, textArea, mlcStart);
|
---|
62 | currentFold.setEndOffset(mlcEnd);
|
---|
63 | folds.add(currentFold);
|
---|
64 | currentFold = null;
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | currentFold = currentFold.createChild(FoldType.COMMENT, mlcStart);
|
---|
68 | currentFold.setEndOffset(mlcEnd);
|
---|
69 | currentFold = currentFold.getParent();
|
---|
70 | }
|
---|
71 | inMLC = false;
|
---|
72 | mlcStart = 0;
|
---|
73 | }
|
---|
74 | // Otherwise, this MLC is continuing on to yet
|
---|
75 | // another line.
|
---|
76 | }
|
---|
77 |
|
---|
78 | else {
|
---|
79 | // If we're an MLC that ends on a later line...
|
---|
80 | if (t.type==Token.COMMENT_MULTILINE && !t.endsWith(MLC_END)) {
|
---|
81 | inMLC = true;
|
---|
82 | mlcStart = t.offset;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | else if (t.type==Token.MARKUP_TAG_DELIMITER && t.isSingleChar('<')) {
|
---|
89 | if (currentFold==null) {
|
---|
90 | currentFold = new Fold(FoldType.CODE, textArea, t.offset);
|
---|
91 | folds.add(currentFold);
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | currentFold = currentFold.createChild(FoldType.CODE, t.offset);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | else if (t.is(Token.MARKUP_TAG_DELIMITER, MARKUP_SHORT_TAG_END)) {
|
---|
99 | if (currentFold!=null) {
|
---|
100 | Fold parentFold = currentFold.getParent();
|
---|
101 | currentFold.removeFromParent();
|
---|
102 | currentFold = parentFold;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | else if (t.is(Token.MARKUP_TAG_DELIMITER, MARKUP_CLOSING_TAG_START)) {
|
---|
107 | if (currentFold!=null) {
|
---|
108 | currentFold.setEndOffset(t.offset);
|
---|
109 | Fold parentFold = currentFold.getParent();
|
---|
110 | // Don't add fold markers for single-line blocks
|
---|
111 | if (currentFold.isOnSingleLine()) {
|
---|
112 | currentFold.removeFromParent();
|
---|
113 | }
|
---|
114 | currentFold = parentFold;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | t = t.getNextToken();
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 | } catch (BadLocationException ble) { // Should never happen
|
---|
125 | ble.printStackTrace();
|
---|
126 | }
|
---|
127 |
|
---|
128 | return folds;
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | } |
---|