KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > ps > extensions > PSExtensionHandler


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.ps.extensions;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.fop.util.ContentHandlerFactory;
25 import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29
30 /**
31  * ContentHandler (parser) for restoring PSExtension objects from XML.
32  */

33 public class PSExtensionHandler extends DefaultHandler JavaDoc
34             implements ContentHandlerFactory.ObjectSource {
35
36     /** Logger instance */
37     protected static Log log = LogFactory.getLog(PSExtensionHandler.class);
38
39     private StringBuffer JavaDoc content = new StringBuffer JavaDoc();
40     private Attributes JavaDoc lastAttributes;
41     
42     private PSSetupCode returnedObject;
43     private ObjectBuiltListener listener;
44     
45     /** @see org.xml.sax.helpers.DefaultHandler */
46     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
47                 throws SAXException JavaDoc {
48         boolean handled = false;
49         if (PSSetupCode.CATEGORY.equals(uri)) {
50             lastAttributes = attributes;
51             handled = true;
52             if ("ps-setup-code".equals(localName)) {
53                 //handled in endElement
54
} else {
55                 handled = false;
56             }
57         }
58         if (!handled) {
59             if (PSSetupCode.CATEGORY.equals(uri)) {
60                 throw new SAXException JavaDoc("Unhandled element " + localName
61                         + " in namespace: " + uri);
62             } else {
63                 log.warn("Unhandled element " + localName
64                         + " in namespace: " + uri);
65             }
66         }
67     }
68
69     /** @see org.xml.sax.helpers.DefaultHandler */
70     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
71         if (PSSetupCode.CATEGORY.equals(uri)) {
72             if ("ps-setup-code".equals(localName)) {
73                 String JavaDoc name = lastAttributes.getValue("name");
74                 this.returnedObject = new PSSetupCode(name, content.toString());
75             }
76         }
77         content.setLength(0); //Reset text buffer (see characters())
78
}
79
80     /** @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) */
81     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
82         content.append(ch, start, length);
83     }
84
85     /**
86      * @see org.xml.sax.helpers.DefaultHandler#endDocument()
87      */

88     public void endDocument() throws SAXException JavaDoc {
89         if (listener != null) {
90             listener.notifyObjectBuilt(getObject());
91         }
92     }
93
94     /**
95      * @see org.apache.fop.util.ContentHandlerFactory.ObjectSource#getObject()
96      */

97     public Object JavaDoc getObject() {
98         return returnedObject;
99     }
100
101     /**
102      * @see org.apache.fop.util.ContentHandlerFactory.ObjectSource
103      */

104     public void setObjectBuiltListener(ObjectBuiltListener listener) {
105         this.listener = listener;
106     }
107
108 }
109
Popular Tags