public void attachXMLReader(XMLReader xmlReader) { AbstractParser.attachXMLReader(xmlReader, executionContext); }
public void detachXMLReader() { AbstractParser.detachXMLReader(executionContext); } }
public static XMLReader getXMLReader(ExecutionContext execContext) { Stack<XMLReader> xmlReaderStack = getReaders(execContext); if(!xmlReaderStack.isEmpty()) { return xmlReaderStack.peek(); } else { return null; } }
protected XMLReader createXMLReader() throws SAXException { XMLReader reader; ExecutionContext execContext = getExecContext(); Source source = FilterSource.getSource(execContext); JavaSource javaSource = (JavaSource) source; if (isFeatureOn(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && !javaSource.isEventStreamRequired()) { throw new SAXException("Invalid Smooks configuration. Feature '" + JavaSource.FEATURE_GENERATE_EVENT_STREAM + "' is explicitly configured 'on' in the Smooks configuration, while the supplied JavaSource has explicitly configured event streaming to be off (through a call to JavaSource.setEventStreamRequired)."); boolean eventStreamingOn = (!isFeatureOff(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && javaSource.isEventStreamRequired()); if (eventStreamingOn && javaSource.getSourceObjects() != null) { reader = new XStreamXMLReader(); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); setHandlers(reader); setFeatures(reader);
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { Smooks smooks = getSmooksInstance(); ExecutionContext nestedExecutionContext = smooks.createExecutionContext(); // In case there's an attached event listener... nestedExecutionContext.setEventListener(executionContext.getEventListener()); // Copy over the XMLReader stack... AbstractParser.setReaders(AbstractParser.getReaders(executionContext), nestedExecutionContext); // Attach the NamespaceDeclarationStack to the nested execution context... NamespaceDeclarationStack nsStack = NamespaceMappings.getNamespaceDeclarationStack(executionContext); NamespaceMappings.setNamespaceDeclarationStack(nsStack, nestedExecutionContext); SmooksContentHandler parentContentHandler = SmooksContentHandler.getHandler(executionContext); if(parentContentHandler.getNestedContentHandler() != null) { throw new SmooksException("Illegal use of more than one nested content handler fired on the same element."); } SmooksContentHandler nestedContentHandler = new SAXHandler(nestedExecutionContext, element.getWriter(this), parentContentHandler); DynamicSAXElementVisitorList.propogateDynamicVisitors(executionContext, nestedExecutionContext); // Attach the XMLReader instance to the nested ExecutionContext and then swap the content handler on // the XMLReader to be the nested handler created here. All events wll be forwarded to the .. XMLReader xmlReader = AbstractParser.getXMLReader(executionContext); AbstractParser.attachXMLReader(xmlReader, nestedExecutionContext); xmlReader.setContentHandler(nestedContentHandler); executionContext.setAttribute(NestedExecutionVisitor.class, nestedExecutionContext); // Note we do not execute the Smooks filterSource methods for a nested instance... we just install // the content handler and redirect the reader events to it... }
protected InputSource createInputSource(Source source, String contentEncoding) { // Also attach the underlying stream to the InputSource... if(source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; InputStream inputStream; Reader reader; inputStream = getInputStream(streamSource); reader = streamSource.getReader(); if(reader == null) { if(inputStream == null) { throw new SmooksException("Invalid StreamSource. Unable to extract an InputStream (even by systemId) or Reader instance."); } reader = streamToReader(inputStream, contentEncoding); } InputSource inputSource = new InputSource(); inputSource.setByteStream(inputStream); inputSource.setCharacterStream(reader); return inputSource; } else { return new InputSource(getReader(source, contentEncoding)); } }
public AbstractParser(ExecutionContext execContext) { this(execContext, getSAXParserConfiguration(execContext.getDeliveryConfig())); }
public static boolean isFeatureOff(String name, SmooksResourceConfiguration saxDriverConfig) throws SAXException { boolean featureOff = isFeature(name, FeatureValue.OFF, saxDriverConfig); // Make sure the same feature is not also configured on... if (featureOff && isFeature(name, FeatureValue.ON, saxDriverConfig)) { throw new SAXException("Invalid Smooks configuration. Feature '" + name + "' is explicitly configured 'on' and 'off'. Must be one or the other!"); } return featureOff; }
private void setHandlers(XMLReader reader) throws SAXException { if (saxDriverConfig != null) { List<Parameter> handlers; handlers = saxDriverConfig.getParameters("sax-handler"); if (handlers != null) { for (Parameter handler : handlers) { Object handlerObj = createHandler(handler.getValue()); if (handlerObj instanceof EntityResolver) { reader.setEntityResolver((EntityResolver) handlerObj); } if (handlerObj instanceof DTDHandler) { reader.setDTDHandler((DTDHandler) handlerObj); } if (handlerObj instanceof ErrorHandler) { reader.setErrorHandler((ErrorHandler) handlerObj); } } } } }
@Override public final void endElement(String uri, String localName, String qName) throws SAXException { try { endEvent.set(uri, localName, qName); lastEvent = endEvent; endElement(endEvent); depth--; } finally { if(!endReplayed && depth == 0 && parentContentHandler != null) { endReplayed = true; // Replay the last sax event from this handler onto the parent handler ... replay(parentContentHandler); // Reinstate the parent handler on the XMLReader so all events are // forwarded to it again ... XMLReader xmlReader = AbstractParser.getXMLReader(executionContext); xmlReader.setContentHandler(parentContentHandler); // Remove the nested handler (this handler) form the parent handler... parentContentHandler.resetNestedContentHandler(); } } getNamespaceDeclarationStack().popNamespaces(); }
protected XMLReader createXMLReader() throws SAXException { XMLReader reader; ExecutionContext execContext = getExecContext(); Source source = FilterSource.getSource(execContext); JavaSource javaSource = (JavaSource) source; if (isFeatureOn(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && !javaSource.isEventStreamRequired()) { throw new SAXException("Invalid Smooks configuration. Feature '" + JavaSource.FEATURE_GENERATE_EVENT_STREAM + "' is explicitly configured 'on' in the Smooks configuration, while the supplied JavaSource has explicitly configured event streaming to be off (through a call to JavaSource.setEventStreamRequired)."); boolean eventStreamingOn = (!isFeatureOff(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && javaSource.isEventStreamRequired()); if (eventStreamingOn && javaSource.getSourceObjects() != null) { reader = new XStreamXMLReader(); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); setHandlers(reader); setFeatures(reader);
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { Smooks smooks = getSmooksInstance(); ExecutionContext nestedExecutionContext = smooks.createExecutionContext(); // In case there's an attached event listener... nestedExecutionContext.setEventListener(executionContext.getEventListener()); // Copy over the XMLReader stack... AbstractParser.setReaders(AbstractParser.getReaders(executionContext), nestedExecutionContext); // Attach the NamespaceDeclarationStack to the nested execution context... NamespaceDeclarationStack nsStack = NamespaceMappings.getNamespaceDeclarationStack(executionContext); NamespaceMappings.setNamespaceDeclarationStack(nsStack, nestedExecutionContext); SmooksContentHandler parentContentHandler = SmooksContentHandler.getHandler(executionContext); if(parentContentHandler.getNestedContentHandler() != null) { throw new SmooksException("Illegal use of more than one nested content handler fired on the same element."); } SmooksContentHandler nestedContentHandler = new SAXHandler(nestedExecutionContext, element.getWriter(this), parentContentHandler); DynamicSAXElementVisitorList.propogateDynamicVisitors(executionContext, nestedExecutionContext); // Attach the XMLReader instance to the nested ExecutionContext and then swap the content handler on // the XMLReader to be the nested handler created here. All events wll be forwarded to the .. XMLReader xmlReader = AbstractParser.getXMLReader(executionContext); AbstractParser.attachXMLReader(xmlReader, nestedExecutionContext); xmlReader.setContentHandler(nestedContentHandler); executionContext.setAttribute(NestedExecutionVisitor.class, nestedExecutionContext); // Note we do not execute the Smooks filterSource methods for a nested instance... we just install // the content handler and redirect the reader events to it... }
protected InputSource createInputSource(Source source, String contentEncoding) { // Also attach the underlying stream to the InputSource... if(source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; InputStream inputStream; Reader reader; inputStream = getInputStream(streamSource); reader = streamSource.getReader(); if(reader == null) { if(inputStream == null) { throw new SmooksException("Invalid StreamSource. Unable to extract an InputStream (even by systemId) or Reader instance."); } reader = streamToReader(inputStream, contentEncoding); } InputSource inputSource = new InputSource(); inputSource.setByteStream(inputStream); inputSource.setCharacterStream(reader); return inputSource; } else { return new InputSource(getReader(source, contentEncoding)); } }
public AbstractParser(ExecutionContext execContext) { this(execContext, getSAXParserConfiguration(execContext.getDeliveryConfig())); }
public static boolean isFeatureOff(String name, SmooksResourceConfiguration saxDriverConfig) throws SAXException { boolean featureOff = isFeature(name, FeatureValue.OFF, saxDriverConfig); // Make sure the same feature is not also configured on... if (featureOff && isFeature(name, FeatureValue.ON, saxDriverConfig)) { throw new SAXException("Invalid Smooks configuration. Feature '" + name + "' is explicitly configured 'on' and 'off'. Must be one or the other!"); } return featureOff; }
private void setHandlers(XMLReader reader) throws SAXException { if (saxDriverConfig != null) { List<Parameter> handlers; handlers = saxDriverConfig.getParameters("sax-handler"); if (handlers != null) { for (Parameter handler : handlers) { Object handlerObj = createHandler(handler.getValue()); if (handlerObj instanceof EntityResolver) { reader.setEntityResolver((EntityResolver) handlerObj); } if (handlerObj instanceof DTDHandler) { reader.setDTDHandler((DTDHandler) handlerObj); } if (handlerObj instanceof ErrorHandler) { reader.setErrorHandler((ErrorHandler) handlerObj); } } } } }
@Override public final void endElement(String uri, String localName, String qName) throws SAXException { try { endEvent.set(uri, localName, qName); lastEvent = endEvent; endElement(endEvent); depth--; } finally { if(!endReplayed && depth == 0 && parentContentHandler != null) { endReplayed = true; // Replay the last sax event from this handler onto the parent handler ... replay(parentContentHandler); // Reinstate the parent handler on the XMLReader so all events are // forwarded to it again ... XMLReader xmlReader = AbstractParser.getXMLReader(executionContext); xmlReader.setContentHandler(parentContentHandler); // Remove the nested handler (this handler) form the parent handler... parentContentHandler.resetNestedContentHandler(); } } getNamespaceDeclarationStack().popNamespaces(); }
protected XMLReader createXMLReader() throws SAXException { XMLReader reader; ExecutionContext execContext = getExecContext(); Source source = FilterSource.getSource(execContext); JavaSource javaSource = (JavaSource) source; if (isFeatureOn(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && !javaSource.isEventStreamRequired()) { throw new SAXException("Invalid Smooks configuration. Feature '" + JavaSource.FEATURE_GENERATE_EVENT_STREAM + "' is explicitly configured 'on' in the Smooks configuration, while the supplied JavaSource has explicitly configured event streaming to be off (through a call to JavaSource.setEventStreamRequired)."); boolean eventStreamingOn = (!isFeatureOff(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && javaSource.isEventStreamRequired()); if (eventStreamingOn && javaSource.getSourceObjects() != null) { reader = new XStreamXMLReader(); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); setHandlers(reader); setFeatures(reader);
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { Smooks smooks = getSmooksInstance(); ExecutionContext nestedExecutionContext = smooks.createExecutionContext(); // In case there's an attached event listener... nestedExecutionContext.setEventListener(executionContext.getEventListener()); // Copy over the XMLReader stack... AbstractParser.setReaders(AbstractParser.getReaders(executionContext), nestedExecutionContext); // Attach the NamespaceDeclarationStack to the nested execution context... NamespaceDeclarationStack nsStack = NamespaceMappings.getNamespaceDeclarationStack(executionContext); NamespaceMappings.setNamespaceDeclarationStack(nsStack, nestedExecutionContext); SmooksContentHandler parentContentHandler = SmooksContentHandler.getHandler(executionContext); if(parentContentHandler.getNestedContentHandler() != null) { throw new SmooksException("Illegal use of more than one nested content handler fired on the same element."); } SmooksContentHandler nestedContentHandler = new SAXHandler(nestedExecutionContext, element.getWriter(this), parentContentHandler); DynamicSAXElementVisitorList.propogateDynamicVisitors(executionContext, nestedExecutionContext); // Attach the XMLReader instance to the nested ExecutionContext and then swap the content handler on // the XMLReader to be the nested handler created here. All events wll be forwarded to the .. XMLReader xmlReader = AbstractParser.getXMLReader(executionContext); AbstractParser.attachXMLReader(xmlReader, nestedExecutionContext); xmlReader.setContentHandler(nestedContentHandler); executionContext.setAttribute(NestedExecutionVisitor.class, nestedExecutionContext); // Note we do not execute the Smooks filterSource methods for a nested instance... we just install // the content handler and redirect the reader events to it... }
protected InputSource createInputSource(Source source, String contentEncoding) { // Also attach the underlying stream to the InputSource... if(source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; InputStream inputStream; Reader reader; inputStream = getInputStream(streamSource); reader = streamSource.getReader(); if(reader == null) { if(inputStream == null) { throw new SmooksException("Invalid StreamSource. Unable to extract an InputStream (even by systemId) or Reader instance."); } reader = streamToReader(inputStream, contentEncoding); } InputSource inputSource = new InputSource(); inputSource.setByteStream(inputStream); inputSource.setCharacterStream(reader); return inputSource; } else { return new InputSource(getReader(source, contentEncoding)); } }