bloritsch 01/10/18 10:23:46 Modified: src/org/apache/cocoon/transformation Tag: cocoon_20_branch XIncludeTransformer.java Log: Convert XIncludeTransformer to use SourceResolver instead of URLFactory Revision Changes Path No revision No revision 1.6.2.11 +44 -73 xml-cocoon2/src/org/apache/cocoon/transformation/XIncludeTransformer.java Index: XIncludeTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/transformation/XIncludeTransformer.java,v retrieving revision 1.6.2.10 retrieving revision 1.6.2.11 diff -u -r1.6.2.10 -r1.6.2.11 --- XIncludeTransformer.java 2001/10/11 08:56:15 1.6.2.10 +++ XIncludeTransformer.java 2001/10/18 17:23:46 1.6.2.11 @@ -13,11 +13,10 @@ import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; -import org.apache.avalon.framework.logger.Loggable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.components.parser.Parser; -import org.apache.cocoon.components.url.URLFactory; +import org.apache.cocoon.environment.Source; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.xml.IncludeXMLConsumer; import org.apache.cocoon.xml.dom.DOMStreamer; @@ -32,7 +31,6 @@ import javax.xml.transform.TransformerException; import java.io.*; import java.net.MalformedURLException; -import java.net.URL; import java.util.Map; import java.util.Stack; @@ -44,11 +42,11 @@ * by the SAX event FSM yet. * * @author <a href="mailto:[EMAIL PROTECTED]">Donald Ball</a> - * @version CVS $Revision: 1.6.2.10 $ $Date: 2001/10/11 08:56:15 $ $Author: cziegeler $ + * @version CVS $Revision: 1.6.2.11 $ $Date: 2001/10/18 17:23:46 $ $Author: bloritsch $ */ public class XIncludeTransformer extends AbstractTransformer implements Composable, Recyclable, Disposable { - protected URLFactory urlFactory; + protected SourceResolver resolver; protected ComponentManager manager = null; @@ -60,10 +58,10 @@ public static final String XINCLUDE_INCLUDE_ELEMENT_HREF_ATTRIBUTE = "href"; public static final String XINCLUDE_INCLUDE_ELEMENT_PARSE_ATTRIBUTE = "parse"; - protected URL base_xmlbase_uri = null; + protected Source base_xmlbase_uri = null; /** The current XMLBase URI. We start with an empty "dummy" URL. **/ - protected URL current_xmlbase_uri = null; + protected Source current_xmlbase_uri = null; /** This is a stack of xml:base attributes which belong to our ancestors **/ protected Stack xmlbase_stack = new Stack(); @@ -80,15 +78,12 @@ public void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters) - throws ProcessingException, SAXException, IOException {} + throws ProcessingException, SAXException, IOException { + this.resolver = resolver; + } public void compose(ComponentManager manager) { this.manager = manager; - try { - this.urlFactory = (URLFactory)this.manager.lookup(URLFactory.ROLE); - } catch (Exception e) { - getLogger().error("cannot obtain URLFactory", e); - } } public void startElement(String uri, String name, String raw, Attributes attr) throws SAXException { @@ -96,7 +91,7 @@ if ((value = attr.getValue(XMLBASE_NAMESPACE_URI,XMLBASE_ATTRIBUTE)) != null) { try { startXMLBaseAttribute(uri,name,value); - } catch (MalformedURLException e) { + } catch (ProcessingException e) { getLogger().debug("XincludeTransformer", e); throw new SAXException(e); } @@ -109,7 +104,7 @@ try { processXIncludeElement(href, parse); - } catch (MalformedURLException e) { + } catch (ProcessingException e) { getLogger().debug("XincludeTransformer", e); throw new SAXException(e); } catch (IOException e) { @@ -138,22 +133,22 @@ getLogger().debug("XIncludeTransformer: setDocumentLocator called " + locator.getSystemId()); } - base_xmlbase_uri = urlFactory.getURL(locator.getSystemId()); + base_xmlbase_uri = this.resolver.resolve(locator.getSystemId()); // If url ends with .xxx then truncate to dir - if (base_xmlbase_uri.toExternalForm().lastIndexOf('.') > base_xmlbase_uri.toExternalForm().lastIndexOf('/')) { - base_xmlbase_uri = new URL(base_xmlbase_uri.toExternalForm().substring(0,base_xmlbase_uri.toExternalForm().lastIndexOf('/')+1)); + if (base_xmlbase_uri.getSystemId().lastIndexOf('.') > base_xmlbase_uri.getSystemId().lastIndexOf('/')) { + base_xmlbase_uri = this.resolver.resolve(base_xmlbase_uri.getSystemId().substring(0,base_xmlbase_uri.getSystemId().lastIndexOf('/')+1)); } if (current_xmlbase_uri == null) { current_xmlbase_uri = base_xmlbase_uri; } - } catch (MalformedURLException e) {getLogger().debug("XincludeTransformer", e);} + } catch (Exception e) {getLogger().debug("XincludeTransformer", e);} super.setDocumentLocator(locator); } - protected void startXMLBaseAttribute(String uri, String name, String value) throws MalformedURLException { + protected void startXMLBaseAttribute(String uri, String name, String value) throws ProcessingException { String urlLoc = value; if (! urlLoc.endsWith("/")) { @@ -167,13 +162,18 @@ if (current_xmlbase_uri != null) { xmlbase_stack.push(current_xmlbase_uri); } - current_xmlbase_uri = urlFactory.getURL(urlLoc); - xmlbase_element_uri_stack.push(last_xmlbase_element_uri); - last_xmlbase_element_uri = uri; - - xmlbase_element_name_stack.push(last_xmlbase_element_name); - last_xmlbase_element_name = name; + try { + current_xmlbase_uri = this.resolver.resolve(urlLoc); + + xmlbase_element_uri_stack.push(last_xmlbase_element_uri); + last_xmlbase_element_uri = uri; + + xmlbase_element_name_stack.push(last_xmlbase_element_name); + last_xmlbase_element_name = name; + } catch (Exception e) { + throw new ProcessingException("Could not resolve '" + urlLoc + "'", e); + } } protected void endXMLBaseAttribute() { @@ -182,7 +182,7 @@ } if (xmlbase_stack.size() > 0) { - current_xmlbase_uri = (URL)xmlbase_stack.pop(); + current_xmlbase_uri = (Source)xmlbase_stack.pop(); } else { current_xmlbase_uri = base_xmlbase_uri; } @@ -190,52 +190,36 @@ last_xmlbase_element_name = (String)xmlbase_element_name_stack.pop(); } - protected void processXIncludeElement(String href, String parse) throws SAXException,MalformedURLException,IOException { + protected void processXIncludeElement(String href, String parse) throws SAXException,ProcessingException,IOException { if (getLogger().isDebugEnabled()) { getLogger().debug("Processing XInclude element: href="+href+", parse="+parse); - getLogger().debug("Base URI: " + current_xmlbase_uri.toExternalForm()); + getLogger().debug("Base URI: " + current_xmlbase_uri.getSystemId()); } - URL url; + Source url; String suffix; int index = href.indexOf('#'); if (index < 0) { - url = urlFactory.getURL(current_xmlbase_uri,href); + url = this.resolver.resolve(current_xmlbase_uri.getSystemId() + href); suffix = ""; } else { - url = urlFactory.getURL(current_xmlbase_uri,href.substring(0,index)); + url = this.resolver.resolve(current_xmlbase_uri.getSystemId() + href.substring(0,index)); suffix = href.substring(index+1); } if (getLogger().isDebugEnabled()) { getLogger().debug("URL: "+url+"\nSuffix: "+suffix); } - Object object = url.getContent(); - getLogger().debug("Object: "+object); + if (parse.equals("text")) { getLogger().debug("Parse type is text"); - if (object instanceof Loggable) { - ((Loggable)object).setLogger(getLogger()); - } - if (object instanceof Reader) { - Reader reader = new BufferedReader((Reader)object); - int read; - char ary[] = new char[1024]; - if (reader != null) { - while ((read = reader.read(ary)) != -1) { - super.characters(ary,0,read); - } - reader.close(); - } - } else if (object instanceof InputStream) { - InputStream input = (InputStream)object; - Reader reader = new BufferedReader(new InputStreamReader(input)); - int read; - char ary[] = new char[1024]; - if (reader != null) { - while ((read = reader.read(ary)) != -1) { - super.characters(ary,0,read); - } - reader.close(); + InputStream input = url.getInputStream(); + Reader reader = new BufferedReader(new InputStreamReader(input)); + int read; + char ary[] = new char[1024]; + if (reader != null) { + while ((read = reader.read(ary)) != -1) { + super.characters(ary,0,read); } + reader.close(); } } else if (parse.equals("xml")) { getLogger().debug("Parse type is XML"); @@ -245,19 +229,7 @@ getLogger().debug("Looking up " + Parser.ROLE); parser = (Parser)manager.lookup(Parser.ROLE); - InputSource input; - if (object instanceof Loggable) { - ((Loggable)object).setLogger(getLogger()); - } - if (object instanceof Reader) { - input = new InputSource(new BufferedReader((Reader)object)); - input.setSystemId(url.toString()); - } else if (object instanceof InputStream) { - input = new InputSource(new BufferedInputStream((InputStream)object)); - input.setSystemId(url.toString()); - } else { - throw new SAXException("Unknown object type: "+object); - } + InputSource input = url.getInputSource(); if (suffix.startsWith("xpointer(") && suffix.endsWith(")")) { String xpath = suffix.substring(9,suffix.length()-1); getLogger().debug("XPath is "+xpath); @@ -282,7 +254,7 @@ } catch(SAXException e) { getLogger().error("Error in processXIncludeElement", e); throw e; - } catch(MalformedURLException e) { + } catch(ProcessingException e) { getLogger().error("Error in processXIncludeElement", e); throw e; } catch(IOException e) { @@ -300,6 +272,7 @@ public void recycle() { // Reset all variables to initial state. + this.resolver = null; base_xmlbase_uri = null; current_xmlbase_uri = null; xmlbase_stack = new Stack(); @@ -312,7 +285,5 @@ public void dispose() { - if(this.urlFactory != null) - this.manager.release((Component)this.urlFactory); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]