/** * Simple test program * Usage: java StaxBridge in.xml [out.xml] * @param args command line arguments */ public static void main(String[] args) throws Exception { for (int i=0; i<1; i++) { long startTime = System.currentTimeMillis(); PipelineConfiguration pipe = new Configuration().makePipelineConfiguration(); StaxBridge puller = new StaxBridge(); File f = new File(args[0]); puller.setInputStream(f.toURI().toString(), new FileInputStream(f)); XMLEmitter emitter = new XMLEmitter(); emitter.setPipelineConfiguration(pipe); emitter.setOutputProperties(new Properties()); if (args.length > 1) { emitter.setOutputStream(new FileOutputStream(args[1])); } else { emitter.setOutputStream(System.out); } NamespaceReducer r = new NamespaceReducer(); r.setUnderlyingReceiver(emitter); puller.setPipelineConfiguration(pipe); r.setPipelineConfiguration(pipe); new PullPushCopier(puller, r).copy(); System.err.println("Elapsed time: " + (System.currentTimeMillis() - startTime) + "ms"); } } }
/** * Output a namespace node (binding) * @param namespaceCode encapsulates the prefix and URI * @param properties the properties of the namespace binding * @throws XPathException */ public void namespace(int namespaceCode, int properties) throws XPathException { // Keep the namespace only if it is actually needed if (isNeeded(namespaceCode)) { addToStack(namespaceCode); countStack[depth - 1]++; nextReceiver.namespace(namespaceCode, properties); } }
/** * startContent: Add any namespace undeclarations needed to stop * namespaces being inherited from parent elements */ public void startContent() throws XPathException { if (pendingUndeclarations != null) { for (int i=0; i<pendingUndeclarations.length; i++) { int nscode = pendingUndeclarations[i]; if (nscode != -1) { namespace(nscode & 0xffff0000, 0); // relies on the namespace() method to prevent duplicate undeclarations } } } pendingUndeclarations = null; nextReceiver.startContent(); }
public boolean isReadyForGrafting() { Receiver r2 = getReceiver(); if (r2 instanceof NamespaceReducer) { if (!((NamespaceReducer) r2).isDisinheritingNamespaces()) { Receiver r3 = ((NamespaceReducer) r2).getNextReceiver(); return r3 instanceof TinyBuilder && ((state == StartTag && (startElementProperties & ReceiverOptions.DISINHERIT_NAMESPACES) == 0) || ((TinyBuilder) r3).isPositionedAtElement()); } } return false; }
/** * Get the namespace URI corresponding to a given prefix. Return null * if the prefix is not in scope. * * @param prefix the namespace prefix * @param useDefault true if the default namespace is to be used when the * prefix is "" * @return the uri for the namespace, or null if the prefix is not in scope */ public String getURIForPrefix(String prefix, boolean useDefault) { NamePool pool = getNamePool(); if ((prefix==null || prefix.length()==0) && !useDefault) { return ""; } else if ("xml".equals(prefix)) { return NamespaceConstant.XML; } else { short prefixCode = pool.getCodeForPrefix(prefix); short uriCode = getURICode(prefixCode); if (uriCode == -1) { return null; } return pool.getURIFromURICode(uriCode); } }
/** * Create a DOMSender that will send events representing the nodes in a tree * to a nominated receiver * * @param startNode the root node of the tree to be send. Usually a document or element node. * @param receiver the object to be notified of the resulting events. The supplied Receiver must * be initialized with a PipelineConfiguration. The caller is responsible for opening * and closing the Receiver. */ public DOMSender(Node startNode, Receiver receiver) { if (startNode == null) { throw new NullPointerException("startNode"); } if (receiver == null) { throw new NullPointerException("receiver"); } this.root = startNode; this.receiver = new NamespaceReducer(receiver); }
Receiver receiver = sf.getReceiver( result, pipe, getOutputProperties()); NamespaceReducer reducer = new NamespaceReducer(); reducer.setUnderlyingReceiver(receiver); new Sender(pipe).send(source, reducer, true); } catch (XPathException err) {
NamespaceReducer ne = new NamespaceReducer(receiver); ne.setSystemId(receiver.getSystemId()); receiver = ne;
namespace(getNamePool().allocateNamespaceCode(nameCode), 0);
/** * Create a NamespaceReducer with a given destination Receiver * @param base the Receiver to which events will be passed after namespace reduction */ public NamespaceReducer(Receiver base) { setUnderlyingReceiver(base); if (pipelineConfiguration == null) { pipelineConfiguration = base.getPipelineConfiguration(); } }
public void bulkCopyElementNode(TinyElementImpl elementNode, int copyOptions) throws XPathException { NamespaceReducer r2 = (NamespaceReducer) getReceiver(); TinyBuilder target = (TinyBuilder) r2.getNextReceiver(); beforeBulkCopy(); boolean copyNamespaces = CopyOptions.includes(copyOptions, CopyOptions.ALL_NAMESPACES); target.bulkCopy(elementNode, copyNamespaces); afterBulkCopy(); }
/** * Get an iterator over all the prefixes declared in this namespace context. This will include * the default namespace (prefix="") and the XML namespace where appropriate */ public Iterator iteratePrefixes() { NamePool pool = getNamePool(); List prefixes = new ArrayList(namespacesSize); for (int i=namespacesSize-1; i>=0; i--) { String prefix = pool.getPrefixFromNamespaceCode(namespaces[i]); if (!prefixes.contains(prefix)) { prefixes.add(prefix); } } prefixes.add("xml"); return prefixes.iterator(); } }
/** * Create a DOMSender that will send events representing the nodes in a tree * to a nominated receiver * * @param startNode the root node of the tree to be send. Usually a document or element node. * @param receiver the object to be notified of the resulting events. The supplied Receiver must * be initialized with a PipelineConfiguration. The caller is responsible for opening * and closing the Receiver. */ public DOMSender(Node startNode, Receiver receiver) { if (startNode == null) { throw new NullPointerException("startNode"); } if (receiver == null) { throw new NullPointerException("receiver"); } this.root = startNode; this.receiver = new NamespaceReducer(receiver); }
Receiver receiver = sf.getReceiver( result, pipe, getOutputProperties()); NamespaceReducer reducer = new NamespaceReducer(); reducer.setUnderlyingReceiver(receiver); new Sender(pipe).send(source, reducer, true); } catch (XPathException err) {
NamespaceReducer ne = new NamespaceReducer(receiver); ne.setSystemId(receiver.getSystemId()); receiver = ne;
namespace(getNamePool().allocateNamespaceCode(nameCode), 0);
public boolean isReadyForBulkCopy() { Receiver r2 = getReceiver(); if (r2 instanceof NamespaceReducer) { if (!((NamespaceReducer) r2).isDisinheritingNamespaces()) { Receiver r3 = ((NamespaceReducer) r2).getNextReceiver(); return r3 instanceof TinyBuilder && ((state == StartTag && (startElementProperties & ReceiverOptions.DISINHERIT_NAMESPACES) == 0) || ((TinyBuilder) r3).isPositionedAtElement()); } } return false; }
/** * Get the namespace URI corresponding to a given prefix. Return null * if the prefix is not in scope. * * @param prefix the namespace prefix * @param useDefault true if the default namespace is to be used when the * prefix is "" * @return the uri for the namespace, or null if the prefix is not in scope */ public String getURIForPrefix(String prefix, boolean useDefault) { NamePool pool = getNamePool(); if ((prefix==null || prefix.length()==0) && !useDefault) { return ""; } else if ("xml".equals(prefix)) { return NamespaceConstant.XML; } else { short prefixCode = pool.getCodeForPrefix(prefix); short uriCode = getURICode(prefixCode); if (uriCode == -1) { return null; } return pool.getURIFromURICode(uriCode); } }
/** * Create a NamespaceReducer with a given destination Receiver * @param base the Receiver to which events will be passed after namespace reduction */ public NamespaceReducer(Receiver base) { setUnderlyingReceiver(base); if (pipelineConfiguration == null) { pipelineConfiguration = base.getPipelineConfiguration(); } }
public void graftElementNode(TinyElementImpl elementNode, int copyOptions) throws XPathException { NamespaceReducer r2 = (NamespaceReducer)getReceiver(); TinyBuilder target = (TinyBuilder)r2.getNextReceiver(); beforeBulkCopy(); boolean copyNamespaces = CopyOptions.includes(copyOptions, CopyOptions.ALL_NAMESPACES); target.graft(elementNode, copyNamespaces); afterBulkCopy(); }