| Method from org.apache.cocoon.transformation.CIncludeTransformer Detail: |
public void dispose() {
if (null != this.manager) {
this.manager.release(this.cacheManager);
this.manager = null;
}
super.dispose();
}
|
protected Object endCompiledXMLRecording() throws SAXException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("BEGIN endCompiledXMLRecording");
}
XMLSerializer recorder = (XMLSerializer)this.removeRecorder();
Object text = recorder.getSAXFragment();
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("END endCompiledXMLRecording text="+text);
}
return text;
}
Stop recording of compiled XML. |
public void endDocument() throws SAXException {
if ( this.compiling ) {
Object compiledXML = this.endCompiledXMLRecording();
XMLDeserializer deserializer = null;
try {
deserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
deserializer.setConsumer(this.filter);
deserializer.deserialize(compiledXML);
} catch (ServiceException ce) {
throw new SAXException("Unable to lookup xml deserializer.", ce);
} finally {
this.manager.release( deserializer );
}
}
super.endDocument();
}
|
public void endTransformingElement(String uri,
String name,
String raw) throws IOException, SAXException, ProcessingException {
if (name.equals(CINCLUDE_INCLUDE_ELEMENT)) {
// do nothing
return;
} else if (name.equals(CINCLUDE_INCLUDEXML_ELEMENT)
&& this.state == STATE_INCLUDE) {
// Element: includexml
this.state = STATE_OUTSIDE;
final String resource = (String)stack.pop();
final boolean ignoreErrors = stack.pop().equals("true");
if (getLogger().isDebugEnabled()) {
getLogger().debug("Processing includexml element: src=" + resource
+ ", ignoreErrors=" + ignoreErrors
+ ", configuration=" + this.configurationParameters
+ ", parameters=" + this.resourceParameters);
}
Source source = null;
try {
source = SourceUtil.getSource(resource,
this.configurationParameters,
this.resourceParameters,
this.resolver);
XMLSerializer serializer = null;
XMLDeserializer deserializer = null;
try {
if ( ignoreErrors ) {
serializer = (XMLSerializer) this.manager.lookup(XMLSerializer.ROLE);
deserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
SourceUtil.toSAX(source, serializer, this.configurationParameters, true);
deserializer.setConsumer( this.xmlConsumer );
deserializer.deserialize( serializer.getSAXFragment() );
} else {
SourceUtil.toSAX(source, this.xmlConsumer, this.configurationParameters, true);
}
} catch (ProcessingException pe) {
if (!ignoreErrors) throw pe;
} catch (ServiceException ignore) {
} finally {
this.manager.release( serializer );
this.manager.release( deserializer );
}
} catch (SourceException se) {
if (!ignoreErrors) throw SourceUtil.handle(se);
} catch (SAXException se) {
if (!ignoreErrors) throw se;
} catch (IOException ioe) {
if (!ignoreErrors) throw ioe;
} finally {
this.resolver.release(source);
}
// restore values
this.ignoreWhitespaces = ((Boolean)stack.pop()).booleanValue();
this.ignoreEmptyCharacters = ((Boolean)stack.pop()).booleanValue();
// src element
} else if (name.equals(CINCLUDE_SRC_ELEMENT)
&& this.state == STATE_INCLUDE) {
this.stack.push(this.endTextRecording());
} else if (name.equals(CINCLUDE_PARAMETERS_ELEMENT)
&& this.state == STATE_INCLUDE) {
this.resourceParameters = new SourceParameters();
// Now get the parameters off the stack
String label = (String)stack.pop();
String key = null;
String value = null;
while (!label.equals("end")) {
if (label.equals("name")) key = (String)stack.pop();
if (label.equals("value")) value = (String)stack.pop();
if (key != null && value != null) {
this.resourceParameters.setParameter(key, value);
key = null;
value = null;
}
label = (String)stack.pop();
}
} else if (name.equals(CINCLUDE_CONFIGURATION_ELEMENT) == true
&& this.state == STATE_INCLUDE) {
this.configurationParameters = new Parameters();
// Now get the parameters off the stack
String label = (String)stack.pop();
String key = null;
String value = null;
while (!label.equals("end")) {
if (label.equals("name")) key = (String)stack.pop();
if (label.equals("value")) value = (String)stack.pop();
if (key != null && value != null) {
this.configurationParameters.setParameter(key, value);
key = null;
value = null;
}
label = (String)stack.pop();
}
} else if (name.equals(CINCLUDE_PARAMETER_ELEMENT) == true
&& this.state == STATE_INCLUDE) {
} else if (name.equals(CINCLUDE_NAME_ELEMENT) == true
&& this.state == STATE_INCLUDE) {
stack.push(this.endTextRecording());
stack.push("name");
// parameter value
} else if (name.equals(CINCLUDE_VALUE_ELEMENT) == true
&& this.state == STATE_INCLUDE) {
stack.push(this.endSerializedXMLRecording());
stack.push("value");
} else if (name.equals(CINCLUDE_CACHED_INCLUDE_ELEMENT)) {
if (this.compiling) {
super.endTransformingElement(uri,
CINCLUDE_CACHED_INCLUDE_PLACEHOLDER_ELEMENT,
raw + "p");
}
// do nothing else
} else {
super.endTransformingElement(uri, name, raw);
}
}
|
public Serializable getKey() {
if (this.supportCaching
&& null != this.cacheManager
&& this.cachingSession.getExpires() > 0) {
return "1";
}
return null;
}
|
public SourceValidity getValidity() {
if (this.supportCaching
&& null != this.cacheManager
&& this.cachingSession.getExpires() > 0
&& !this.cachingSession.isPurging()) {
return this.cachingSession.getExpiresValidity();
}
return null;
}
|
protected String processCIncludeElement(String src,
String element,
String select,
String ns,
String prefix,
boolean stripRoot,
boolean cache) throws IOException, SAXException {
if (src == null) {
throw new SAXException("Missing 'src' attribute on cinclude:include element");
}
if (element == null) element="";
if (select == null) select="";
if (ns == null) ns="";
if (prefix == null) prefix="";
if (getLogger().isDebugEnabled()) {
getLogger().debug("Processing include element: src=" + src
+ ", element=" + element
+ ", select=" + select
+ ", ns=" + ns
+ ", prefix=" + prefix
+ ", stripRoot=" + stripRoot
+ ", caching=" + cache);
}
if (cache) {
src = this.cacheManager.load(src, this.cachingSession);
if (this.cachingSession.isParallel() && !this.cachingSession.isPreemptive()) {
if (!this.compiling) {
this.compiling = true;
this.startCompiledXMLRecording();
}
} else {
this.cacheManager.stream(src, this.cachingSession, this.filter);
}
return src;
}
// usual no caching stuff
if (!"".equals(element)) {
if (!ns.equals("")) {
super.startPrefixMapping(prefix, ns);
}
super.startElement(ns,
element,
(!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element),
XMLUtils.EMPTY_ATTRIBUTES);
}
Source source = null;
try {
source = this.resolver.resolveURI(src);
if (!"".equals(select)) {
DOMParser parser = null;
XPathProcessor processor = null;
try {
parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
InputSource input = SourceUtil.getInputSource(source);
Document document = parser.parseDocument(input);
NodeList list = processor.selectNodeList(document, select);
int length = list.getLength();
for (int i=0; i< length; i++) {
IncludeXMLConsumer.includeNode(list.item(i),
this.filter,
this.filter);
}
} finally {
this.manager.release(parser);
this.manager.release(processor);
}
} else {
String mimeType = null;
if (null != this.configurationParameters) {
mimeType = this.configurationParameters.getParameter("mime-type", mimeType);
}
if (this.compiling) {
SourceUtil.toSAX(source, mimeType, new IncludeXMLConsumer(this.contentHandler, this.lexicalHandler));
} else {
this.filter.setIgnoreRootElement(stripRoot);
SourceUtil.toSAX(source, mimeType, this.filter);
}
}
} catch (SourceException se) {
throw new SAXException("Exception in CIncludeTransformer",se);
} catch (IOException e) {
throw new SAXException("CIncludeTransformer could not read resource", e);
} catch (ProcessingException e){
throw new SAXException("Exception in CIncludeTransformer",e);
} catch(ServiceException e) {
throw new SAXException(e);
} finally {
this.resolver.release(source);
}
if (!"".equals(element)) {
super.endElement(ns, element, (!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element));
if (!ns.equals("")) {
super.endPrefixMapping(prefix);
}
}
return src;
}
|
public void recycle() {
if ( null != this.cachingSession ) {
this.cacheManager.terminateSession( this.cachingSession );
}
this.cachingSession = null;
if ( null != this.recorder) {
this.manager.release( this.recorder );
this.recorder = null;
}
this.configurationParameters = null;
this.resourceParameters = null;
if (getLogger().isDebugEnabled()) {
getLogger().debug("Finishing, time: " +
(System.currentTimeMillis() - this.startTime));
this.startTime = 0;
}
this.filter = null;
super.recycle();
}
|
public void service(ServiceManager manager) throws ServiceException {
super.service(manager);
if (this.manager.hasService(IncludeCacheManager.ROLE)) {
this.cacheManager = (IncludeCacheManager) this.manager.lookup(IncludeCacheManager.ROLE);
} else {
getLogger().warn("The cinclude transformer cannot find the IncludeCacheManager. " +
"Therefore caching is turned off for the include transformer.");
}
}
|
public void setup(SourceResolver resolver,
Map objectModel,
String source,
Parameters parameters) throws IOException, SAXException, ProcessingException {
super.setup(resolver, objectModel, source, parameters);
this.state = STATE_OUTSIDE;
if ( null != this.cacheManager ) {
this.cachingSession = this.cacheManager.getSession( this.parameters );
}
this.compiling = false;
this.supportCaching = parameters.getParameterAsBoolean("support-caching", false);
if (getLogger().isDebugEnabled()) {
getLogger().debug("Starting, session " + this.cachingSession);
this.startTime = System.currentTimeMillis();
}
}
|
protected void startCompiledXMLRecording() throws SAXException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("BEGIN startCompiledXMLRecording");
}
try {
this.recorder = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
this.addRecorder(recorder);
} catch (ServiceException ce) {
throw new SAXException("Unable to lookup xml serializer for compiling xml.", ce);
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("END startCompiledXMLRecording");
}
}
Start recording of compiled xml.
The incomming SAX events are recorded and a compiled representation
is created. These events are not forwarded to the next component in
the pipeline. |
public void startDocument() throws SAXException {
this.filter = new MyFilter(this.xmlConsumer,
this);
super.startDocument();
}
|
public void startTransformingElement(String uri,
String name,
String raw,
Attributes attr) throws IOException, SAXException, ProcessingException {
// Element: include
if (name.equals(CINCLUDE_INCLUDE_ELEMENT)) {
String stripRootValue = attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_STRIP_ROOT_ATTRIBUTE);
boolean stripRoot = StringUtils.equals(stripRootValue, "true");
processCIncludeElement(attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE),
attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE),
attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_SELECT_ATTRIBUTE),
attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE),
attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE),
stripRoot,
false);
// Element: includexml
} else if (name.equals(CINCLUDE_INCLUDEXML_ELEMENT)
&& this.state == STATE_OUTSIDE) {
this.state = STATE_INCLUDE;
String ignoreErrors = attr.getValue("", CINCLUDE_INCLUDEXML_ELEMENT_IGNORE_ERRORS_ATTRIBUTE);
if (ignoreErrors == null || ignoreErrors.length() == 0) {
ignoreErrors = "false";
}
this.stack.push(BooleanUtils.toBooleanObject(this.ignoreEmptyCharacters));
this.stack.push(BooleanUtils.toBooleanObject(this.ignoreWhitespaces));
this.stack.push(ignoreErrors);
this.ignoreEmptyCharacters = false;
this.ignoreWhitespaces = true;
// target
} else if (name.equals(CINCLUDE_SRC_ELEMENT)
&& this.state == STATE_INCLUDE) {
this.startTextRecording();
// configparameters
} else if (name.equals(CINCLUDE_CONFIGURATION_ELEMENT)
&& this.state == STATE_INCLUDE) {
stack.push("end");
// parameters
} else if (name.equals(CINCLUDE_PARAMETERS_ELEMENT)
&& this.state == STATE_INCLUDE) {
stack.push("end");
// parameter
} else if (name.equals(CINCLUDE_PARAMETER_ELEMENT)
&& this.state == STATE_INCLUDE) {
// parameter name
} else if (name.equals(CINCLUDE_NAME_ELEMENT)
&& this.state == STATE_INCLUDE) {
this.startTextRecording();
// parameter value
} else if (name.equals(CINCLUDE_VALUE_ELEMENT)
&& this.state == STATE_INCLUDE) {
this.startSerializedXMLRecording(XMLUtils.createPropertiesForXML(true));
} else if (name.equals(CINCLUDE_CACHED_INCLUDE_ELEMENT)) {
String src = processCIncludeElement(attr.getValue("", CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE),
null,
null,
null,
null,
false,
this.cacheManager != null);
if (this.compiling) {
this.srcAttributes.addAttribute("", CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE, CINCLUDE_SRC_ELEMENT, "CDATA", src);
super.startTransformingElement(uri,
CINCLUDE_CACHED_INCLUDE_PLACEHOLDER_ELEMENT,
raw + "p",
this.srcAttributes);
this.srcAttributes.clear();
}
} else {
super.startTransformingElement(uri, name, raw, attr);
}
}
|