2
* xslthl - XSLT Syntax Highlighting
3
* https://sourceforge.net/projects/xslthl/
4
* Copyright (C) 2005-2008 Michal Molhanec, Jirka Kosek, Michiel Hendriks
6
* This software is provided 'as-is', without any express or implied
7
* warranty. In no event will the authors be held liable for any damages
8
* arising from the use of this software.
10
* Permission is granted to anyone to use this software for any purpose,
11
* including commercial applications, and to alter it and redistribute it
12
* freely, subject to the following restrictions:
14
* 1. The origin of this software must not be misrepresented; you must not
15
* claim that you wrote the original software. If you use this software
16
* in a product, an acknowledgment in the product documentation would be
17
* appreciated but is not required.
18
* 2. Altered source versions must be plainly marked as such, and must not be
19
* misrepresented as being the original software.
20
* 3. This notice may not be removed or altered from any source distribution.
22
* Michal Molhanec <mol1111 at users.sourceforge.net>
23
* Jirka Kosek <kosek at users.sourceforge.net>
24
* Michiel Hendriks <elmuerte at users.sourceforge.net>
26
package net.sf.xslthl;
28
import java.util.ArrayList;
29
import java.util.List;
31
import net.sf.saxon.event.Builder;
32
import net.sf.saxon.expr.XPathContext;
33
import net.sf.saxon.om.Axis;
34
import net.sf.saxon.om.AxisIterator;
35
import net.sf.saxon.om.Item;
36
import net.sf.saxon.om.ListIterator;
37
import net.sf.saxon.om.NamePool;
38
import net.sf.saxon.om.NodeInfo;
39
import net.sf.saxon.om.SequenceIterator;
40
import net.sf.saxon.pattern.AnyNodeTest;
41
import net.sf.saxon.type.Type;
44
* A new saxon connector to be used with saxon 8.5 and later.
46
public class ConnectorSaxonB {
48
private static void blockToSaxon6Node(Block b, Builder builder,
49
NamePool pool, Config config) throws Exception {
51
int elemId = pool.allocate(config.prefix, config.uri,
52
((StyledBlock) b).getStyle());
53
builder.startElement(elemId, -1, 0, 0);
54
builder.characters(b.getText(), 0, b.getText().length());
57
builder.characters(b.getText(), 0, b.getText().length());
62
* Highlight the nodes using the standard configuration file
70
public static SequenceIterator highlight(XPathContext context,
71
String hlCode, SequenceIterator nodes) throws Exception {
72
return highlight(context, hlCode, nodes, null);
76
* highlight the nodes using a specific interface
81
* @param configFilename
85
public static SequenceIterator highlight(XPathContext context,
86
String hlCode, SequenceIterator seq, String configFilename)
89
Config c = Config.getInstance(configFilename);
90
MainHighlighter hl = c.getMainHighlighter(hlCode);
92
NamePool pool = context.getController().getNamePool();
94
List<Item> resultNodes = new ArrayList<Item>();
95
while (seq.next() != null) {
96
Item itm = seq.current();
97
if (itm instanceof NodeInfo) {
98
NodeInfo ni = (NodeInfo) itm;
99
SequenceIterator ae = ni.iterateAxis(Axis.CHILD,
100
AnyNodeTest.getInstance());
101
while (ae.next() != null) {
102
Item itm2 = ae.current();
103
if (itm2 instanceof NodeInfo) {
104
NodeInfo n2i = (NodeInfo) itm2;
105
if (n2i.getNodeKind() == Type.TEXT) {
107
Builder builder = context.getController()
110
builder.startDocument(0);
111
List<Block> l = hl.highlight(n2i
114
blockToSaxon6Node(b, builder, pool, c);
116
builder.endDocument();
118
NodeInfo doc = builder.getCurrentRoot();
119
AxisIterator elms = doc.iterateAxis(
120
Axis.CHILD, AnyNodeTest
122
while (elms.next() != null) {
123
resultNodes.add(elms.current());
126
resultNodes.add(n2i);
129
resultNodes.add(n2i);
132
resultNodes.add(itm2);
136
resultNodes.add(itm);
139
return new ListIterator(resultNodes);
140
} catch (Exception e) {