You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by jh...@apache.org on 2005/11/03 15:00:48 UTC
svn commit: r330548 [34/132] - in
/cocoon/whiteboard/maven2/cocoon-flat-layout: ./ cocoon-ajax-block/
cocoon-ajax-block/api/ cocoon-ajax-block/api/src/
cocoon-ajax-block/api/src/main/ cocoon-ajax-block/api/src/main/java/
cocoon-ajax-block/api/src/main/...
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableResolverFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableResolverFactory.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableResolverFactory.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableResolverFactory.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,114 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.treeprocessor.variables;
+
+import org.apache.avalon.framework.service.ServiceManager;
+
+import org.apache.cocoon.sitemap.PatternException;
+
+import java.util.List;
+
+/**
+ *
+ * @version CVS $Id: VariableResolverFactory.java 179045 2005-05-30 11:08:13Z cziegeler $
+ */
+public class VariableResolverFactory {
+
+ private static ThreadLocal disposableCollector = new ThreadLocal();
+
+ /**
+ * Set the thread-local list where all created resolvers that need to be
+ * disposed will be collected.
+ * <p>
+ * The purpose of collecting resolvers is to avoid manual release (or lack thereof)
+ * that requires most <code>ProcessingNodes</code> to implement <code>Disposable</code>.
+ */
+ public static void setDisposableCollector(List collector) {
+ disposableCollector.set(collector);
+ }
+
+ /**
+ * Does an expression need resolving (i.e. contain {...} patterns) ?
+ */
+ public static boolean needsResolve(String expression) {
+ if (expression == null || expression.length() == 0) {
+ return false;
+ }
+
+ // Is the first char a '{' ?
+ if (expression.charAt(0) == '{') {
+ return true;
+ }
+
+ if (expression.length() < 2) {
+ return false;
+ }
+
+ // Is there any unescaped '{' ?
+ int pos = 1;
+ while ( (pos = expression.indexOf('{', pos)) != -1) {
+ // Found a '{' : is it escaped ?
+ if (expression.charAt(pos - 1) != '\\') {
+ // No : need to resolve
+ return true;
+ }
+ pos++;
+ }
+ // Nothing found...
+ return false;
+ }
+
+ /**
+ * Unescape an expression that doesn't need to be resolved, but may contain
+ * escaped '{' characters.
+ *
+ * @param expression the expression to unescape.
+ * @return the unescaped result, or <code>expression</code> if unescaping isn't necessary.
+ */
+ public static String unescape(String expression) {
+ // Does it need escaping ?
+ if (expression == null || expression.indexOf("\\{") == -1) {
+ return expression;
+ }
+
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < expression.length(); i++) {
+ char ch = expression.charAt(i);
+ if (ch != '\\' || i >= (expression.length() - 1) || expression.charAt(i+1) != '{') {
+ buf.append(ch);
+ }
+ }
+
+ return buf.toString();
+ }
+
+ /**
+ * Get a resolver for a given expression. Chooses the most efficient implementation
+ * depending on <code>expression</code>.
+ */
+ public static VariableResolver getResolver(String expression, ServiceManager manager) throws PatternException {
+ if (needsResolve(expression)) {
+ VariableResolver resolver = new PreparedVariableResolver(expression, manager);
+ List collector = (List)disposableCollector.get();
+ if (collector != null)
+ collector.add(resolver);
+
+ return resolver;
+
+ }
+ return new NOPVariableResolver(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableResolverFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ElementPathPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ElementPathPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ElementPathPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ElementPathPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,258 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.apache.cocoon.xml.AbstractXMLPipe;
+import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.ProcessingException;
+
+import java.util.StringTokenizer;
+import java.util.ArrayList;
+import java.io.IOException;
+
+/**
+ * A custom XPointer scheme that allows to include the content of a specific element without
+ * building a DOM. The element must be specified using an absolute path reference such as
+ * <tt>/html/body</tt>. Namespace prefixes within these element names are supported.
+ *
+ * <p>This xpointer scheme will always be succesful (thus any further xpointer parts will
+ * never be executed).
+ *
+ * <p>The scheme name for this XPointer scheme is 'elementpath' and its namespace is
+ * http://apache.org/cocoon/xpointer.
+ *
+ * <p>See the samples for a usage example.
+ */
+public class ElementPathPart implements PointerPart {
+ private String expression;
+
+ public ElementPathPart(String expression) {
+ this.expression = expression;
+ }
+
+ public boolean process(XPointerContext xpointerContext) throws SAXException {
+ PathInclusionPipe pipe = new PathInclusionPipe(expression, xpointerContext);
+ pipe.setConsumer(xpointerContext.getXmlConsumer());
+ try {
+ SourceUtil.toSAX(xpointerContext.getSource(), pipe);
+ } catch (IOException e) {
+ throw new SAXException("Exception while trying to XInclude data: " + e.getMessage(), e);
+ } catch (ProcessingException e) {
+ throw new SAXException("Exception while trying to XInclude data: " + e.getMessage(), e);
+ }
+ return true;
+ }
+
+ public static class PathInclusionPipe extends AbstractXMLPipe {
+ /** The QNames that must be matched before inclusion can start. */
+ private QName[] elementPath;
+ /** The current element nesting level. */
+ private int level;
+ /** Should we currently be including? */
+ private boolean include;
+ /** The element nesting level since we started inclusion, used to know when to stop inclusion. */
+ private int includeLevel;
+
+ /** The element nesting level that should currently be matched. */
+ private int levelToMatch;
+ private boolean done;
+
+ public PathInclusionPipe(String expression, XPointerContext xpointerContext) throws SAXException {
+ // parse the expression to an array of QName objects
+ ArrayList path = new ArrayList();
+ StringTokenizer tokenizer = new StringTokenizer(expression, "/");
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken();
+ try {
+ path.add(QName.parse(token, xpointerContext));
+ } catch (SAXException e) {
+ throw new SAXException("Error in element path xpointer expression \"" + expression + "\": " + e.getMessage());
+ }
+ }
+ if (path.size() < 1)
+ throw new SAXException("Invalid element path xpointer expression \"" + expression + "\".");
+
+ this.elementPath = (QName[])path.toArray(new QName[0]);
+ this.level = -1;
+ this.include = false;
+ this.levelToMatch = 0;
+ this.done = false;
+ }
+
+ public void startElement(String namespaceURI, String localName, String raw, Attributes a)
+ throws SAXException {
+ level++;
+
+ if (include) {
+ super.startElement(namespaceURI, localName, raw, a);
+ return;
+ }
+
+ if (!done && level == levelToMatch && elementPath[level].matches(namespaceURI, localName)) {
+ levelToMatch++;
+ if (levelToMatch == elementPath.length) {
+ include = true;
+ done = true;
+ includeLevel = level;
+ }
+ }
+ }
+
+ public void endElement(String uri, String loc, String raw)
+ throws SAXException {
+ if (include && level == includeLevel)
+ include = false;
+
+ if (include)
+ super.endElement(uri, loc, raw);
+
+ level--;
+ }
+
+ public void setDocumentLocator(Locator locator) {
+ if (include)
+ super.setDocumentLocator(locator);
+ }
+
+ public void startDocument()
+ throws SAXException {
+ if (include)
+ super.startDocument();
+ }
+
+ public void endDocument()
+ throws SAXException {
+ if (include)
+ super.endDocument();
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException {
+ // let namespace prefix alway through
+ super.startPrefixMapping(prefix, uri);
+ }
+
+ public void endPrefixMapping(String prefix)
+ throws SAXException {
+ // let namespace prefix alway through
+ super.endPrefixMapping(prefix);
+ }
+
+ public void characters(char c[], int start, int len)
+ throws SAXException {
+ if (include)
+ super.characters(c, start, len);
+ }
+
+ public void ignorableWhitespace(char c[], int start, int len)
+ throws SAXException {
+ if (include)
+ super.ignorableWhitespace(c, start, len);
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException {
+ if (include)
+ super.processingInstruction(target, data);
+ }
+
+ public void skippedEntity(String name)
+ throws SAXException {
+ if (include)
+ super.skippedEntity(name);
+ }
+
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException {
+ if (include)
+ super.startDTD(name, publicId, systemId);
+ }
+
+ public void endDTD()
+ throws SAXException {
+ if (include)
+ super.endDTD();
+ }
+
+ public void startEntity(String name)
+ throws SAXException {
+ if (include)
+ super.startEntity(name);
+ }
+
+ public void endEntity(String name)
+ throws SAXException {
+ if (include)
+ super.endEntity(name);
+ }
+
+ public void startCDATA()
+ throws SAXException {
+ if (include)
+ super.startCDATA();
+ }
+
+ public void endCDATA()
+ throws SAXException {
+ if (include)
+ super.endCDATA();
+ }
+
+ public void comment(char ch[], int start, int len)
+ throws SAXException {
+ if (include)
+ super.comment(ch, start, len);
+ }
+
+ public static class QName {
+ private String namespaceURI;
+ private String localName;
+
+ public QName(String namespaceURI, String localName) {
+ this.namespaceURI = namespaceURI;
+ this.localName = localName;
+ }
+
+ public static QName parse(String qName, XPointerContext xpointerContext) throws SAXException {
+ int pos = qName.indexOf(':');
+ if (pos > 0) {
+ String prefix = qName.substring(0, pos);
+ String localName = qName.substring(pos + 1);
+ String namespaceURI = xpointerContext.prefixToNamespace(prefix);
+ if (namespaceURI == null)
+ throw new SAXException("Namespace prefix \"" + prefix + "\" not declared.");
+ return new QName(prefix, localName);
+ }
+ return new QName("", qName);
+ }
+
+ public String getNamespaceURI() {
+ return namespaceURI;
+ }
+
+ public String getLocalName() {
+ return localName;
+ }
+
+ public boolean matches(String namespaceURI, String localName) {
+ return this.localName.equals(localName) && this.namespaceURI.equals(namespaceURI);
+ }
+ }
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ElementPathPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/PointerPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/PointerPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/PointerPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/PointerPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,31 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.xml.sax.SAXException;
+import org.apache.cocoon.ResourceNotFoundException;
+
+/**
+ * Interface to be implemented by pointer parts (xpointer schemes).
+ */
+public interface PointerPart {
+ /**
+ * If this pointer part successfully identifies any subresources, it should
+ * stream them to the XMLConsumer available from the XPointerContext and return true.
+ * Otherwise this method should return false.
+ */
+ public boolean process(XPointerContext xpointerContext) throws SAXException, ResourceNotFoundException;
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/PointerPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ShorthandPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ShorthandPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ShorthandPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ShorthandPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,52 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.apache.cocoon.xml.dom.DOMStreamer;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/**
+ * Implements support for shorthand XPointers (= id-based lookup). We treat them here as if they
+ * were a pointerpart too.
+ *
+ * <p>Note that although this is implemented here, this feature depends on the presence of a DTD,
+ * and a validating parser. Currently, this means its unuseable within Cocoon.
+ */
+public class ShorthandPart implements PointerPart {
+ private String shorthand;
+
+ public ShorthandPart(String shorthand) {
+ this.shorthand = shorthand;
+ }
+
+ public boolean process(XPointerContext xpointerContext) throws SAXException, ResourceNotFoundException {
+ Document document = xpointerContext.getDocument();
+ Element element = document.getElementById(shorthand);
+ if (element != null) {
+ DOMStreamer streamer = new DOMStreamer();
+ streamer.setConsumer(xpointerContext.getXmlConsumer());
+ streamer.stream(element);
+ return true;
+ } else {
+ if (xpointerContext.getLogger().isDebugEnabled())
+ xpointerContext.getLogger().debug("XPointer: found no element with id " + shorthand + " in document " + xpointerContext.getSource().getURI());
+ }
+ return false;
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/ShorthandPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/UnsupportedPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/UnsupportedPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/UnsupportedPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/UnsupportedPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,30 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.xml.sax.SAXException;
+
+public class UnsupportedPart implements PointerPart {
+ private String schemeName;
+
+ public UnsupportedPart(String schemeName) {
+ this.schemeName = schemeName;
+ }
+
+ public boolean process(XPointerContext xpointerContext) throws SAXException {
+ throw new SAXException("Scheme " + schemeName + " not supported by this XPointer implementation, as used in the fragment identifier " + xpointerContext.getXPointer());
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/UnsupportedPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointer.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointer.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointer.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointer.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,47 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.xml.sax.SAXException;
+import org.apache.cocoon.ResourceNotFoundException;
+
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Iterator;
+
+/**
+ * Represents a fragment identifier conforming to the XML Pointer Language Framework.
+ * See also the specification at <a href="http://www.w3.org/TR/2003/REC-xptr-framework-20030325">
+ * http://www.w3.org/TR/2003/REC-xptr-framework-20030325</a>.
+ *
+ * <p>To create an instance of this class, call
+ * {@link org.apache.cocoon.components.xpointer.parser.XPointerFrameworkParser#parse(String)}.
+ */
+public class XPointer {
+ private List pointerParts = new LinkedList();
+
+ public void addPart(PointerPart part) {
+ pointerParts.add(part);
+ }
+
+ public void process(XPointerContext context) throws SAXException, ResourceNotFoundException {
+ Iterator pointerPartsIt = pointerParts.iterator();
+ while (pointerPartsIt.hasNext()) {
+ PointerPart part = (PointerPart)pointerPartsIt.next();
+ part.process(context);
+ }
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointer.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerContext.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerContext.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerContext.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerContext.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,109 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.w3c.dom.Document;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.xml.xpath.PrefixResolver;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.xml.sax.SAXException;
+
+import java.util.HashMap;
+
+/**
+ * A context object used during the evaluating of XPointers.
+ */
+public class XPointerContext implements PrefixResolver {
+ private Source source;
+ private Document document;
+ private XMLConsumer xmlConsumer;
+ private Logger logger;
+ private String xpointer;
+ private HashMap prefixes = new HashMap();
+ private ServiceManager manager;
+
+ /**
+ * Constructs an XPointerContext object.
+ *
+ * @param xpointer the original fragment identifier string, used for debugging purposes
+ * @param source the source into which the xpointer points
+ * @param xmlConsumer the consumer to which the result of the xpointer evaluation should be send
+ */
+ public XPointerContext(String xpointer, Source source, XMLConsumer xmlConsumer, Logger logger, ServiceManager manager) {
+ this.source = source;
+ this.xmlConsumer = xmlConsumer;
+ this.logger = logger;
+ this.manager = manager;
+ this.xpointer = xpointer;
+
+ prefixes.put("xml", "http://www.w3.org/XML/1998/namespace");
+ }
+
+ public Document getDocument() throws SAXException, ResourceNotFoundException {
+ if (document == null) {
+ try {
+ document = SourceUtil.toDOM(source);
+ } catch (ResourceNotFoundException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new SAXException("Error during XPointer evaluation while trying to load " + source.getURI(), e);
+ }
+ }
+ return document;
+ }
+
+ public Source getSource() {
+ return source;
+ }
+
+ public XMLConsumer getXmlConsumer() {
+ return xmlConsumer;
+ }
+
+ public Logger getLogger() {
+ return logger;
+ }
+
+ public String getXPointer() {
+ return xpointer;
+ }
+
+ public ServiceManager getServiceManager() {
+ return manager;
+ }
+
+ public void addPrefix(String prefix, String namespace) throws SAXException {
+ // according to the xmlns() scheme spec, these should not result to any change in namespace context
+ if (prefix.equalsIgnoreCase("xml"))
+ return;
+ else if (prefix.equals("xmlns"))
+ return;
+ else if (namespace.equals("http://www.w3.org/XML/1998/namespace"))
+ return;
+ else if (namespace.equals("http://www.w3.org/2000/xmlns/"))
+ return;
+
+ prefixes.put(prefix, namespace);
+ }
+
+ public String prefixToNamespace(String prefix) {
+ return (String)prefixes.get(prefix);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,70 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.xml.dom.DOMStreamer;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.excalibur.xml.xpath.XPathProcessor;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.LocatorImpl;
+
+/**
+ * Partly implementation of the xpointer() scheme. Only the XPath subset of xpointer is supported.
+ */
+public class XPointerPart implements PointerPart {
+ private String expression;
+
+ public XPointerPart(String expression) {
+ this.expression = expression;
+ }
+
+ public boolean process(XPointerContext xpointerContext) throws SAXException, ResourceNotFoundException {
+ Document document = xpointerContext.getDocument();
+ ServiceManager manager = xpointerContext.getServiceManager();
+ XPathProcessor xpathProcessor = null;
+ try {
+ try {
+ xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
+ } catch (Exception e) {
+ throw new SAXException("XPointerPart: error looking up XPathProcessor.", e);
+ }
+ NodeList nodeList = xpathProcessor.selectNodeList(document, expression, xpointerContext);
+ if (nodeList.getLength() > 0) {
+ XMLConsumer consumer = xpointerContext.getXmlConsumer();
+ LocatorImpl locator = new LocatorImpl();
+ locator.setSystemId(xpointerContext.getSource().getURI());
+ consumer.setDocumentLocator(locator);
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ DOMStreamer streamer = new DOMStreamer();
+ streamer.setConsumer(consumer);
+ streamer.stream(nodeList.item(i));
+ }
+ return true;
+ } else {
+ if (xpointerContext.getLogger().isDebugEnabled())
+ xpointerContext.getLogger().debug("XPointer: expression \"" + expression + "\" gave no results.");
+ return false;
+ }
+ } finally {
+ if (xpathProcessor != null)
+ manager.release(xpathProcessor);
+ }
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XPointerPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XmlnsPart.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XmlnsPart.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XmlnsPart.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XmlnsPart.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,40 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer;
+
+import org.xml.sax.SAXException;
+
+/**
+ * Implements support for the XPointer xmlns() Scheme.
+ * See also <a href="http://www.w3.org/TR/xptr-xmlns/">http://www.w3.org/TR/xptr-xmlns/</a>.
+ */
+public class XmlnsPart implements PointerPart {
+ private String prefix;
+ private String namespace;
+
+ /**
+ * Creates an XmlnsPart.
+ */
+ public XmlnsPart(String prefix, String namespace) {
+ this.prefix = prefix;
+ this.namespace = namespace;
+ }
+
+ public boolean process(XPointerContext xpointerContext) throws SAXException {
+ xpointerContext.addPrefix(prefix, namespace);
+ return false;
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/XmlnsPart.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/ParseException.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/ParseException.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/ParseException.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/ParseException.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,210 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer.parser;
+
+/**
+ * This exception is thrown when parse errors are encountered.
+ * You can explicitly create objects of this exception type by
+ * calling the method generateParseException in the generated
+ * parser.
+ *
+ * You can modify this class to customize your error reporting
+ * mechanisms so long as you retain the public fields.
+ */
+public class ParseException extends Exception {
+
+ /**
+ * This constructor is used by the method "generateParseException"
+ * in the generated parser. Calling this constructor generates
+ * a new object of this type with the fields "currentToken",
+ * "expectedTokenSequences", and "tokenImage" set. The boolean
+ * flag "specialConstructor" is also set to true to indicate that
+ * this constructor was used to create this object.
+ * This constructor calls its super class with the empty string
+ * to force the "toString" method of parent class "Throwable" to
+ * print the error message in the form:
+ * ParseException: <result of getMessage>
+ */
+ public ParseException(
+ Token currentTokenVal,
+ int[][] expectedTokenSequencesVal,
+ String[] tokenImageVal) {
+ super("");
+ specialConstructor = true;
+ currentToken = currentTokenVal;
+ expectedTokenSequences = expectedTokenSequencesVal;
+ tokenImage = tokenImageVal;
+ }
+
+ /**
+ * The following constructors are for use by you for whatever
+ * purpose you can think of. Constructing the exception in this
+ * manner makes the exception behave in the normal way - i.e., as
+ * documented in the class "Throwable". The fields "errorToken",
+ * "expectedTokenSequences", and "tokenImage" do not contain
+ * relevant information. The JavaCC generated code does not use
+ * these constructors.
+ */
+
+ public ParseException() {
+ super();
+ specialConstructor = false;
+ }
+
+ public ParseException(String message) {
+ super(message);
+ specialConstructor = false;
+ }
+
+ /**
+ * This variable determines which constructor was used to create
+ * this object and thereby affects the semantics of the
+ * "getMessage" method (see below).
+ */
+ protected boolean specialConstructor;
+
+ /**
+ * This is the last token that has been consumed successfully. If
+ * this object has been created due to a parse error, the token
+ * followng this token will (therefore) be the first error token.
+ */
+ public Token currentToken;
+
+ /**
+ * Each entry in this array is an array of integers. Each array
+ * of integers represents a sequence of tokens (by their ordinal
+ * values) that is expected at this point of the parse.
+ */
+ public int[][] expectedTokenSequences;
+
+ /**
+ * This is a reference to the "tokenImage" array of the generated
+ * parser within which the parse error occurred. This array is
+ * defined in the generated ...Constants interface.
+ */
+ public String[] tokenImage;
+
+ /**
+ * This method has the standard behavior when this object has been
+ * created using the standard constructors. Otherwise, it uses
+ * "currentToken" and "expectedTokenSequences" to generate a parse
+ * error message and returns it. If this object has been created
+ * due to a parse error, and you do not catch it (it gets thrown
+ * from the parser), then this method is called during the printing
+ * of the final stack trace, and hence the correct error message
+ * gets displayed.
+ */
+ public String getMessage() {
+ if (!specialConstructor) {
+ return super.getMessage();
+ }
+ String expected = "";
+ int maxSize = 0;
+ for (int i = 0; i < expectedTokenSequences.length; i++) {
+ if (maxSize < expectedTokenSequences[i].length) {
+ maxSize = expectedTokenSequences[i].length;
+ }
+ for (int j = 0; j < expectedTokenSequences[i].length; j++) {
+ expected += tokenImage[expectedTokenSequences[i][j]] + " ";
+ }
+ if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1]
+ != 0) {
+ expected += "...";
+ }
+ expected += eol + " ";
+ }
+ String retval = "Encountered \"";
+ Token tok = currentToken.next;
+ for (int i = 0; i < maxSize; i++) {
+ if (i != 0)
+ retval += " ";
+ if (tok.kind == 0) {
+ retval += tokenImage[0];
+ break;
+ }
+ retval += add_escapes(tok.image);
+ tok = tok.next;
+ }
+ retval += "\" at line "
+ + currentToken.next.beginLine
+ + ", column "
+ + currentToken.next.beginColumn;
+ retval += "." + eol;
+ if (expectedTokenSequences.length == 1) {
+ retval += "Was expecting:" + eol + " ";
+ } else {
+ retval += "Was expecting one of:" + eol + " ";
+ }
+ retval += expected;
+ return retval;
+ }
+
+ /**
+ * The end of line string for this machine.
+ */
+ protected String eol = System.getProperty("line.separator", "\n");
+
+ /**
+ * Used to convert raw characters to their escaped version
+ * when these raw version cannot be used as part of an ASCII
+ * string literal.
+ */
+ protected String add_escapes(String str) {
+ StringBuffer retval = new StringBuffer();
+ char ch;
+ for (int i = 0; i < str.length(); i++) {
+ switch (str.charAt(i)) {
+ case 0 :
+ continue;
+ case '\b' :
+ retval.append("\\b");
+ continue;
+ case '\t' :
+ retval.append("\\t");
+ continue;
+ case '\n' :
+ retval.append("\\n");
+ continue;
+ case '\f' :
+ retval.append("\\f");
+ continue;
+ case '\r' :
+ retval.append("\\r");
+ continue;
+ case '\"' :
+ retval.append("\\\"");
+ continue;
+ case '\'' :
+ retval.append("\\\'");
+ continue;
+ case '\\' :
+ retval.append("\\\\");
+ continue;
+ default :
+ if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
+ String s = "0000" + Integer.toString(ch, 16);
+ retval.append(
+ "\\u" + s.substring(s.length() - 4, s.length()));
+ } else {
+ retval.append(ch);
+ }
+ continue;
+ }
+ }
+ return retval.toString();
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/ParseException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/SimpleCharStream.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/SimpleCharStream.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/SimpleCharStream.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/SimpleCharStream.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,425 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer.parser;
+
+/**
+ * An implementation of interface CharStream, where the stream is assumed to
+ * contain only ASCII characters (without unicode processing).
+ */
+
+public class SimpleCharStream {
+ public static final boolean staticFlag = false;
+ int bufsize;
+ int available;
+ int tokenBegin;
+ public int bufpos = -1;
+ protected int bufline[];
+ protected int bufcolumn[];
+
+ protected int column = 0;
+ protected int line = 1;
+
+ protected boolean prevCharIsCR = false;
+ protected boolean prevCharIsLF = false;
+
+ protected java.io.Reader inputStream;
+
+ protected char[] buffer;
+ protected int maxNextCharInd = 0;
+ protected int inBuf = 0;
+
+ protected void ExpandBuff(boolean wrapAround) {
+ char[] newbuffer = new char[bufsize + 2048];
+ int newbufline[] = new int[bufsize + 2048];
+ int newbufcolumn[] = new int[bufsize + 2048];
+
+ try {
+ if (wrapAround) {
+ System.arraycopy(
+ buffer,
+ tokenBegin,
+ newbuffer,
+ 0,
+ bufsize - tokenBegin);
+ System.arraycopy(
+ buffer,
+ 0,
+ newbuffer,
+ bufsize - tokenBegin,
+ bufpos);
+ buffer = newbuffer;
+
+ System.arraycopy(
+ bufline,
+ tokenBegin,
+ newbufline,
+ 0,
+ bufsize - tokenBegin);
+ System.arraycopy(
+ bufline,
+ 0,
+ newbufline,
+ bufsize - tokenBegin,
+ bufpos);
+ bufline = newbufline;
+
+ System.arraycopy(
+ bufcolumn,
+ tokenBegin,
+ newbufcolumn,
+ 0,
+ bufsize - tokenBegin);
+ System.arraycopy(
+ bufcolumn,
+ 0,
+ newbufcolumn,
+ bufsize - tokenBegin,
+ bufpos);
+ bufcolumn = newbufcolumn;
+
+ maxNextCharInd = (bufpos += (bufsize - tokenBegin));
+ } else {
+ System.arraycopy(
+ buffer,
+ tokenBegin,
+ newbuffer,
+ 0,
+ bufsize - tokenBegin);
+ buffer = newbuffer;
+
+ System.arraycopy(
+ bufline,
+ tokenBegin,
+ newbufline,
+ 0,
+ bufsize - tokenBegin);
+ bufline = newbufline;
+
+ System.arraycopy(
+ bufcolumn,
+ tokenBegin,
+ newbufcolumn,
+ 0,
+ bufsize - tokenBegin);
+ bufcolumn = newbufcolumn;
+
+ maxNextCharInd = (bufpos -= tokenBegin);
+ }
+ } catch (Throwable t) {
+ throw new Error(t.getMessage());
+ }
+
+ bufsize += 2048;
+ available = bufsize;
+ tokenBegin = 0;
+ }
+
+ protected void FillBuff() throws java.io.IOException {
+ if (maxNextCharInd == available) {
+ if (available == bufsize) {
+ if (tokenBegin > 2048) {
+ bufpos = maxNextCharInd = 0;
+ available = tokenBegin;
+ } else if (tokenBegin < 0)
+ bufpos = maxNextCharInd = 0;
+ else
+ ExpandBuff(false);
+ } else if (available > tokenBegin)
+ available = bufsize;
+ else if ((tokenBegin - available) < 2048)
+ ExpandBuff(true);
+ else
+ available = tokenBegin;
+ }
+
+ int i;
+ try {
+ if ((i =
+ inputStream.read(
+ buffer,
+ maxNextCharInd,
+ available - maxNextCharInd))
+ == -1) {
+ inputStream.close();
+ throw new java.io.IOException();
+ } else
+ maxNextCharInd += i;
+ return;
+ } catch (java.io.IOException e) {
+ --bufpos;
+ backup(0);
+ if (tokenBegin == -1)
+ tokenBegin = bufpos;
+ throw e;
+ }
+ }
+
+ public char BeginToken() throws java.io.IOException {
+ tokenBegin = -1;
+ char c = readChar();
+ tokenBegin = bufpos;
+
+ return c;
+ }
+
+ protected void UpdateLineColumn(char c) {
+ column++;
+
+ if (prevCharIsLF) {
+ prevCharIsLF = false;
+ line += (column = 1);
+ } else if (prevCharIsCR) {
+ prevCharIsCR = false;
+ if (c == '\n') {
+ prevCharIsLF = true;
+ } else
+ line += (column = 1);
+ }
+
+ switch (c) {
+ case '\r' :
+ prevCharIsCR = true;
+ break;
+ case '\n' :
+ prevCharIsLF = true;
+ break;
+ case '\t' :
+ column--;
+ column += (8 - (column & 07));
+ break;
+ default :
+ break;
+ }
+
+ bufline[bufpos] = line;
+ bufcolumn[bufpos] = column;
+ }
+
+ public char readChar() throws java.io.IOException {
+ if (inBuf > 0) {
+ --inBuf;
+
+ if (++bufpos == bufsize)
+ bufpos = 0;
+
+ return buffer[bufpos];
+ }
+
+ if (++bufpos >= maxNextCharInd)
+ FillBuff();
+
+ char c = buffer[bufpos];
+
+ UpdateLineColumn(c);
+ return (c);
+ }
+
+ public int getEndColumn() {
+ return bufcolumn[bufpos];
+ }
+
+ public int getEndLine() {
+ return bufline[bufpos];
+ }
+
+ public int getBeginColumn() {
+ return bufcolumn[tokenBegin];
+ }
+
+ public int getBeginLine() {
+ return bufline[tokenBegin];
+ }
+
+ public void backup(int amount) {
+
+ inBuf += amount;
+ if ((bufpos -= amount) < 0)
+ bufpos += bufsize;
+ }
+
+ public SimpleCharStream(
+ java.io.Reader dstream,
+ int startline,
+ int startcolumn,
+ int buffersize) {
+ inputStream = dstream;
+ line = startline;
+ column = startcolumn - 1;
+
+ available = bufsize = buffersize;
+ buffer = new char[buffersize];
+ bufline = new int[buffersize];
+ bufcolumn = new int[buffersize];
+ }
+
+ public SimpleCharStream(
+ java.io.Reader dstream,
+ int startline,
+ int startcolumn) {
+ this(dstream, startline, startcolumn, 4096);
+ }
+
+ public SimpleCharStream(java.io.Reader dstream) {
+ this(dstream, 1, 1, 4096);
+ }
+ public void ReInit(
+ java.io.Reader dstream,
+ int startline,
+ int startcolumn,
+ int buffersize) {
+ inputStream = dstream;
+ line = startline;
+ column = startcolumn - 1;
+
+ if (buffer == null || buffersize != buffer.length) {
+ available = bufsize = buffersize;
+ buffer = new char[buffersize];
+ bufline = new int[buffersize];
+ bufcolumn = new int[buffersize];
+ }
+ prevCharIsLF = prevCharIsCR = false;
+ tokenBegin = inBuf = maxNextCharInd = 0;
+ bufpos = -1;
+ }
+
+ public void ReInit(
+ java.io.Reader dstream,
+ int startline,
+ int startcolumn) {
+ ReInit(dstream, startline, startcolumn, 4096);
+ }
+
+ public void ReInit(java.io.Reader dstream) {
+ ReInit(dstream, 1, 1, 4096);
+ }
+ public SimpleCharStream(
+ java.io.InputStream dstream,
+ int startline,
+ int startcolumn,
+ int buffersize) {
+ this(
+ new java.io.InputStreamReader(dstream),
+ startline,
+ startcolumn,
+ buffersize);
+ }
+
+ public SimpleCharStream(
+ java.io.InputStream dstream,
+ int startline,
+ int startcolumn) {
+ this(dstream, startline, startcolumn, 4096);
+ }
+
+ public SimpleCharStream(java.io.InputStream dstream) {
+ this(dstream, 1, 1, 4096);
+ }
+
+ public void ReInit(
+ java.io.InputStream dstream,
+ int startline,
+ int startcolumn,
+ int buffersize) {
+ ReInit(
+ new java.io.InputStreamReader(dstream),
+ startline,
+ startcolumn,
+ buffersize);
+ }
+
+ public void ReInit(java.io.InputStream dstream) {
+ ReInit(dstream, 1, 1, 4096);
+ }
+ public void ReInit(
+ java.io.InputStream dstream,
+ int startline,
+ int startcolumn) {
+ ReInit(dstream, startline, startcolumn, 4096);
+ }
+ public String GetImage() {
+ if (bufpos >= tokenBegin)
+ return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
+ else
+ return new String(buffer, tokenBegin, bufsize - tokenBegin)
+ + new String(buffer, 0, bufpos + 1);
+ }
+
+ public char[] GetSuffix(int len) {
+ char[] ret = new char[len];
+
+ if ((bufpos + 1) >= len)
+ System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
+ else {
+ System.arraycopy(
+ buffer,
+ bufsize - (len - bufpos - 1),
+ ret,
+ 0,
+ len - bufpos - 1);
+ System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
+ }
+
+ return ret;
+ }
+
+ public void Done() {
+ buffer = null;
+ bufline = null;
+ bufcolumn = null;
+ }
+
+ /**
+ * Method to adjust line and column numbers for the start of a token.<BR>
+ */
+ public void adjustBeginLineColumn(int newLine, int newCol) {
+ int start = tokenBegin;
+ int len;
+
+ if (bufpos >= tokenBegin) {
+ len = bufpos - tokenBegin + inBuf + 1;
+ } else {
+ len = bufsize - tokenBegin + bufpos + 1 + inBuf;
+ }
+
+ int i = 0, j = 0, k = 0;
+ int nextColDiff = 0, columnDiff = 0;
+
+ while (i < len && bufline[j =
+ start % bufsize] == bufline[k = ++start % bufsize]) {
+ bufline[j] = newLine;
+ nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
+ bufcolumn[j] = newCol + columnDiff;
+ columnDiff = nextColDiff;
+ i++;
+ }
+
+ if (i < len) {
+ bufline[j] = newLine++;
+ bufcolumn[j] = newCol + columnDiff;
+
+ while (i++ < len) {
+ if (bufline[j = start % bufsize] != bufline[++start % bufsize])
+ bufline[j] = newLine++;
+ else
+ bufline[j] = newLine;
+ }
+ }
+
+ line = bufline[j];
+ column = bufcolumn[j];
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/SimpleCharStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/Token.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/Token.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/Token.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/Token.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,93 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer.parser;
+
+/**
+ * Describes the input token stream.
+ */
+
+public class Token {
+
+ /**
+ * An integer that describes the kind of this token. This numbering
+ * system is determined by JavaCCParser, and a table of these numbers is
+ * stored in the file ...Constants.java.
+ */
+ public int kind;
+
+ /**
+ * beginLine and beginColumn describe the position of the first character
+ * of this token; endLine and endColumn describe the position of the
+ * last character of this token.
+ */
+ public int beginLine, beginColumn, endLine, endColumn;
+
+ /**
+ * The string image of the token.
+ */
+ public String image;
+
+ /**
+ * A reference to the next regular (non-special) token from the input
+ * stream. If this is the last token from the input stream, or if the
+ * token manager has not read tokens beyond this one, this field is
+ * set to null. This is true only if this token is also a regular
+ * token. Otherwise, see below for a description of the contents of
+ * this field.
+ */
+ public Token next;
+
+ /**
+ * This field is used to access special tokens that occur prior to this
+ * token, but after the immediately preceding regular (non-special) token.
+ * If there are no such special tokens, this field is set to null.
+ * When there are more than one such special token, this field refers
+ * to the last of these special tokens, which in turn refers to the next
+ * previous special token through its specialToken field, and so on
+ * until the first special token (whose specialToken field is null).
+ * The next fields of special tokens refer to other special tokens that
+ * immediately follow it (without an intervening regular token). If there
+ * is no such token, this field is null.
+ */
+ public Token specialToken;
+
+ /**
+ * Returns the image.
+ */
+ public String toString() {
+ return image;
+ }
+
+ /**
+ * Returns a new Token object, by default. However, if you want, you
+ * can create and return subclass objects based on the value of ofKind.
+ * Simply add the cases to the switch for all those special cases.
+ * For example, if you have a subclass of Token called IDToken that
+ * you want to create if ofKind is ID, simlpy add something like :
+ *
+ * case MyParserConstants.ID : return new IDToken();
+ *
+ * to the following switch statement. Then you can cast matchedToken
+ * variable to the appropriate type and use it in your lexical actions.
+ */
+ public static final Token newToken(int ofKind) {
+ switch (ofKind) {
+ default :
+ return new Token();
+ }
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/Token.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/TokenMgrError.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/TokenMgrError.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/TokenMgrError.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/TokenMgrError.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,177 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.xpointer.parser;
+
+public class TokenMgrError extends Error {
+ /*
+ * Ordinals for various reasons why an Error of this type can be thrown.
+ */
+
+ /**
+ * Lexical error occured.
+ */
+ static final int LEXICAL_ERROR = 0;
+
+ /**
+ * An attempt wass made to create a second instance of a static token manager.
+ */
+ static final int STATIC_LEXER_ERROR = 1;
+
+ /**
+ * Tried to change to an invalid lexical state.
+ */
+ static final int INVALID_LEXICAL_STATE = 2;
+
+ /**
+ * Detected (and bailed out of) an infinite loop in the token manager.
+ */
+ static final int LOOP_DETECTED = 3;
+
+ /**
+ * Indicates the reason why the exception is thrown. It will have
+ * one of the above 4 values.
+ */
+ int errorCode;
+
+ /**
+ * Replaces unprintable characters by their espaced (or unicode escaped)
+ * equivalents in the given string
+ */
+ protected static final String addEscapes(String str) {
+ StringBuffer retval = new StringBuffer();
+ char ch;
+ for (int i = 0; i < str.length(); i++) {
+ switch (str.charAt(i)) {
+ case 0 :
+ continue;
+ case '\b' :
+ retval.append("\\b");
+ continue;
+ case '\t' :
+ retval.append("\\t");
+ continue;
+ case '\n' :
+ retval.append("\\n");
+ continue;
+ case '\f' :
+ retval.append("\\f");
+ continue;
+ case '\r' :
+ retval.append("\\r");
+ continue;
+ case '\"' :
+ retval.append("\\\"");
+ continue;
+ case '\'' :
+ retval.append("\\\'");
+ continue;
+ case '\\' :
+ retval.append("\\\\");
+ continue;
+ default :
+ if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
+ String s = "0000" + Integer.toString(ch, 16);
+ retval.append(
+ "\\u" + s.substring(s.length() - 4, s.length()));
+ } else {
+ retval.append(ch);
+ }
+ continue;
+ }
+ }
+ return retval.toString();
+ }
+
+ /**
+ * Returns a detailed message for the Error when it is thrown by the
+ * token manager to indicate a lexical error.
+ * Parameters :
+ * EOFSeen : indicates if EOF caused the lexicl error
+ * curLexState : lexical state in which this error occured
+ * errorLine : line number when the error occured
+ * errorColumn : column number when the error occured
+ * errorAfter : prefix that was seen before this error occured
+ * curchar : the offending character
+ * Note: You can customize the lexical error message by modifying this method.
+ */
+ protected static String LexicalError(
+ boolean EOFSeen,
+ int lexState,
+ int errorLine,
+ int errorColumn,
+ String errorAfter,
+ char curChar) {
+ return (
+ "Lexical error at line "
+ + errorLine
+ + ", column "
+ + errorColumn
+ + ". Encountered: "
+ + (EOFSeen
+ ? "<EOF> "
+ : ("\"" + addEscapes(String.valueOf(curChar)) + "\"")
+ + " ("
+ + (int) curChar
+ + "), ")
+ + "after : \""
+ + addEscapes(errorAfter)
+ + "\"");
+ }
+
+ /**
+ * You can also modify the body of this method to customize your error messages.
+ * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
+ * of end-users concern, so you can return something like :
+ *
+ * "Internal Error : Please file a bug report .... "
+ *
+ * from this method for such cases in the release version of your parser.
+ */
+ public String getMessage() {
+ return super.getMessage();
+ }
+
+ /*
+ * Constructors of various flavors follow.
+ */
+
+ public TokenMgrError() {
+ }
+
+ public TokenMgrError(String message, int reason) {
+ super(message);
+ errorCode = reason;
+ }
+
+ public TokenMgrError(
+ boolean EOFSeen,
+ int lexState,
+ int errorLine,
+ int errorColumn,
+ String errorAfter,
+ char curChar,
+ int reason) {
+ this(
+ LexicalError(
+ EOFSeen,
+ lexState,
+ errorLine,
+ errorColumn,
+ errorAfter,
+ curChar),
+ reason);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/components/xpointer/parser/TokenMgrError.java
------------------------------------------------------------------------------
svn:eol-style = native