From: <sha...@us...> - 2009-01-11 18:59:42
|
Revision: 8498 http://exist.svn.sourceforge.net/exist/?rev=8498&view=rev Author: shabanovd Date: 2009-01-11 18:59:37 +0000 (Sun, 11 Jan 2009) Log Message: ----------- [feature] handle input stream for stylesheet Modified Paths: -------------- branches/shabanovd/xslt/src/org/exist/xslt/Configuration.java branches/shabanovd/xslt/src/org/exist/xslt/Transformer.java branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryAllocator.java branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryImpl.java branches/shabanovd/xslt/src/org/exist/xslt/TransformerHandlerImpl.java branches/shabanovd/xslt/src/org/exist/xslt/XSL.java branches/shabanovd/xslt/src/org/exist/xslt/XSLContext.java branches/shabanovd/xslt/src/org/exist/xslt/XSLStylesheet.java branches/shabanovd/xslt/src/org/exist/xslt/compiler/Element.java branches/shabanovd/xslt/src/org/exist/xslt/expression/ApplyTemplates.java branches/shabanovd/xslt/src/org/exist/xslt/expression/ForEach.java branches/shabanovd/xslt/src/org/exist/xslt/expression/Param.java branches/shabanovd/xslt/src/org/exist/xslt/expression/Template.java branches/shabanovd/xslt/src/org/exist/xslt/expression/ValueOf.java branches/shabanovd/xslt/src/org/exist/xslt/expression/Variable.java branches/shabanovd/xslt/src/org/exist/xslt/pattern/Pattern.java Added Paths: ----------- branches/shabanovd/xslt/src/org/exist/xslt/expression/i/ branches/shabanovd/xslt/src/org/exist/xslt/expression/i/Parameted.java branches/shabanovd/xslt/src/org/exist/xslt/functions/ branches/shabanovd/xslt/src/org/exist/xslt/functions/Generate_id.java branches/shabanovd/xslt/src/org/exist/xslt/functions/XSLTModule.java Modified: branches/shabanovd/xslt/src/org/exist/xslt/Configuration.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/Configuration.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/Configuration.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -1,8 +1,7 @@ /* * eXist Open Source Native XML Database - * Copyright (C) 2001-08 Wolfgang M. Meier - * wol...@ex... - * http://exist.sourceforge.net + * Copyright (C) 2001-2009 The eXist Project + * http://exist-db.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -18,8 +17,9 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * - * Configuration.java shabanovd + * $Id$ */ + package org.exist.xslt; /** Modified: branches/shabanovd/xslt/src/org/exist/xslt/Transformer.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/Transformer.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/Transformer.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -44,8 +44,10 @@ private XSLStylesheet compiled = null; - private URIResolver resolver; + private URIResolver resolver; + private Properties outputPropertys = null; + /* (non-Javadoc) * @see javax.xml.transform.Transformer#clearParameters() */ @@ -69,8 +71,10 @@ */ @Override public Properties getOutputProperties() { - // TODO Auto-generated method stub - throw new RuntimeException("Not implemented: Transformer.getOutputProperties"); + if (outputPropertys == null) + outputPropertys = new Properties(); + + return outputPropertys; } /* (non-Javadoc) @@ -78,8 +82,7 @@ */ @Override public String getOutputProperty(String name) throws IllegalArgumentException { - // TODO Auto-generated method stub - throw new RuntimeException("Not implemented: Transformer.getOutputProperty"); + return getOutputProperties().getProperty(name); } /* (non-Javadoc) @@ -113,8 +116,7 @@ */ @Override public void setOutputProperties(Properties oformat) { - // TODO Auto-generated method stub - throw new RuntimeException("Not implemented: Transformer.setOutputProperties"); + outputPropertys = oformat; } /* (non-Javadoc) @@ -122,8 +124,7 @@ */ @Override public void setOutputProperty(String name, String value) throws IllegalArgumentException { - // TODO Auto-generated method stub - throw new RuntimeException("Not implemented: Transformer.setOutputProperty"); + getOutputProperties().put(name, value); } /* (non-Javadoc) Modified: branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryAllocator.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryAllocator.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryAllocator.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -29,6 +29,7 @@ import org.apache.log4j.Logger; import org.exist.storage.BrokerPool; +import org.exist.storage.DBBroker; /** * Allows the TransformerFactory that is used for XSLT to be @@ -52,6 +53,8 @@ public final static String CONFIGURATION_TRANSFORMER_ATTRIBUTE_ELEMENT_NAME = "attribute"; public final static String PROPERTY_TRANSFORMER_ATTRIBUTES = "transformer.attributes"; + private static SAXTransformerFactory factory = null; + //private constructor private TransformerFactoryAllocator() { @@ -73,9 +76,9 @@ * Instead of SAXTransformerFactory.newInstance() use * TransformerFactoryAllocator.getTransformerFactory(broker).newInstance() */ - public static SAXTransformerFactory getTransformerFactory( BrokerPool pool ) - { - SAXTransformerFactory factory; + public static SAXTransformerFactory getTransformerFactory( BrokerPool pool, DBBroker broker ) {//UNDERSTAND: why static -shdk + if (factory != null) + return factory; //get the transformer class name from conf.xml String transformerFactoryClassName = (String)pool.getConfiguration().getProperty(PROPERTY_TRANSFORMER_CLASS); @@ -98,6 +101,11 @@ LOG.debug( "Set transformer factory: " + transformerFactoryClassName ); } + if (factory instanceof TransformerFactoryImpl) { + TransformerFactoryImpl factoryAtExist = (TransformerFactoryImpl) factory; + factoryAtExist.setBroker(broker); + } + Hashtable attributes = (Hashtable)pool.getConfiguration().getProperty( PROPERTY_TRANSFORMER_ATTRIBUTES ); Enumeration attrNames = attributes.keys(); Modified: branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryImpl.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryImpl.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/TransformerFactoryImpl.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,6 +21,8 @@ */ package org.exist.xslt; +import java.io.InputStream; + import javax.xml.transform.ErrorListener; import javax.xml.transform.Source; import javax.xml.transform.Templates; @@ -30,6 +32,7 @@ import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TemplatesHandler; import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamSource; import org.apache.log4j.Logger; import org.exist.dom.i.ElementAtExist; @@ -47,12 +50,18 @@ private DBBroker broker = null; + private URIResolver resolver; + public TransformerFactoryImpl() { } public void setBroker(DBBroker broker) { this.broker = broker; } + + public DBBroker getBroker() { + return broker; + } /* (non-Javadoc) * @see javax.xml.transform.TransformerFactory#getAssociatedStylesheet(javax.xml.transform.Source, java.lang.String, java.lang.String, java.lang.String) @@ -92,7 +101,7 @@ */ @Override public URIResolver getURIResolver() { - throw new RuntimeException("Not implemented: TransformerFactory.getURIResolver"); + return resolver; } /* (non-Javadoc) @@ -100,12 +109,30 @@ */ @Override public Templates newTemplates(Source source) throws TransformerConfigurationException { - try { - return XSL.compile((ElementAtExist)source); - } catch (XPathException e) { - LOG.debug(e); - throw new TransformerConfigurationException("compilation error."); + //XXX: handle buffered input stream + if (source instanceof ElementAtExist) { + try { + return XSL.compile((ElementAtExist)source); + } catch (XPathException e) { + LOG.debug(e); + throw new TransformerConfigurationException("Compilation error.",e); + } + } else if (source instanceof InputStream) { + try { + return XSL.compile((InputStream)source, getBroker()); + } catch (XPathException e) { + LOG.debug(e); + throw new TransformerConfigurationException("Compilation error.",e); + } + } else if (source instanceof StreamSource) { + try { + return XSL.compile(((StreamSource)source).getInputStream(), getBroker()); + } catch (XPathException e) { + LOG.debug(e); + throw new TransformerConfigurationException("Compilation error.",e); + } } + throw new TransformerConfigurationException("Not supported source "+source.getClass()); } /* (non-Javadoc) @@ -113,6 +140,7 @@ */ @Override public Transformer newTransformer() throws TransformerConfigurationException { + //TODO: setURIresolver ??? return new org.exist.xslt.Transformer(); } @@ -153,7 +181,7 @@ */ @Override public void setURIResolver(URIResolver resolver) { - throw new RuntimeException("Not implemented: TransformerFactory.setURIResolver"); + this.resolver = resolver; } @Override Modified: branches/shabanovd/xslt/src/org/exist/xslt/TransformerHandlerImpl.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/TransformerHandlerImpl.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/TransformerHandlerImpl.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -34,7 +34,7 @@ * */ public class TransformerHandlerImpl extends SAXAdapter implements TransformerHandler { - + //XXX: handle SAX events private Transformer transformer; private String SystemId = null; Modified: branches/shabanovd/xslt/src/org/exist/xslt/XSL.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/XSL.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/XSL.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,10 +21,19 @@ */ package org.exist.xslt; +import java.io.IOException; +import java.io.InputStream; import java.text.NumberFormat; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + import org.apache.log4j.Logger; +import org.exist.dom.i.DocumentAtExist; import org.exist.dom.i.ElementAtExist; +import org.exist.memtree.SAXAdapter; +import org.exist.storage.DBBroker; import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.Expression; import org.exist.xquery.Optimizer; @@ -32,6 +41,9 @@ import org.exist.xquery.XQueryContext; import org.exist.xquery.util.ExpressionDumper; import org.exist.xslt.compiler.Element; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; /** * <xsl:stylesheet version="1.0" @@ -96,4 +108,33 @@ } return expr; } + + protected static XSLStylesheet compile(InputStream source, DBBroker broker) throws XPathException { + try { + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); + + InputSource src = new InputSource(source); + SAXParser parser = factory.newSAXParser(); + XMLReader reader = parser.getXMLReader(); + SAXAdapter adapter = new SAXAdapter(); + reader.setContentHandler(adapter); +// reader.setContentHandler(adapter); + reader.parse(src); + + DocumentAtExist document = (DocumentAtExist) adapter.getDocument(); + document.setContext(new XSLContext(broker)); + //return receiver.getDocument(); + return compile((ElementAtExist) document.getDocumentElement()); + } catch (ParserConfigurationException e) { + LOG.debug(e); + throw new XPathException(e); + } catch (SAXException e) { + LOG.debug(e); + throw new XPathException(e); + } catch (IOException e) { + LOG.debug(e); + throw new XPathException(e); + } + } } Modified: branches/shabanovd/xslt/src/org/exist/xslt/XSLContext.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/XSLContext.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/XSLContext.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -23,8 +23,10 @@ import org.exist.security.xacml.AccessContext; import org.exist.storage.DBBroker; +import org.exist.xquery.XPathException; import org.exist.xquery.XQueryContext; import org.exist.xquery.XQueryWatchDog; +import org.exist.xslt.functions.XSLTModule; /** * @author shabanovd @@ -42,6 +44,18 @@ private void init() { setWatchDog(new XQueryWatchDog(this)); + + //UNDERSTAND: what to do? + /* + try { + importModule(XSLTModule.NAMESPACE_URI, + XSLTModule.PREFIX, + "java:org.exist.xslt.functions.XSLTModule"); + } catch (XPathException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + */ } public void setXSLStylesheet(XSLStylesheet xsl) { Modified: branches/shabanovd/xslt/src/org/exist/xslt/XSLStylesheet.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/XSLStylesheet.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/XSLStylesheet.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -36,18 +36,22 @@ import org.exist.security.xacml.XACMLSource; import org.exist.xquery.CompiledXQuery; import org.exist.xquery.Expression; +import org.exist.xquery.Variable; import org.exist.xquery.XPathException; import org.exist.xquery.XQueryContext; import org.exist.xquery.value.Item; import org.exist.xquery.value.Sequence; import org.exist.xquery.value.SequenceIterator; import org.exist.xquery.value.ValueSequence; +import org.exist.dom.QName; import org.exist.dom.i.NamespaceNodeAtExist; import org.exist.dom.om.Validation; import org.exist.xslt.expression.AttributeSet; import org.exist.xslt.expression.Declaration; +import org.exist.xslt.expression.Param; import org.exist.xslt.expression.Template; import org.exist.xslt.expression.XSLExpression; +import org.exist.xslt.expression.i.Parameted; import org.w3c.dom.Attr; /** @@ -66,7 +70,8 @@ * @author shabanovd * */ -public class XSLStylesheet extends Declaration implements CompiledXQuery, Templates, XSLExpression { +public class XSLStylesheet extends Declaration + implements CompiledXQuery, Templates, XSLExpression, Parameted { public final double version = 2.0; @@ -83,6 +88,7 @@ private Map<String, AttributeSet> attributeSets = new HashMap<String, AttributeSet>(); + private Map<QName, org.exist.xquery.Variable> params = null; public XSLStylesheet(XSLContext context) { super(context); @@ -182,7 +188,7 @@ Template template = (Template) expr; if (template.isRootMatch()) { if (rootTemplate != null) - compileError("double root match"); + compileError("double root match");//XXX: put error code rootTemplate = template; } else templates.add(template); @@ -296,4 +302,25 @@ // // return result; // } + + public Map<QName, org.exist.xquery.Variable> getXSLParams() { + if (params == null) + params = new HashMap<QName, org.exist.xquery.Variable>(); + + return params; + } + + /* (non-Javadoc) + * @see org.exist.xslt.expression.i.Parameted#addXSLParam(org.exist.xslt.expression.Param) + */ + public void addXSLParam(Param param) throws XPathException { + Map<QName, org.exist.xquery.Variable> params = getXSLParams(); + + if (params.containsKey(param.getName())) + compileError(XSLExceptions.ERR_XTSE0580); + + Variable variable = context.declareVariable(param.getName(), param);//UNDERSTAND: global + params.put(param.getName(), variable); + } + } Modified: branches/shabanovd/xslt/src/org/exist/xslt/compiler/Element.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/compiler/Element.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/compiler/Element.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -23,6 +23,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.Map; import org.exist.xquery.AttributeConstructor; import org.exist.xquery.CDATAConstructor; @@ -69,10 +70,13 @@ return (XQueryContext) getDocument().getContext(); } + @SuppressWarnings("unchecked") protected XSLPathExpr getExpressionInstance() throws XPathException { if (expr != null) return expr; + System.out.println(getQName()); + Class<XSLPathExpr> clazz = Factory.qns.get(getQName()); try { @@ -101,12 +105,26 @@ XSLPathExpr exec = getExpressionInstance(); compileNode((PathExpr) exec, this); - exec.validate(); + exec.validate();//UNDERSTAND: at compile time??? analyze ??? return exec; } public void compileNode(PathExpr content, Node node) throws XPathException { + //namespaces + if (node instanceof ElementAtExist) { + ElementAtExist elementAtExist = (ElementAtExist) node; + Map<String, String> namespaceMap = elementAtExist.getNamespaceMap(); + for (String name : namespaceMap.keySet()) { + //getContext().declareInScopeNamespace(name, namespaceMap.get(name)); + getContext().declareNamespace(name, namespaceMap.get(name)); + //TODO: rewrite, changes at xquery.parser. it use static + } + } + + if (!node.hasChildNodes()) + return; + NodeConstructor constructer = null; NodeList children = node.getChildNodes(); @@ -185,6 +203,9 @@ } protected void preprocessNode(Node node) throws XPathException { + if (!node.hasChildNodes()) + return; + NodeList children = node.getChildNodes(); for (int i=0; i<children.getLength(); i++) { NodeAtExist child = (NodeAtExist)children.item(i); @@ -402,8 +423,7 @@ * @see org.w3c.dom.Node#hasChildNodes() */ public boolean hasChildNodes() { - // TODO Auto-generated method stub - throw new RuntimeException("Method is not implemented"); + return element.hasChildNodes(); } /* (non-Javadoc) @@ -718,4 +738,15 @@ return element.matchChildren(test); } + public String toString() { + return element.toString(); + } + + + /* (non-Javadoc) + * @see org.exist.dom.i.ElementAtExist#getNamespaceMap() + */ + public Map<String, String> getNamespaceMap() { + return element.getNamespaceMap(); + } } Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/ApplyTemplates.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/ApplyTemplates.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/ApplyTemplates.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -24,6 +24,7 @@ import java.util.Iterator; import org.exist.memtree.MemTreeBuilder; +import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.AnyNodeTest; import org.exist.xquery.Constants; import org.exist.xquery.Expression; @@ -35,6 +36,7 @@ import org.exist.xquery.value.Sequence; import org.exist.xquery.value.SequenceIterator; import org.exist.xquery.value.ValueSequence; +import org.exist.xslt.XSLContext; import org.exist.xslt.XSLStylesheet; import org.exist.xquery.XQueryContext; import org.exist.xslt.pattern.Pattern; @@ -55,12 +57,14 @@ */ public class ApplyTemplates extends SimpleConstructor { + private String attr_select = null; + private PathExpr select = null; private String mode = null; private static LocationStep anyChild; - public ApplyTemplates(XQueryContext context) { + public ApplyTemplates(XSLContext context) { super(context); anyChild = new LocationStep(getContext(), Constants.CHILD_AXIS, new AnyNodeTest()); @@ -74,13 +78,21 @@ public void prepareAttribute(Attr attr) throws XPathException { String attr_name = attr.getNodeName(); if (attr_name.equals(SELECT)) { - select = new PathExpr(getContext()); - Pattern.parse(getContext(), attr.getValue(), select); + attr_select = attr.getValue(); } else if (attr_name.equals(MODE)) { mode = attr.getValue(); } } + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + super.analyze(contextInfo); + + if (attr_select != null) { + select = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_select, select); + } + } + public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException { Sequence result = new ValueSequence(); Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/ForEach.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/ForEach.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/ForEach.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,6 +21,7 @@ */ package org.exist.xslt.expression; +import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.AnyNodeTest; import org.exist.xquery.Constants; import org.exist.xquery.LocationStep; @@ -47,6 +48,8 @@ */ public class ForEach extends SimpleConstructor { + private String attr_select = null; + private PathExpr select = null; private PathExpr childNodes = null; @@ -65,14 +68,19 @@ public void prepareAttribute(Attr attr) throws XPathException { String attr_name = attr.getNodeName(); if (attr_name.equals(SELECT)) { - select = new PathExpr(getContext()); - Pattern.parse(getContext(), attr.getValue(), select); - - //UNDERSTAND: salesdata/year -> child::salesdata/child::year, but need self::salesdata/child::year - _check_(select, false); + attr_select = attr.getValue(); } } + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + super.analyze(contextInfo); + + if (attr_select != null) { + select = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_select, select); + } + } + // protected void _check_(PathExpr path) { //// for (int pos = 0; pos < select.getLength(); pos++) { // Expression expr = select.getExpression(0);//pos Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/Param.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/Param.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/Param.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,11 +21,16 @@ */ package org.exist.xslt.expression; +import org.exist.dom.QName; +import org.exist.xquery.AnalyzeContextInfo; +import org.exist.xquery.PathExpr; import org.exist.xquery.XPathException; import org.exist.xquery.util.ExpressionDumper; import org.exist.xquery.value.Item; import org.exist.xquery.value.Sequence; import org.exist.xslt.XSLContext; +import org.exist.xslt.expression.i.Parameted; +import org.exist.xslt.pattern.Pattern; import org.w3c.dom.Attr; /** @@ -44,8 +49,10 @@ */ public class Param extends Declaration { - private String name = null; - private String select = null; + private String attr_select = null; + + private QName name = null; + private PathExpr select = null; private String as = null; private Boolean required = null; private Boolean tunnel = null; @@ -55,6 +62,8 @@ } public void setToDefaults() { + attr_select = null; + name = null; select = null; as = null; @@ -66,9 +75,9 @@ String attr_name = attr.getLocalName(); if (attr_name.equals(NAME)) { - name = attr.getValue(); + name = new QName(attr.getValue()); } else if (attr_name.equals(SELECT)) { - select = attr.getValue(); + attr_select = attr.getValue(); } else if (attr_name.equals(AS)) { as = attr.getValue(); } else if (attr_name.equals(REQUIRED)) { @@ -77,9 +86,28 @@ tunnel = getBoolean(attr.getValue()); } } + + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + if (contextInfo.getParent() instanceof Parameted) { + Parameted parameted = (Parameted) contextInfo.getParent(); + parameted.addXSLParam(this); + } else { + throw new XPathException("");//XXX: wrong parent + } + + super.analyze(contextInfo); + + if (attr_select != null) { + select = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_select, select); + + _check_(select); + + } + } public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException { - throw new RuntimeException("eval(Sequence contextSequence, Item contextItem) at "+this.getClass()); + return select.eval(contextSequence, contextItem); } /* (non-Javadoc) @@ -88,9 +116,9 @@ public void dump(ExpressionDumper dumper) { dumper.display("<xsl:param"); - if (name != null) { + if (getName() != null) { dumper.display(" name = "); - dumper.display(name); + dumper.display(getName()); } if (select != null) { dumper.display(" select = "); @@ -118,8 +146,8 @@ StringBuffer result = new StringBuffer(); result.append("<xsl:param"); - if (name != null) - result.append(" name = "+name.toString()); + if (getName() != null) + result.append(" name = "+getName().toString()); if (select != null) result.append(" select = "+select.toString()); if (as != null) @@ -135,5 +163,14 @@ result.append("</xsl:param> "); return result.toString(); - } + } + + + + /** + * @return the name + */ + public QName getName() { + return name; + } } Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/Template.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/Template.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/Template.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,11 +21,18 @@ */ package org.exist.xslt.expression; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.Expression; import org.exist.xquery.LocationStep; import org.exist.xquery.NodeTest; import org.exist.xquery.PathExpr; import org.exist.xquery.TypeTest; +import org.exist.xquery.Variable; import org.exist.xquery.XPathException; import org.exist.xquery.util.ExpressionDumper; import org.exist.xquery.value.Item; @@ -33,6 +40,7 @@ import org.exist.xquery.value.ValueSequence; import org.exist.xslt.XSLContext; import org.exist.dom.QName; +import org.exist.xslt.expression.i.Parameted; import org.exist.xslt.pattern.Pattern; import org.exist.xslt.XSLExceptions; import org.w3c.dom.Attr; @@ -52,7 +60,7 @@ * @author shabanovd * */ -public class Template extends Declaration implements Comparable<Template> { +public class Template extends Declaration implements Parameted, Comparable<Template> { private String attr_match = null; @@ -61,6 +69,8 @@ private QName[] mode = null; private Double priority = null; private String as = null; + + private Map<QName, org.exist.xquery.Variable> params = null; public Template(XSLContext context) { super(context); @@ -81,11 +91,6 @@ if (attr_name.equals(MATCH)) { attr_match = attr.getValue(); - match = new PathExpr(getContext()); - Pattern.parse(getContext(), attr_match, match); - - _check_(match); - } else if (attr_name.equals(NAME)) { name = new QName(attr.getValue()); } else if (attr_name.equals(PRIORITY)) { @@ -95,12 +100,24 @@ compileError(XSLExceptions.ERR_XTSE0530); } } else if (attr_name.equals(MODE)) { -// mode = attr.getValue(); +// mode = attr.getValue();//TODO: write } else if (attr_name.equals(AS)) { as = attr.getValue(); } } - + + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + super.analyze(contextInfo); + + if (attr_match != null) { + match = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_match, match); + + _check_(match); + + } + } + public void validate() throws XPathException { if (priority == null) priority = computedPriority(); @@ -267,4 +284,24 @@ public boolean isRootMatch() { return ("/".equals(attr_match)); } + + public Map<QName, org.exist.xquery.Variable> getXSLParams() { + if (params == null) + params = new HashMap<QName, org.exist.xquery.Variable>(); + + return params; + } + + /* (non-Javadoc) + * @see org.exist.xslt.expression.i.Parameted#addXSLParam(org.exist.xslt.expression.Param) + */ + public void addXSLParam(Param param) throws XPathException { + Map<QName, org.exist.xquery.Variable> params = getXSLParams(); + + if (params.containsKey(param.getName())) + compileError(XSLExceptions.ERR_XTSE0580); + + Variable variable = context.declareVariable(param.getName(), param); + params.put(param.getName(), variable); + } } Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/ValueOf.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/ValueOf.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/ValueOf.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -23,6 +23,7 @@ import org.exist.memtree.DocumentBuilderReceiver; import org.exist.memtree.MemTreeBuilder; +import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.Dependency; import org.exist.xquery.PathExpr; import org.exist.xquery.Profiler; @@ -51,6 +52,8 @@ */ public class ValueOf extends SimpleConstructor { + private String attr_select = null; + private PathExpr select = null; private String separator = null; private Boolean disable_output_escaping = null; @@ -72,18 +75,25 @@ public void prepareAttribute(Attr attr) throws XPathException { String attr_name = attr.getNodeName(); if (attr_name.equals(SELECT)) { - select = new PathExpr(getContext()); - Pattern.parse(getContext(), attr.getValue(), select); - - //UNDERSTAND: <node>text<node> step = "." -> SELF:node(), but need CHILD:node() - _check_(select, true); - + attr_select = attr.getValue(); } else if (attr_name.equals(SEPARATOR)) { separator = attr.getValue(); } else if (attr_name.equals(DISABLE_OUTPUT_ESCAPING)) { disable_output_escaping = getBoolean(attr.getValue()); } } + + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + super.analyze(contextInfo); + + if (attr_select != null) { + select = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_select, select); + + //UNDERSTAND: <node>text<node> step = "." -> SELF:node(), but need CHILD:node() + _check_(select, true); + } + } // protected void _check_(Expression path) { // for (int pos = 0; pos < path.getLength(); pos++) { Modified: branches/shabanovd/xslt/src/org/exist/xslt/expression/Variable.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/Variable.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/Variable.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -21,6 +21,8 @@ */ package org.exist.xslt.expression; +import org.exist.dom.QName; +import org.exist.xquery.AnalyzeContextInfo; import org.exist.xquery.PathExpr; import org.exist.xquery.XPathException; import org.exist.xquery.util.ExpressionDumper; @@ -43,33 +45,54 @@ * @author shabanovd * */ -public class Variable extends SimpleConstructor { +public class Variable extends XSLPathExpr { - private String name = null; + private String attr_select = null; + + private QName name = null; private PathExpr select = null; private String as = null; + private org.exist.xquery.Variable variable = null; + public Variable(XSLContext context) { super(context); } public void setToDefaults() { + attr_select = null; + name = null; select = null; as = null; + + variable = null; } public void prepareAttribute(Attr attr) throws XPathException { String attr_name = attr.getNodeName(); if (attr_name.equals(NAME)) { - name = attr.getValue(); + name = new QName(attr.getValue()); } else if (attr_name.equals(SELECT)) { - select = new PathExpr(getContext()); - Pattern.parse(getContext(), attr.getValue(), select); + attr_select = attr.getValue(); } else if (attr_name.equals(as)) { as = attr.getValue(); } } + + public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { + variable = context.declareVariable(name, this); + + super.analyze(contextInfo); + + if (attr_select != null) { + select = new PathExpr(getContext()); + Pattern.parse(getContext(), attr_select, select); + + _check_(select); + + } + } public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException { throw new RuntimeException("eval(Sequence contextSequence, Item contextItem) at "+this.getClass()); @@ -102,7 +125,7 @@ result.append("<xsl:variable"); if (name != null) - result.append(" name = "+name.toString()); + result.append(" name = "+name.getStringValue()); if (select != null) result.append(" select = "+select.toString()); if (as != null) Added: branches/shabanovd/xslt/src/org/exist/xslt/expression/i/Parameted.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/expression/i/Parameted.java (rev 0) +++ branches/shabanovd/xslt/src/org/exist/xslt/expression/i/Parameted.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -0,0 +1,40 @@ +/* + * eXist Open Source Native XML Database + * Copyright (C) 2001-2009 The eXist Project + * http://exist-db.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * $Id$ + */ + +package org.exist.xslt.expression.i; + +import java.util.Map; + +import org.exist.dom.QName; +import org.exist.xquery.XPathException; +import org.exist.xslt.expression.Param; + +/** + * @author <a href="mailto:sha...@gm...">Dmitriy Shabanov</a> + * + */ +public interface Parameted { + + public void addXSLParam(Param param) throws XPathException; + public Map<QName, org.exist.xquery.Variable> getXSLParams(); + +} Added: branches/shabanovd/xslt/src/org/exist/xslt/functions/Generate_id.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/functions/Generate_id.java (rev 0) +++ branches/shabanovd/xslt/src/org/exist/xslt/functions/Generate_id.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -0,0 +1,67 @@ +/* + * eXist Open Source Native XML Database + * Copyright (C) 2001-2009 The eXist Project + * http://exist-db.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * $Id$ + */ + +package org.exist.xslt.functions; + +import org.exist.dom.QName; +import org.exist.xquery.BasicFunction; +import org.exist.xquery.Cardinality; +import org.exist.xquery.FunctionSignature; +import org.exist.xquery.XPathException; +import org.exist.xquery.XQueryContext; +import org.exist.xquery.value.Sequence; +import org.exist.xquery.value.SequenceType; +import org.exist.xquery.value.Type; + +/** + * generate-id() as xs:string + * generate-id($node as node()?) as xs:string + * + * @author <a href="mailto:sha...@gm...">Dmitriy Shabanov</a> + * + */ +public class Generate_id extends BasicFunction { + + public final static FunctionSignature signatures[] = { + new FunctionSignature( + new QName("generate_id", XSLTModule.NAMESPACE_URI, XSLTModule.PREFIX), + "", + FunctionSignature.NO_ARGS, + new SequenceType(Type.ITEM, Cardinality.EXACTLY_ONE) + ) + }; + + /** + * @param context + */ + public Generate_id(XQueryContext context, FunctionSignature signature) { + super(context, signature); + } + + @Override + public Sequence eval(Sequence[] args, Sequence contextSequence) + throws XPathException { + // TODO Auto-generated method stub + throw new RuntimeException("Method is not implemented"); + } + +} Added: branches/shabanovd/xslt/src/org/exist/xslt/functions/XSLTModule.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/functions/XSLTModule.java (rev 0) +++ branches/shabanovd/xslt/src/org/exist/xslt/functions/XSLTModule.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -0,0 +1,72 @@ +/* + * eXist Open Source Native XML Database + * Copyright (C) 2001-2009 The eXist Project + * http://exist-db.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * $Id$ + */ + +package org.exist.xslt.functions; + +import org.exist.xquery.AbstractInternalModule; +import org.exist.xquery.FunctionDef; +import org.exist.xquery.modules.xslfo.RenderFunction; + +/** + * @author <a href="mailto:sha...@gm...">Dmitriy Shabanov</a> + * + */ +public class XSLTModule extends AbstractInternalModule { + + public final static String NAMESPACE_URI = "http://exist-db.org/xquery/xslt"; + public final static String PREFIX = "xslt"; + + private final static FunctionDef[] functions = { + new FunctionDef(RenderFunction.signatures[0], RenderFunction.class), + new FunctionDef(RenderFunction.signatures[1], RenderFunction.class) }; + + /** + * @param functions + */ + public XSLTModule() { + super(functions); + } + + /* (non-Javadoc) + * @see org.exist.xquery.AbstractInternalModule#getDefaultPrefix() + */ + @Override + public String getDefaultPrefix() { + return PREFIX; + } + + /* (non-Javadoc) + * @see org.exist.xquery.AbstractInternalModule#getNamespaceURI() + */ + @Override + public String getNamespaceURI() { + return NAMESPACE_URI; + } + + /* (non-Javadoc) + * @see org.exist.xquery.Module#getDescription() + */ + public String getDescription() { + return "XSLT Module"; + } + +} Modified: branches/shabanovd/xslt/src/org/exist/xslt/pattern/Pattern.java =================================================================== --- branches/shabanovd/xslt/src/org/exist/xslt/pattern/Pattern.java 2009-01-11 14:07:00 UTC (rev 8497) +++ branches/shabanovd/xslt/src/org/exist/xslt/pattern/Pattern.java 2009-01-11 18:59:37 UTC (rev 8498) @@ -38,6 +38,7 @@ import org.exist.xquery.parser.XQueryParser; import org.exist.xquery.parser.XQueryTreeParser; import org.exist.xquery.util.ExpressionDumper; +import org.exist.xslt.functions.Generate_id; import antlr.RecognitionException; import antlr.TokenStreamException; @@ -73,8 +74,14 @@ static final String ATTRIBUTE_A = "attribute(*)"; public static void parse(XQueryContext context, String pattern, PathExpr content) throws XPathException { - boolean xpointer = false; + boolean xpointer = false; + //TODO: add to antl + if (pattern.equals("generate-id()")) { + content.add(new Generate_id(context, Generate_id.signatures[0])); + return; + } + Source source = new StringSource(pattern); Reader reader; try { @@ -106,6 +113,8 @@ if (xpointer) treeParser.xpointer(ast, expr); else + System.out.println("************ pattern ************"); //TODO: remove + System.out.println(ast); treeParser.xpath(ast, expr); if (treeParser.foundErrors()) { throw new StaticXQueryException( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |