diff options
author | Jeremias Maerki <jeremias@apache.org> | 2008-08-12 14:44:55 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2008-08-12 14:44:55 +0000 |
commit | e714868660adf4707400d5a6d7b1bd9da0db2e1a (patch) | |
tree | 967c2bee385a1f6ab6cafd2fe0f280d80b6f1e4f /src/java/org/apache/fop/render/intermediate/extensions | |
parent | 8454b389c6655242c35359f9db76e75ee2910cca (diff) | |
download | xmlgraphics-fop-e714868660adf4707400d5a6d7b1bd9da0db2e1a.tar.gz xmlgraphics-fop-e714868660adf4707400d5a6d7b1bd9da0db2e1a.zip |
Added support for bookmarks to intermediate format (including PDF painter).
Added infrastructure for IF testing similar to AT testing with XPaths. To minimize the additional processing needed it is attached to the layout engine tests and only executed if there are IF checks in the test case.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@685170 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/intermediate/extensions')
7 files changed, 589 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/render/intermediate/extensions/AbstractAction.java b/src/java/org/apache/fop/render/intermediate/extensions/AbstractAction.java new file mode 100644 index 000000000..37a3032cd --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/AbstractAction.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import org.apache.xmlgraphics.util.XMLizable; + +/** + * Abstract base class for document actions, like "go-to" actions with absolute page coordinates. + */ +public abstract class AbstractAction implements XMLizable { + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/Bookmark.java b/src/java/org/apache/fop/render/intermediate/extensions/Bookmark.java new file mode 100644 index 000000000..a96bbd0ff --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/Bookmark.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.apache.xmlgraphics.util.XMLizable; + +import org.apache.fop.util.XMLUtil; + +/** + * This class is a bookmark element for use in the intermediate format. + */ +public class Bookmark implements XMLizable, BookmarkExtensionConstants { + + private String title; + private boolean show; + private List childBookmarks; + private AbstractAction action; + + /** + * Creates a new bookmark. + * @param title the bookmark's title + * @param show true if the bookmark shall be shown, false for hidden + * @param action the action performed when the bookmark is clicked + */ + public Bookmark(String title, boolean show, AbstractAction action) { + this.title = title; + this.show = show; + this.action = action; + } + + /** + * Returns the bookmark's title. + * @return the title + */ + public String getTitle() { + return this.title; + } + + /** + * Indicates whether the bookmark shall be shown initially. + * @return true if it shall be shown + */ + public boolean isShown() { + return this.show; + } + + /** + * Returns the action performed when the bookmark is clicked. + * @return the action + */ + public AbstractAction getAction() { + return this.action; + } + + /** + * Sets the action performed when the bookmark is clicked. + * @param action the action + */ + public void setAction(AbstractAction action) { + this.action = action; + } + + /** + * Adds a child bookmark. + * @param bookmark the child bookmark + */ + public void addChildBookmark(Bookmark bookmark) { + if (this.childBookmarks == null) { + this.childBookmarks = new java.util.ArrayList(); + } + this.childBookmarks.add(bookmark); + } + + /** + * Returns a list of child bookmarks. + * @return the child bookmarks + */ + public List getChildBookmarks() { + if (this.childBookmarks == null) { + return Collections.EMPTY_LIST; + } else { + return Collections.unmodifiableList(this.childBookmarks); + } + } + + /** {@inheritDoc} */ + public void toSAX(ContentHandler handler) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute(null, "title", "title", XMLUtil.CDATA, getTitle()); + atts.addAttribute(null, "starting-state", "starting-state", + XMLUtil.CDATA, isShown() ? "show" : "hide"); + handler.startElement(BOOKMARK.getNamespaceURI(), + BOOKMARK.getLocalName(), BOOKMARK.getQName(), atts); + if (getAction() != null) { + getAction().toSAX(handler); + } + if (this.childBookmarks != null) { + Iterator iter = this.childBookmarks.iterator(); + while (iter.hasNext()) { + Bookmark b = (Bookmark)iter.next(); + b.toSAX(handler); + } + } + handler.endElement(BOOKMARK.getNamespaceURI(), + BOOKMARK.getLocalName(), BOOKMARK.getQName()); + } + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionConstants.java b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionConstants.java new file mode 100644 index 000000000..184c71dbe --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionConstants.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import org.apache.xmlgraphics.util.QName; + +import org.apache.fop.render.intermediate.IFConstants; + +/** + * Constants for the IF bookmark extension. + */ +public interface BookmarkExtensionConstants { + + /** Namespace URI for the bookmark extension */ + String NAMESPACE = IFConstants.NAMESPACE + "/bookmarks"; + + /** the bookmark-tree element */ + QName BOOKMARK_TREE = new QName(NAMESPACE, "bookmark-tree"); + /** the bookmark element */ + QName BOOKMARK = new QName(NAMESPACE, "bookmark"); + /** the goto-xy element */ + QName GOTO_XY = new QName(NAMESPACE, "goto-xy"); + /** the goto-uri element */ + QName GOTO_URI = new QName(NAMESPACE, "goto-uri"); + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionHandlerFactory.java b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionHandlerFactory.java new file mode 100644 index 000000000..ac3f9563a --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkExtensionHandlerFactory.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import java.awt.Point; +import java.util.Stack; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.fop.render.ps.extensions.PSExtensionAttachment; +import org.apache.fop.util.ContentHandlerFactory; +import org.apache.fop.util.XMLUtil; + +/** + * Factory for the ContentHandler that handles the IF bookmarks namespace. + */ +public class BookmarkExtensionHandlerFactory + implements ContentHandlerFactory, BookmarkExtensionConstants { + + /** Logger instance */ + protected static Log log = LogFactory.getLog(BookmarkExtensionHandlerFactory.class); + + /** {@inheritDoc} */ + public String[] getSupportedNamespaces() { + return new String[] {NAMESPACE}; + } + + /** {@inheritDoc} */ + public ContentHandler createContentHandler() { + return new BookmarkExtensionHandler(); + } + + private static class BookmarkExtensionHandler extends DefaultHandler + implements ContentHandlerFactory.ObjectSource { + + private StringBuffer content = new StringBuffer(); + //private Attributes lastAttributes; + private Stack objectStack = new Stack(); + private BookmarkTree bookmarkTree; + + private ObjectBuiltListener listener; + + /** {@inheritDoc} */ + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + boolean handled = false; + if (NAMESPACE.equals(uri)) { + if (BOOKMARK_TREE.getLocalName().equals(localName)) { + if (bookmarkTree != null) { + throw new SAXException(localName + " must be the root element!"); + } + bookmarkTree = new BookmarkTree(); + objectStack.push(bookmarkTree); + } else if (BOOKMARK.getLocalName().equals(localName)) { + String title = attributes.getValue("title"); + String s = attributes.getValue("starting-state"); + boolean show = !"hide".equals(s); + Bookmark b = new Bookmark(title, show, null); + Object o = objectStack.peek(); + if (o instanceof AbstractAction) { + AbstractAction action = (AbstractAction)objectStack.pop(); + o = objectStack.peek(); + ((Bookmark)o).setAction(action); + } + if (o instanceof BookmarkTree) { + ((BookmarkTree)o).addBookmark(b); + } else { + ((Bookmark)o).addChildBookmark(b); + } + objectStack.push(b); + } else if (GOTO_XY.getLocalName().equals(localName)) { + int pageIndex = XMLUtil.getAttributeAsInt(attributes, "page-index"); + int x = XMLUtil.getAttributeAsInt(attributes, "x"); + int y = XMLUtil.getAttributeAsInt(attributes, "y"); + GoToXYAction action = new GoToXYAction(pageIndex, new Point(x, y)); + objectStack.push(action); + } else if (GOTO_URI.getLocalName().equals(localName)) { + String gotoURI = attributes.getValue("uri"); + URIAction action = new URIAction(gotoURI); + objectStack.push(action); + } else { + throw new SAXException( + "Invalid element " + localName + " in namespace: " + uri); + } + handled = true; + } + if (!handled) { + if (PSExtensionAttachment.CATEGORY.equals(uri)) { + throw new SAXException("Unhandled element " + localName + " in namespace: " + + uri); + } else { + log.warn("Unhandled element " + localName + " in namespace: " + uri); + } + } + } + + /** {@inheritDoc} */ + public void endElement(String uri, String localName, String qName) throws SAXException { + if (NAMESPACE.equals(uri)) { + if (BOOKMARK_TREE.getLocalName().equals(localName)) { + //nop + } else if (BOOKMARK.getLocalName().equals(localName)) { + if (objectStack.peek() instanceof AbstractAction) { + AbstractAction action = (AbstractAction)objectStack.pop(); + Bookmark b = (Bookmark)objectStack.pop(); + b.setAction(action); + } else { + objectStack.pop(); + } + } + } + content.setLength(0); // Reset text buffer (see characters()) + } + + /** {@inheritDoc} */ + public void characters(char[] ch, int start, int length) throws SAXException { + content.append(ch, start, length); + } + + /** {@inheritDoc} */ + public void endDocument() throws SAXException { + if (listener != null) { + listener.notifyObjectBuilt(getObject()); + } + } + + /** {@inheritDoc} */ + public Object getObject() { + return bookmarkTree; + } + + /** {@inheritDoc} */ + public void setObjectBuiltListener(ObjectBuiltListener listener) { + this.listener = listener; + } + } + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/BookmarkTree.java b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkTree.java new file mode 100644 index 000000000..8ecd87103 --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/BookmarkTree.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.apache.xmlgraphics.util.XMLizable; + +/** + * This class is the root of the bookmark tree for use in the intermediate format. + */ +public class BookmarkTree implements XMLizable, BookmarkExtensionConstants { + + private List bookmarks = new java.util.ArrayList(); + + /** + * Constructs a new bookmark tree. + */ + public BookmarkTree() { + //nop + } + + /** + * Adds a new top-level bookmark. + * @param bookmark the bookmark + */ + public void addBookmark(Bookmark bookmark) { + this.bookmarks.add(bookmark); + } + + /** + * Returns a list of top-level bookmarks. + * @return the top-level bookmarks + */ + public List getBookmarks() { + return Collections.unmodifiableList(this.bookmarks); + } + + /** {@inheritDoc} */ + public void toSAX(ContentHandler handler) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + handler.startElement(BOOKMARK_TREE.getNamespaceURI(), + BOOKMARK_TREE.getLocalName(), BOOKMARK_TREE.getQName(), atts); + Iterator iter = this.bookmarks.iterator(); + while (iter.hasNext()) { + Bookmark b = (Bookmark)iter.next(); + b.toSAX(handler); + } + handler.endElement(BOOKMARK_TREE.getNamespaceURI(), + BOOKMARK_TREE.getLocalName(), BOOKMARK_TREE.getQName()); + } + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/GoToXYAction.java b/src/java/org/apache/fop/render/intermediate/extensions/GoToXYAction.java new file mode 100644 index 000000000..ef6d9c2a6 --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/GoToXYAction.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import java.awt.Point; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.apache.fop.util.XMLUtil; + +/** + * Action class which represents a "go-to" action to an absolute coordinate on a page. + */ +public class GoToXYAction extends AbstractAction implements BookmarkExtensionConstants { + + private int pageIndex; + private Point targetLocation; + + /** + * Creates a new instance. + * @param pageIndex the page index (0-based) of the target page + * @param targetLocation the absolute location on the page (coordinates in millipoints) + */ + public GoToXYAction(int pageIndex, Point targetLocation) { + this.pageIndex = pageIndex; + this.targetLocation = targetLocation; + } + + /** + * Returns the page index of the target page. + * @return the page index (0-based) + */ + public int getPageIndex() { + return this.pageIndex; + } + + /** + * Returns the absolute coordinates of the target location on the page. + * @return the target location (coordinates in millipoints) + */ + public Point getTargetLocation() { + return this.targetLocation; + } + + /** + * Sets the absolute coordinates of the target location on the page. + * @param location the location (coordinates in millipoints) + */ + public void setTargetLocation(Point location) { + this.targetLocation = location; + } + + /** {@inheritDoc} */ + public void toSAX(ContentHandler handler) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute(null, "page-index", "page-index", + XMLUtil.CDATA, Integer.toString(pageIndex)); + atts.addAttribute(null, "x", "x", XMLUtil.CDATA, Integer.toString(targetLocation.x)); + atts.addAttribute(null, "y", "y", XMLUtil.CDATA, Integer.toString(targetLocation.y)); + handler.startElement(GOTO_XY.getNamespaceURI(), + GOTO_XY.getLocalName(), GOTO_XY.getQName(), atts); + handler.endElement(GOTO_XY.getNamespaceURI(), + GOTO_XY.getLocalName(), GOTO_XY.getQName()); + } + +} diff --git a/src/java/org/apache/fop/render/intermediate/extensions/URIAction.java b/src/java/org/apache/fop/render/intermediate/extensions/URIAction.java new file mode 100644 index 000000000..c5d56d9a4 --- /dev/null +++ b/src/java/org/apache/fop/render/intermediate/extensions/URIAction.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.render.intermediate.extensions; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import org.apache.fop.util.XMLUtil; + +/** + * Action class which represents a "URI" action, i.e. an action that will call up an external + * resource identified by a URI. + */ +public class URIAction extends AbstractAction implements BookmarkExtensionConstants { + + private String uri; + + /** + * Creates a new instance. + * @param uri the target URI + */ + public URIAction(String uri) { + this.uri = uri; + } + + /** + * Returns the target URI. + * @return the target URI + */ + public String getURI() { + return this.uri; + } + + /** {@inheritDoc} */ + public void toSAX(ContentHandler handler) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute(null, "uri", "uri", XMLUtil.CDATA, getURI()); + handler.startElement(GOTO_URI.getNamespaceURI(), + GOTO_URI.getLocalName(), GOTO_URI.getQName(), atts); + handler.endElement(GOTO_URI.getNamespaceURI(), + GOTO_URI.getLocalName(), GOTO_URI.getQName()); + } + +} |