001: /**
002: * Copyright (c) 2004-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.fdf;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import org.pdfbox.cos.COSArray;
036: import org.pdfbox.cos.COSBase;
037: import org.pdfbox.cos.COSDictionary;
038:
039: import org.pdfbox.pdmodel.common.COSArrayList;
040: import org.pdfbox.pdmodel.common.COSObjectable;
041:
042: /**
043: * This represents an FDF template that is part of the FDF page.
044: *
045: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
046: * @version $Revision: 1.3 $
047: */
048: public class FDFTemplate implements COSObjectable {
049: private COSDictionary template;
050:
051: /**
052: * Default constructor.
053: */
054: public FDFTemplate() {
055: template = new COSDictionary();
056: }
057:
058: /**
059: * Constructor.
060: *
061: * @param t The FDF page template.
062: */
063: public FDFTemplate(COSDictionary t) {
064: template = t;
065: }
066:
067: /**
068: * Convert this standard java object to a COS object.
069: *
070: * @return The cos object that matches this Java object.
071: */
072: public COSBase getCOSObject() {
073: return template;
074: }
075:
076: /**
077: * Convert this standard java object to a COS object.
078: *
079: * @return The cos object that matches this Java object.
080: */
081: public COSDictionary getCOSDictionary() {
082: return template;
083: }
084:
085: /**
086: * This is the template reference.
087: *
088: * @return The template reference.
089: */
090: public FDFNamedPageReference getTemplateReference() {
091: FDFNamedPageReference retval = null;
092: COSDictionary dict = (COSDictionary) template
093: .getDictionaryObject("TRef");
094: if (dict != null) {
095: retval = new FDFNamedPageReference(dict);
096: }
097: return retval;
098: }
099:
100: /**
101: * This will set the template reference.
102: *
103: * @param tRef The template reference.
104: */
105: public void setTemplateReference(FDFNamedPageReference tRef) {
106: template.setItem("TRef", tRef);
107: }
108:
109: /**
110: * This will get a list of fields that are part of this template.
111: *
112: * @return A list of fields.
113: */
114: public List getFields() {
115: List retval = null;
116: COSArray array = (COSArray) template
117: .getDictionaryObject("Fields");
118: if (array != null) {
119: List fields = new ArrayList();
120: for (int i = 0; i < array.size(); i++) {
121: fields.add(new FDFField((COSDictionary) array
122: .getObject(i)));
123: }
124: retval = new COSArrayList(fields, array);
125: }
126: return retval;
127: }
128:
129: /**
130: * This will set a list of fields for this template.
131: *
132: * @param fields The list of fields to set for this template.
133: */
134: public void setFields(List fields) {
135: template.setItem("Fields", COSArrayList
136: .converterToCOSArray(fields));
137: }
138:
139: /**
140: * A flag telling if the fields imported from the template may be renamed if there are conflicts.
141: *
142: * @return A flag telling if the fields can be renamed.
143: */
144: public boolean shouldRename() {
145: return template.getBoolean("Rename", false);
146: }
147:
148: /**
149: * This will set if the fields can be renamed.
150: *
151: * @param value The flag value.
152: */
153: public void setRename(boolean value) {
154: template.setBoolean("Rename", value);
155: }
156: }
|