bloritsch 01/10/18 10:23:59 Modified: src/org/apache/cocoon/transformation XIncludeTransformer.java Log: Convert XIncludeTransformer to use SourceResolver instead of URLFactory Revision Changes Path 1.18 +44 -71 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.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- XIncludeTransformer.java 2001/10/11 07:28:24 1.17 +++ XIncludeTransformer.java 2001/10/18 17:23:59 1.18 @@ -13,12 +13,11 @@ 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.components.xpath.XPathProcessor; +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; @@ -31,7 +30,6 @@ import java.io.*; import java.net.MalformedURLException; -import java.net.URL; import java.util.Map; import java.util.Stack; @@ -43,11 +41,11 @@ * by the SAX event FSM yet. * * @author <a href="mailto:[EMAIL PROTECTED]">Donald Ball</a> - * @version CVS $Revision: 1.17 $ $Date: 2001/10/11 07:28:24 $ $Author: cziegeler $ + * @version CVS $Revision: 1.18 $ $Date: 2001/10/18 17:23:59 $ $Author: bloritsch $ */ public class XIncludeTransformer extends AbstractTransformer implements Composable, Recyclable, Disposable { - protected URLFactory urlFactory; + private SourceResolver resolver; /** XPath Processor */ private XPathProcessor processor = null; @@ -62,10 +60,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(); @@ -82,16 +80,13 @@ 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); - } - try { this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); } catch (Exception e) { getLogger().error("cannot obtain XPathProcessor", e); @@ -103,7 +98,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); } @@ -116,7 +111,7 @@ try { processXIncludeElement(href, parse); - } catch (MalformedURLException e) { + } catch (ProcessingException e) { getLogger().debug("XincludeTransformer", e); throw new SAXException(e); } catch (IOException e) { @@ -145,22 +140,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("/")) { @@ -174,13 +169,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() { @@ -189,7 +189,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; } @@ -197,52 +197,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"); @@ -252,19 +236,8 @@ 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); @@ -302,6 +275,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(); @@ -314,7 +288,6 @@ public void dispose() { - this.manager.release((Component)this.urlFactory); this.manager.release((Component)this.processor); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]