File: view-source.xml

package info (click to toggle)
cocoon 1.8-1
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 12,016 kB
  • ctags: 3,793
  • sloc: xml: 16,682; java: 8,089; sh: 174; makefile: 61
file content (269 lines) | stat: -rw-r--r-- 9,159 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?xml version="1.0"?>
<?cocoon-process type="xsp"?>

<!-- Written by Ricardo Rocha <rrocha@plenix.org> -->

<!-- A quick'n'dirty XML colorizer -->

<xsp:page language="java" xmlns:xsp="http://www.apache.org/1999/XSP/Core">

  <xsp:structure>
   <xsp:include>java.net.*</xsp:include>
  </xsp:structure>
  
  <xsp:logic><![CDATA[
    private static final String ATTR_NAME_COLOR = "darkblue";
    private static final String ATTR_VALUE_COLOR = "navy";
    private static final String COMMENT_COLOR = "gray";
    private static final String DELIMITER_COLOR = "blue";
    private static final String ELEMENT_COLOR = "navy";
    private static final String ENTITY_REF_COLOR = "navy";
    private static final String PI_COLOR = "darkred";
    private static final String TEXT_COLOR = "black";
    private static final String CUSTOM_ELEMENT_COLOR = "green";
    private static final String XSL_ELEMENT_COLOR = "purple";
    private static final String XSP_ELEMENT_COLOR = "green";
    private static final String XSP_TEXT_COLOR = "red";

    protected Element colorize(Node node, Document factory) {
      Element element = factory.createElement("pre");
      DocumentFragment fragment = factory.createDocumentFragment();
      element.appendChild(doColorize(node, factory, 0, TEXT_COLOR));
      return element;
    }

    protected static DocumentFragment
      doColorize(Node node, Document factory, int level, String textColor)
    {
      Element result = null;
      DocumentFragment fragment = factory.createDocumentFragment();

      switch (node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          result.appendChild(factory.createTextNode("<![CDATA["));
          fragment.appendChild(result);

          result = factory.createElement("font");
          result.setAttribute("color", textColor);
          result.appendChild(factory.createTextNode(node.getNodeValue()));
          fragment.appendChild(result);

          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          // I know the thing below is ugly, but man, nexted CDATA are a pain in the ass!
          result.appendChild(factory.createTextNode("]]]]>&gt;<![CDATA["));
          fragment.appendChild(result);

          break;
        case Node.ELEMENT_NODE: {
          Element element = (Element) node;
  
          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          result.appendChild(factory.createTextNode("<"));
          fragment.appendChild(result);

          String tagColor = ELEMENT_COLOR;
          String tagName = element.getTagName();

          if (tagName.startsWith("xsp:")) {
            tagColor = XSP_ELEMENT_COLOR;
          } else if (tagName.startsWith("xsl:")) {
            tagColor = XSL_ELEMENT_COLOR;
          } else if (tagName.indexOf(':') >= 0) {
            tagColor = CUSTOM_ELEMENT_COLOR;
          }

          result = factory.createElement("font");
          result.setAttribute("color", tagColor);
          result.appendChild(factory.createTextNode(tagName));
          fragment.appendChild(result);

          NamedNodeMap attributes = element.getAttributes();
          int attributeCount = attributes.getLength();
  
          for (int i = 0; i < attributeCount; i++) {
            Attr attribute = (Attr) attributes.item(i);
  
            result = factory.createElement("font");
            result.setAttribute("color", ATTR_NAME_COLOR);
            result.appendChild(
              factory.createTextNode(" " + attribute.getName() + "=")
            );
            fragment.appendChild(result);

            result = factory.createElement("font");
            result.setAttribute("color", ATTR_VALUE_COLOR);
            result.appendChild(
              factory.createTextNode("\"" + attribute.getValue() + "\"")
            );
            fragment.appendChild(result);
          }
  
          NodeList nodeList = element.getChildNodes();
          int childCount = nodeList.getLength();
  
          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          result.appendChild(
            factory.createTextNode((childCount == 0 ? "/" : "") + ">")
          );
          fragment.appendChild(result);

          if (tagName.startsWith("xsp:")) {
            textColor = XSP_TEXT_COLOR;
          } else {
            textColor = TEXT_COLOR;
          }
            
          result = factory.createElement("font");
          result.setAttribute("color", textColor);

          for (int i = 0; i < childCount; i++) {
            result.appendChild(
              doColorize(nodeList.item(i), factory, level + 1, textColor)
            );
          }
          fragment.appendChild(result);

          if (childCount > 0) {
            result = factory.createElement("font");
            result.setAttribute("color", DELIMITER_COLOR);
            result.appendChild(factory.createTextNode("</"));
            fragment.appendChild(result);

            result = factory.createElement("font");
            result.setAttribute("color", tagColor);
            result.appendChild(factory.createTextNode(tagName));
            fragment.appendChild(result);

            result = factory.createElement("font");
            result.setAttribute("color", DELIMITER_COLOR);
            result.appendChild(factory.createTextNode(">"));
            fragment.appendChild(result);
          }
  
          break;
        }
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE: {
          NodeList nodeList = node.getChildNodes();
          int childCount = nodeList.getLength();
  
          for (int i = 0; i < childCount; i++) {
            fragment.appendChild(
              doColorize(nodeList.item(i), factory, level + 1, textColor)
            );
          }
  
          break;
        }
        case Node.COMMENT_NODE:
          result = factory.createElement("font");
          result.setAttribute("color", COMMENT_COLOR);

          result.appendChild(
            factory.createTextNode(
              "<!-- " + node.getNodeValue() + " -->\n"
            )
          );

          fragment.appendChild(result);

          break;
        case Node.PROCESSING_INSTRUCTION_NODE:
          ProcessingInstruction pi = (ProcessingInstruction) node;

          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          result.appendChild(factory.createTextNode("<?"));
          fragment.appendChild(result);
  
          result = factory.createElement("font");
          result.setAttribute("color", PI_COLOR);
          result.appendChild(factory.createTextNode(pi.getTarget() + " " + pi.getData()));
          fragment.appendChild(result);
  
          result = factory.createElement("font");
          result.setAttribute("color", DELIMITER_COLOR);
          result.appendChild(factory.createTextNode("?>\n"));
          fragment.appendChild(result);

          break;
        case Node.ENTITY_REFERENCE_NODE:
          result = factory.createElement("font");
          result.setAttribute("color", ENTITY_REF_COLOR);
          result.appendChild(
            factory.createTextNode("<" + node.getNodeValue() + ";")
          );
          fragment.appendChild(result);

          break;
        case Node.TEXT_NODE:
          fragment.appendChild(factory.createTextNode(node.getNodeValue()));
          break;
        default:
          break;
      }

      return fragment;
    }
  ]]></xsp:logic>

  <html>
    <head>
      <title>Source Code</title>
    </head>

    <body>
      <xsp:logic>
        String filename = request.getParameter("filename");
        String resourcename = request.getParameter("url");
        
        if (filename != null) {
          String filepath = XSPUtil.relativeFilename(filename, request, (ServletContext) context);

          <h3 style="color:navy; text-align: center">
            <xsp:expr>filename</xsp:expr>
          </h3>
      
          <xsp:content>
           <xsp:expr>
            this.colorize(
              this.xspParser.parse(
                new InputSource(
                  new FileReader(filepath)
                )
              ), document
            )
           </xsp:expr>
          </xsp:content>
        } else if (resourcename != null) {
          URL resource = new URL(resourcename);
          
          <h3 style="color:navy; text-align: center">
            <xsp:expr>resourcename</xsp:expr>
          </h3>
      
          <xsp:content>
           <xsp:expr>
            this.colorize(
              this.xspParser.parse(
                new InputSource(
                  resource.openStream()
                )
              ), document
            )
           </xsp:expr>
          </xsp:content>
        } else {
          <h3 style="color:navy; text-align: center">
            Need <em>filename</em> or <em>url</em> parameters to work
          </h3>
        }
      </xsp:logic>
    </body>
  </html>
</xsp:page>