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: AbstractPSExtensionElement.java 1465599 2013-04-08 11:51:52Z vhennebert $ */
19 
20 package org.apache.fop.render.ps.extensions;
21 
22 // FOP
23 import org.xml.sax.Locator;
24 
25 import org.apache.fop.apps.FOPException;
26 import org.apache.fop.fo.FONode;
27 import org.apache.fop.fo.PropertyList;
28 import org.apache.fop.fo.ValidationException;
29 import org.apache.fop.fo.extensions.ExtensionAttachment;
30 
31 /**
32  * Base class for the PostScript-specific extension elements.
33  */
34 public abstract class AbstractPSExtensionElement extends FONode {
35 
36     /**
37      * extension attachment
38      */
39     protected PSExtensionAttachment attachment;
40 
41     /**
42      * Default constructor
43      *
44      * @param parent parent of this node
45      * @see org.apache.fop.fo.FONode#FONode(FONode)
46      */
AbstractPSExtensionElement(FONode parent)47     public AbstractPSExtensionElement(FONode parent) {
48         super(parent);
49     }
50 
51     /**
52      * Blocks XSL FO's from having non-FO parents.
53      *
54      * @param loc location in the FO source file
55      * @param nsURI namespace of incoming node
56      * @param localName (e.g. "table" for "fo:table")
57      * @throws ValidationException if incoming node not valid for parent
58      * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
59      */
validateChildNode(Locator loc, String nsURI, String localName)60     protected void validateChildNode(Locator loc, String nsURI, String localName)
61                 throws ValidationException {
62         if (FO_URI.equals(nsURI)) {
63             invalidChildError(loc, nsURI, localName);
64         }
65     }
66 
67     /** {@inheritDoc} */
characters(char[] data, int start, int length, PropertyList pList, Locator locator)68     protected void characters(char[] data, int start, int length,
69                                  PropertyList pList, Locator locator) {
70         PSExtensionAttachment a = (PSExtensionAttachment)getExtensionAttachment();
71         if (a.getContent() != null) {
72             StringBuffer sb = new StringBuffer(a.getContent());
73             sb.append(data, start, length);
74             a.setContent(sb.toString());
75         } else {
76             a.setContent(new String(data, start, length));
77         }
78     }
79 
80     /**
81      * @return a String representation of this object
82      * @see org.apache.fop.fo.FONode#getNamespaceURI()
83      */
getNamespaceURI()84     public String getNamespaceURI() {
85         return PSExtensionElementMapping.NAMESPACE;
86     }
87 
88     /**
89      * @return a String representation of this object
90      * @see org.apache.fop.fo.FONode#getNormalNamespacePrefix()
91      */
getNormalNamespacePrefix()92     public String getNormalNamespacePrefix() {
93         return "ps";
94     }
95 
96     /**
97      * @see org.apache.fop.fo.FONode#endOfNode()
98      * @throws FOPException if there's a problem during processing
99      */
endOfNode()100     public void endOfNode() throws FOPException {
101         super.endOfNode();
102         String s = ((PSExtensionAttachment)getExtensionAttachment()).getContent();
103         if (s == null || s.length() == 0) {
104             missingChildElementError("#PCDATA");
105         }
106     }
107 
108     /**
109      * @return the extension attachment if one is created by the extension element, null otherwise.
110      * @see org.apache.fop.fo.FONode#getExtensionAttachment()
111      */
getExtensionAttachment()112     public ExtensionAttachment getExtensionAttachment() {
113         if (attachment == null) {
114             this.attachment = (PSExtensionAttachment)instantiateExtensionAttachment();
115         }
116         return this.attachment;
117     }
118 
119     /**
120      * Instantiates extension attachment object
121      * @return extension attachment
122      */
instantiateExtensionAttachment()123     protected abstract ExtensionAttachment instantiateExtensionAttachment();
124 }
125 
126