]> source.dussan.org Git - xmlgraphics-fop.git/blob
9044a4a62f1a41d0c352a9b835c6edc7e372a0ce
[xmlgraphics-fop.git] /
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /* $Id$ */
19
20 package org.apache.fop.render.intermediate.extensions;
21
22 import java.awt.Point;
23 import java.util.Stack;
24
25 import org.xml.sax.Attributes;
26 import org.xml.sax.ContentHandler;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.helpers.DefaultHandler;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import org.apache.fop.render.ps.extensions.PSExtensionAttachment;
34 import org.apache.fop.util.ContentHandlerFactory;
35 import org.apache.fop.util.XMLUtil;
36
37 /**
38  * Factory for the ContentHandler that handles the IF document navigation namespace.
39  */
40 public class DocumentNavigationExtensionHandlerFactory
41         implements ContentHandlerFactory, DocumentNavigationExtensionConstants {
42
43     /** Logger instance */
44     protected static Log log = LogFactory.getLog(DocumentNavigationExtensionHandlerFactory.class);
45
46     /** {@inheritDoc} */
47     public String[] getSupportedNamespaces() {
48         return new String[] {NAMESPACE};
49     }
50
51     /** {@inheritDoc} */
52     public ContentHandler createContentHandler() {
53         return new Handler();
54     }
55
56     private static class Handler extends DefaultHandler
57                 implements ContentHandlerFactory.ObjectSource {
58
59         private StringBuffer content = new StringBuffer();
60         private Stack objectStack = new Stack();
61
62         private Object objectBuilt;
63         private ObjectBuiltListener listener;
64
65         /** {@inheritDoc} */
66         public void startElement(String uri, String localName, String qName, Attributes attributes)
67                 throws SAXException {
68             boolean handled = false;
69             if (NAMESPACE.equals(uri)) {
70                 if (BOOKMARK_TREE.getLocalName().equals(localName)) {
71                     if (!objectStack.isEmpty()) {
72                         throw new SAXException(localName + " must be the root element!");
73                     }
74                     BookmarkTree bookmarkTree = new BookmarkTree();
75                     objectStack.push(bookmarkTree);
76                 } else if (BOOKMARK.getLocalName().equals(localName)) {
77                     String title = attributes.getValue("title");
78                     String s = attributes.getValue("starting-state");
79                     boolean show = !"hide".equals(s);
80                     Bookmark b = new Bookmark(title, show, null);
81                     Object o = objectStack.peek();
82                     if (o instanceof AbstractAction) {
83                         AbstractAction action = (AbstractAction)objectStack.pop();
84                         o = objectStack.peek();
85                         ((Bookmark)o).setAction(action);
86                     }
87                     if (o instanceof BookmarkTree) {
88                         ((BookmarkTree)o).addBookmark(b);
89                     } else {
90                         ((Bookmark)o).addChildBookmark(b);
91                     }
92                     objectStack.push(b);
93                 } else if (NAMED_DESTINATION.getLocalName().equals(localName)) {
94                     if (!objectStack.isEmpty()) {
95                         throw new SAXException(localName + " must be the root element!");
96                     }
97                     String name = attributes.getValue("name");
98                     NamedDestination dest = new NamedDestination(name, null);
99                     objectStack.push(dest);
100                 } else if (GOTO_XY.getLocalName().equals(localName)) {
101                     String id = attributes.getValue("id");
102                     int pageIndex = XMLUtil.getAttributeAsInt(attributes, "page-index");
103                     int x = XMLUtil.getAttributeAsInt(attributes, "x");
104                     int y = XMLUtil.getAttributeAsInt(attributes, "y");
105                     GoToXYAction action = new GoToXYAction(id, pageIndex, new Point(x, y));
106                     objectStack.push(action);
107                 } else if (GOTO_URI.getLocalName().equals(localName)) {
108                     String gotoURI = attributes.getValue("uri");
109                     String showDestination = attributes.getValue("show-destination");
110                     boolean newWindow = "new".equals(showDestination);
111                     URIAction action = new URIAction(gotoURI, newWindow);
112                     objectStack.push(action);
113                 } else {
114                     throw new SAXException(
115                             "Invalid element " + localName + " in namespace: " + uri);
116                 }
117                 handled = true;
118             }
119             if (!handled) {
120                 if (PSExtensionAttachment.CATEGORY.equals(uri)) {
121                     throw new SAXException("Unhandled element " + localName + " in namespace: "
122                             + uri);
123                 } else {
124                     log.warn("Unhandled element " + localName + " in namespace: " + uri);
125                 }
126             }
127         }
128
129         /** {@inheritDoc} */
130         public void endElement(String uri, String localName, String qName) throws SAXException {
131             if (NAMESPACE.equals(uri)) {
132                 if (BOOKMARK_TREE.getLocalName().equals(localName)) {
133                     //nop
134                 } else if (BOOKMARK.getLocalName().equals(localName)) {
135                     if (objectStack.peek() instanceof AbstractAction) {
136                         AbstractAction action = (AbstractAction)objectStack.pop();
137                         Bookmark b = (Bookmark)objectStack.pop();
138                         b.setAction(action);
139                     } else {
140                         objectStack.pop();
141                     }
142                 } else if (NAMED_DESTINATION.getLocalName().equals(localName)) {
143                     AbstractAction action = (AbstractAction)objectStack.pop();
144                     NamedDestination dest = (NamedDestination)objectStack.peek();
145                     dest.setAction(action);
146                 }
147             }
148             content.setLength(0); // Reset text buffer (see characters())
149         }
150
151         /** {@inheritDoc} */
152         public void characters(char[] ch, int start, int length) throws SAXException {
153             content.append(ch, start, length);
154         }
155
156         /** {@inheritDoc} */
157         public void endDocument() throws SAXException {
158             this.objectBuilt = objectStack.pop();
159             assert objectStack.isEmpty();
160             if (listener != null) {
161                 listener.notifyObjectBuilt(this.objectBuilt);
162             }
163         }
164
165         /** {@inheritDoc} */
166         public Object getObject() {
167             return objectBuilt;
168         }
169
170         /** {@inheritDoc} */
171         public void setObjectBuiltListener(ObjectBuiltListener listener) {
172             this.listener = listener;
173         }
174     }
175
176 }