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: PSExtensionHandler.java 1343072 2012-05-27 17:39:49Z gadams $ */ 19 20 package org.apache.fop.render.ps.extensions; 21 22 import org.xml.sax.Attributes; 23 import org.xml.sax.SAXException; 24 import org.xml.sax.helpers.AttributesImpl; 25 import org.xml.sax.helpers.DefaultHandler; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.apache.fop.util.ContentHandlerFactory; 31 import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener; 32 33 /** 34 * ContentHandler (parser) for restoring PSExtension objects from XML. 35 */ 36 public class PSExtensionHandler extends DefaultHandler 37 implements ContentHandlerFactory.ObjectSource { 38 39 /** Logger instance */ 40 protected static final Log log = LogFactory.getLog(PSExtensionHandler.class); 41 42 private StringBuffer content = new StringBuffer(); 43 private Attributes lastAttributes; 44 45 private PSExtensionAttachment returnedObject; 46 private ObjectBuiltListener listener; 47 48 /** {@inheritDoc} */ startElement(String uri, String localName, String qName, Attributes attributes)49 public void startElement(String uri, String localName, String qName, Attributes attributes) 50 throws SAXException { 51 boolean handled = false; 52 if (PSExtensionAttachment.CATEGORY.equals(uri)) { 53 lastAttributes = new AttributesImpl(attributes); 54 handled = false; 55 if (localName.equals(PSSetupCode.ELEMENT) 56 || localName.equals(PSPageTrailerCodeBefore.ELEMENT) 57 || localName.equals(PSSetPageDevice.ELEMENT) 58 || localName.equals(PSCommentBefore.ELEMENT) 59 || localName.equals(PSCommentAfter.ELEMENT)) { 60 //handled in endElement 61 handled = true; 62 } 63 } 64 if (!handled) { 65 if (PSExtensionAttachment.CATEGORY.equals(uri)) { 66 throw new SAXException("Unhandled element " + localName 67 + " in namespace: " + uri); 68 } else { 69 log.warn("Unhandled element " + localName 70 + " in namespace: " + uri); 71 } 72 } 73 } 74 75 /** {@inheritDoc} */ endElement(String uri, String localName, String qName)76 public void endElement(String uri, String localName, String qName) throws SAXException { 77 if (PSExtensionAttachment.CATEGORY.equals(uri)) { 78 if (PSSetupCode.ELEMENT.equals(localName)) { 79 String name = lastAttributes.getValue("name"); 80 this.returnedObject = new PSSetupCode(name, content.toString()); 81 } else if (PSSetPageDevice.ELEMENT.equals(localName)) { 82 String name = lastAttributes.getValue("name"); 83 this.returnedObject = new PSSetPageDevice(name, content.toString()); 84 } else if (PSCommentBefore.ELEMENT.equals(localName)) { 85 this.returnedObject = new PSCommentBefore(content.toString()); 86 } else if (PSCommentAfter.ELEMENT.equals(localName)) { 87 this.returnedObject = new PSCommentAfter(content.toString()); 88 } else if (PSPageTrailerCodeBefore.ELEMENT.equals(localName)) { 89 this.returnedObject = new PSPageTrailerCodeBefore(content.toString()); 90 } 91 } 92 content.setLength(0); //Reset text buffer (see characters()) 93 } 94 95 /** {@inheritDoc} */ characters(char[] ch, int start, int length)96 public void characters(char[] ch, int start, int length) throws SAXException { 97 content.append(ch, start, length); 98 } 99 100 /** {@inheritDoc} */ endDocument()101 public void endDocument() throws SAXException { 102 if (listener != null) { 103 listener.notifyObjectBuilt(getObject()); 104 } 105 } 106 107 /** {@inheritDoc} */ getObject()108 public Object getObject() { 109 return returnedObject; 110 } 111 112 /** {@inheritDoc} */ setObjectBuiltListener(ObjectBuiltListener listener)113 public void setObjectBuiltListener(ObjectBuiltListener listener) { 114 this.listener = listener; 115 } 116 } 117