Menu

#10 Add support for XSLT extension to work with latest Saxon EE processor

1.x
closed-fixed
nobody
None
5
2014-10-21
2014-09-12
Radu Coravu
No

The "ConnectorSaxonB" extension no longer compiles with the latest Saxon 9 EE libraries because of API changes made by Saxon.
We can leave it as it is for older Saxon 9 versions and create a new extension called "ConnectorSaxonEE".
It's code should be something like:

        /**
         * A new saxon connector to be used with saxon 8.5 and later.
         */
        public class ConnectorSaxonEE {

            private static void blockToSaxon6Node(Block b, Builder builder,
                    NamePool pool, Config config) throws Exception {
                if (b.isStyled()) {
                    // int elemId = pool.allocate(config.prefix, config.uri,
                    // ((StyledBlock) b).getStyle());

                    builder.startElement(new FingerprintedQName(config.prefix,
                            config.uri, ((StyledBlock) b).getStyle()), AnyType
                            .getInstance(), 0, 0);
                    System.err.println("CREATED " + config.prefix + ":"
                            + ((StyledBlock) b).getStyle() + " in NS "
                            + ((StyledBlock) b).getStyle());
                    // builder.startElement(elemId, null, 0, 0);
                    builder.characters(b.getText(), 0, b.getText().length());
                    builder.endElement();
                } else {
                    builder.characters(b.getText(), 0, b.getText().length());
                }
            }

            /**
             * Highlight the nodes using the standard configuration file
             * 
             * @param context
             * @param hlCode
             * @param nodes
             * @return
             * @throws Exception
             */
            public static SequenceIterator highlight(XPathContext context,
                    String hlCode, SequenceIterator nodes) throws Exception {
                return highlight(context, hlCode, nodes, null);
            }

            /**
             * highlight the nodes using a specific interface
             * 
             * @param context
             * @param hlCode
             * @param seq
             * @param configFilename
             * @return
             * @throws Exception
             */
            public static SequenceIterator highlight(XPathContext context,
                    String hlCode, SequenceIterator seq, String configFilename)
                    throws Exception {
                try {
                    Config c = Config.getInstance(configFilename);
                    MainHighlighter hl = c.getMainHighlighter(hlCode);

                    NamePool pool = context.getController().getNamePool();

                    List<Item> resultNodes = new ArrayList<Item>();
                    while (seq.next() != null) {
                        Item itm = seq.current();
                        if (itm instanceof NodeInfo) {
                            NodeInfo ni = (NodeInfo) itm;
                            SequenceIterator ae = ni.iterateAxis(
                                    net.sf.saxon.om.AxisInfo.CHILD,
                                    net.sf.saxon.pattern.AnyNodeTest.getInstance());
                            while (ae.next() != null) {
                                Item itm2 = ae.current();
                                if (itm2 instanceof NodeInfo) {
                                    NodeInfo n2i = (NodeInfo) itm2;
                                    if (n2i.getNodeKind() == Type.TEXT) {
                                        if (hl != null) {
                                            Builder builder = context.getController()
                                                    .makeBuilder();
                                            builder.open();
                                            builder.startDocument(0);
                                            System.err.println("TEXT IS "
                                                    + n2i.getStringValue());
                                            List<Block> l = hl.highlight(n2i
                                                    .getStringValue());
                                            for (Block b : l) {
                                                System.err.println("BLOCK IS " + b.text
                                                        + " styled " + b.isStyled());
                                                blockToSaxon6Node(b, builder, pool, c);
                                            }
                                            builder.endDocument();
                                            builder.close();
                                            NodeInfo doc = builder.getCurrentRoot();
                                            AxisIterator elms = doc.iterateAxis(
                                                    net.sf.saxon.om.AxisInfo.CHILD,
                                                    net.sf.saxon.pattern.AnyNodeTest
                                                            .getInstance());
                                            while (elms.next() != null) {
                                                resultNodes.add(elms.current());
                                            }
                                        } else {
                                            resultNodes.add(n2i);
                                        }
                                    } else {
                                        resultNodes.add(n2i);
                                    }
                                } else {
                                    resultNodes.add(itm2);
                                }
                            }
                        } else {
                            resultNodes.add(itm);
                        }
                    }
                    return new ListIterator(resultNodes);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

One problem I see is that it's hard to compile both extensions in a single JAR because they need different Saxon libraries.

Discussion

  • Radu Coravu

    Radu Coravu - 2014-09-22

    Committed Saxon EE connector.

     
  • Radu Coravu

    Radu Coravu - 2014-09-22
    • status: open --> closed-fixed
     

Log in to post a comment.