~ubuntu-branches/ubuntu/wily/xslthl/wily

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/xslthl/ConnectorSaxonB.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2013-05-16 08:59:07 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130516085907-ujk9f81x8w344ch1
Tags: 2.1.0-2
Upload to sid

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * xslthl - XSLT Syntax Highlighting
 
3
 * https://sourceforge.net/projects/xslthl/
 
4
 * Copyright (C) 2005-2008 Michal Molhanec, Jirka Kosek, Michiel Hendriks
 
5
 * 
 
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.
 
9
 * 
 
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:
 
13
 * 
 
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.
 
21
 * 
 
22
 * Michal Molhanec <mol1111 at users.sourceforge.net>
 
23
 * Jirka Kosek <kosek at users.sourceforge.net>
 
24
 * Michiel Hendriks <elmuerte at users.sourceforge.net>
 
25
 */
 
26
package net.sf.xslthl;
 
27
 
 
28
import java.util.ArrayList;
 
29
import java.util.List;
 
30
 
 
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;
 
42
 
 
43
/**
 
44
 * A new saxon connector to be used with saxon 8.5 and later.
 
45
 */
 
46
public class ConnectorSaxonB {
 
47
 
 
48
        private static void blockToSaxon6Node(Block b, Builder builder,
 
49
                NamePool pool, Config config) throws Exception {
 
50
                if (b.isStyled()) {
 
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());
 
55
                        builder.endElement();
 
56
                } else {
 
57
                        builder.characters(b.getText(), 0, b.getText().length());
 
58
                }
 
59
        }
 
60
 
 
61
        /**
 
62
         * Highlight the nodes using the standard configuration file
 
63
         * 
 
64
         * @param context
 
65
         * @param hlCode
 
66
         * @param nodes
 
67
         * @return
 
68
         * @throws Exception
 
69
         */
 
70
        public static SequenceIterator highlight(XPathContext context,
 
71
                String hlCode, SequenceIterator nodes) throws Exception {
 
72
                return highlight(context, hlCode, nodes, null);
 
73
        }
 
74
 
 
75
        /**
 
76
         * highlight the nodes using a specific interface
 
77
         * 
 
78
         * @param context
 
79
         * @param hlCode
 
80
         * @param seq
 
81
         * @param configFilename
 
82
         * @return
 
83
         * @throws Exception
 
84
         */
 
85
        public static SequenceIterator highlight(XPathContext context,
 
86
                String hlCode, SequenceIterator seq, String configFilename)
 
87
                throws Exception {
 
88
                try {
 
89
                        Config c = Config.getInstance(configFilename);
 
90
                        MainHighlighter hl = c.getMainHighlighter(hlCode);
 
91
 
 
92
                        NamePool pool = context.getController().getNamePool();
 
93
 
 
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) {
 
106
                                                                if (hl != null) {
 
107
                                                                        Builder builder = context.getController()
 
108
                                                                                .makeBuilder();
 
109
                                                                        builder.open();
 
110
                                                                        builder.startDocument(0);
 
111
                                                                        List<Block> l = hl.highlight(n2i
 
112
                                                                                .getStringValue());
 
113
                                                                        for (Block b : l) {
 
114
                                                                                blockToSaxon6Node(b, builder, pool, c);
 
115
                                                                        }
 
116
                                                                        builder.endDocument();
 
117
                                                                        builder.close();
 
118
                                                                        NodeInfo doc = builder.getCurrentRoot();
 
119
                                                                        AxisIterator elms = doc.iterateAxis(
 
120
                                                                                Axis.CHILD, AnyNodeTest
 
121
                                                                                        .getInstance());
 
122
                                                                        while (elms.next() != null) {
 
123
                                                                                resultNodes.add(elms.current());
 
124
                                                                        }
 
125
                                                                } else {
 
126
                                                                        resultNodes.add(n2i);
 
127
                                                                }
 
128
                                                        } else {
 
129
                                                                resultNodes.add(n2i);
 
130
                                                        }
 
131
                                                } else {
 
132
                                                        resultNodes.add(itm2);
 
133
                                                }
 
134
                                        }
 
135
                                } else {
 
136
                                        resultNodes.add(itm);
 
137
                                }
 
138
                        }
 
139
                        return new ListIterator(resultNodes);
 
140
                } catch (Exception e) {
 
141
                        e.printStackTrace();
 
142
                        return null;
 
143
                }
 
144
        }
 
145
}