Line 0
Link Here
|
|
|
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 |
/* $Id$ */ |
18 |
package org.apache.fop.fo.extensions.pdf; |
19 |
|
20 |
import org.apache.fop.fo.FONode; |
21 |
import org.apache.fop.fo.PropertyList; |
22 |
import org.apache.fop.fo.ValidationException; |
23 |
import org.apache.fop.apps.FOPException; |
24 |
import org.xml.sax.Locator; |
25 |
import org.xml.sax.Attributes; |
26 |
|
27 |
import java.nio.CharBuffer; |
28 |
|
29 |
/** |
30 |
* Class modelling the <code>pdf:entry</code> extension object. |
31 |
* This extension can be used to create PDF entries in a |
32 |
* <code>pdf:dictionary</code> object. |
33 |
* The object is mapped virtually one-on-one to PDF. |
34 |
* <br/><br/> |
35 |
* <code><pdf:entry name="PageMode">FullScreen</pdf:entry></code> |
36 |
* <br/><br/> |
37 |
* will result in "/PageMode FullScreen" being added as an entry |
38 |
* to the dictionary. |
39 |
*/ |
40 |
public class PDFEntryExtension extends AbstractPDFExtension { |
41 |
|
42 |
public static final int TYPE_NUMBER = 0; |
43 |
public static final int TYPE_BOOLEAN = 1; |
44 |
public static final int TYPE_NAME = 2; |
45 |
public static final int TYPE_ARRAY = 3; |
46 |
|
47 |
static final String[] TYPES = { |
48 |
"number", "boolean", "name", "array" }; |
49 |
|
50 |
private String entryName; |
51 |
private int entryType; |
52 |
private CharBuffer entryValue; |
53 |
|
54 |
/** |
55 |
* Construct a new PDFEntryExtension instance that is a child |
56 |
* of the given {@link org.apache.fop.fo.FONode} |
57 |
* |
58 |
* @param parent the parent {@link org.apache.fop.fo.FONode} |
59 |
*/ |
60 |
public PDFEntryExtension(FONode parent) { |
61 |
super(parent); |
62 |
} |
63 |
|
64 |
private static int getType(String typeName) { |
65 |
for (int i = TYPES.length; --i >= 0;) { |
66 |
if (TYPES[i].equals(typeName)) { |
67 |
return i; |
68 |
} |
69 |
} |
70 |
assert false; |
71 |
return -1; |
72 |
} |
73 |
|
74 |
/** {@inheritDoc} */ |
75 |
protected void validateChildNode(Locator loc, String namespaceURI, String localName) throws ValidationException { |
76 |
//TODO: Replace by extension-specific event (?) |
77 |
this.invalidChildError(loc, namespaceURI, localName); |
78 |
} |
79 |
|
80 |
/** {@inheritDoc} */ |
81 |
public void processNode(String elementName, Locator locator, Attributes attlist, PropertyList pList) throws FOPException { |
82 |
this.entryName = attlist.getValue("name"); |
83 |
if (this.entryName == null || "".equals(this.entryName)) { |
84 |
this.missingPropertyError("name"); |
85 |
} |
86 |
|
87 |
this.entryType = getType(attlist.getValue("type")); |
88 |
|
89 |
super.processNode(elementName, locator, attlist, pList); |
90 |
} |
91 |
|
92 |
/** {@inheritDoc} */ |
93 |
protected void addCharacters(char[] data, int start, int length, PropertyList pList, Locator locator) throws FOPException { |
94 |
if (this.entryValue == null) { |
95 |
this.entryValue = CharBuffer.allocate(length); |
96 |
} else { |
97 |
CharBuffer newExpr = CharBuffer.allocate( |
98 |
this.entryValue.length() + length); |
99 |
this.entryValue.position(0); |
100 |
newExpr.put(this.entryValue); |
101 |
this.entryValue = newExpr; |
102 |
} |
103 |
this.entryValue.put(data, start, length); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Return the name of the PDF entry. |
108 |
* |
109 |
* @return the name of the entry |
110 |
*/ |
111 |
public String getEntryName() { |
112 |
return this.entryName; |
113 |
} |
114 |
|
115 |
/** |
116 |
* Return the value of the PDF entry (= the #PCDATA |
117 |
* content of the node in the source document) |
118 |
* |
119 |
* @return the value of the entry |
120 |
*/ |
121 |
public String getEntryValue() { |
122 |
this.entryValue.rewind(); |
123 |
return this.entryValue.toString(); |
124 |
} |
125 |
|
126 |
/** |
127 |
* Return the type of this entry. |
128 |
* |
129 |
* @return one of {@link #TYPE_NUMBER}, {@link #TYPE_BOOLEAN}, |
130 |
* {@link #TYPE_NAME}, or {@link#TYPE_ARRAY} |
131 |
*/ |
132 |
public int getEntryType() { |
133 |
return this.entryType; |
134 |
} |
135 |
|
136 |
/** |
137 |
* Return the type of this entry as a String. |
138 |
* |
139 |
* @return the type name of this entry |
140 |
*/ |
141 |
public String getEntryTypeName() { |
142 |
return TYPES[this.entryType]; |
143 |
} |
144 |
|
145 |
/** {@inheritDoc} */ |
146 |
public String getLocalName() { |
147 |
return "entry"; |
148 |
} |
149 |
|
150 |
} |
0 |
+ Id |
151 |
+ Id |
1 |
+ native |
152 |
+ native |