001: /**
002: * Copyright (c) 2003-2004, 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.util;
031:
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * This class represents an Operator in the content stream.
038: *
039: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
040: * @version $Revision: 1.14 $
041: */
042: public class PDFOperator {
043: private String theOperator;
044: private byte[] imageData;
045: private ImageParameters imageParameters;
046:
047: private static Map operators = Collections
048: .synchronizedMap(new HashMap());
049:
050: /**
051: * Constructor.
052: *
053: * @param aOperator The operator that this object will represent.
054: */
055: private PDFOperator(String aOperator) {
056: theOperator = aOperator;
057: if (aOperator.startsWith("/")) {
058: throw new RuntimeException(
059: "Operators are not allowed to start with / '"
060: + aOperator + "'");
061: }
062: }
063:
064: /**
065: * This is used to create/cache operators in the system.
066: *
067: * @param operator The operator for the system.
068: *
069: * @return The operator that matches the operator keyword.
070: */
071: public static PDFOperator getOperator(String operator) {
072: PDFOperator operation = null;
073: if (operator.equals("ID") || operator.equals("BI")) {
074: //we can't cache the ID operators.
075: operation = new PDFOperator(operator);
076: } else {
077: operation = (PDFOperator) operators.get(operator);
078: if (operation == null) {
079: operation = new PDFOperator(operator);
080: operators.put(operator, operation);
081: }
082: }
083:
084: return operation;
085: }
086:
087: /**
088: * This will get the operation that this operator represents.
089: *
090: * @return The string representation of the operation.
091: */
092: public String getOperation() {
093: return theOperator;
094: }
095:
096: /**
097: * This will print a string rep of this class.
098: *
099: * @return A string rep of this class.
100: */
101: public String toString() {
102: return "PDFOperator{" + theOperator + "}";
103: }
104:
105: /**
106: * This is the special case for the ID operator where there are just random
107: * bytes inlined the stream.
108: *
109: * @return Value of property imageData.
110: */
111: public byte[] getImageData() {
112: return this .imageData;
113: }
114:
115: /**
116: * This will set the image data, this is only used for the ID operator.
117: *
118: * @param imageDataArray New value of property imageData.
119: */
120: public void setImageData(byte[] imageDataArray) {
121: imageData = imageDataArray;
122: }
123:
124: /**
125: * This will get the image parameters, this is only valid for BI operators.
126: *
127: * @return The image parameters.
128: */
129: public ImageParameters getImageParameters() {
130: return imageParameters;
131: }
132:
133: /**
134: * This will set the image parameters, this is only valid for BI operators.
135: *
136: * @param params The image parameters.
137: */
138: public void setImageParameters(ImageParameters params) {
139: imageParameters = params;
140: }
141: }
|