001: /**
002: * Copyright (c) 2003, 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.cos;
031:
032: import java.io.IOException;
033: import java.io.OutputStream;
034:
035: import org.pdfbox.exceptions.COSVisitorException;
036:
037: /**
038: * This class represents a boolean value in the PDF document.
039: *
040: * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
041: * @version $Revision: 1.14 $
042: */
043: public class COSBoolean extends COSBase {
044: /**
045: * The true boolean token.
046: */
047: public static final byte[] TRUE_BYTES = new byte[] { 116, 114, 117,
048: 101 }; //"true".getBytes( "ISO-8859-1" );
049: /**
050: * The false boolean token.
051: */
052: public static final byte[] FALSE_BYTES = new byte[] { 102, 97, 108,
053: 115, 101 }; //"false".getBytes( "ISO-8859-1" );
054:
055: /**
056: * The PDF true value.
057: */
058: public static final COSBoolean TRUE = new COSBoolean(true);
059:
060: /**
061: * The PDF false value.
062: */
063: public static final COSBoolean FALSE = new COSBoolean(false);
064:
065: private boolean value;
066:
067: /**
068: * Constructor.
069: *
070: * @param aValue The boolean value.
071: */
072: private COSBoolean(boolean aValue) {
073: value = aValue;
074: }
075:
076: /**
077: * This will get the value that this object wraps.
078: *
079: * @return The boolean value of this object.
080: */
081: public boolean getValue() {
082: return value;
083: }
084:
085: /**
086: * This will get the value that this object wraps.
087: *
088: * @return The boolean value of this object.
089: */
090: public Boolean getValueAsObject() {
091: return (value ? Boolean.TRUE : Boolean.FALSE);
092: }
093:
094: /**
095: * This will get the boolean value.
096: *
097: * @param value Parameter telling which boolean value to get.
098: *
099: * @return The single boolean instance that matches the parameter.
100: */
101: public static COSBoolean getBoolean(boolean value) {
102: return (value ? TRUE : FALSE);
103: }
104:
105: /**
106: * This will get the boolean value.
107: *
108: * @param value Parameter telling which boolean value to get.
109: *
110: * @return The single boolean instance that matches the parameter.
111: */
112: public static COSBoolean getBoolean(Boolean value) {
113: return getBoolean(value.booleanValue());
114: }
115:
116: /**
117: * visitor pattern double dispatch method.
118: *
119: * @param visitor The object to notify when visiting this object.
120: * @return any object, depending on the visitor implementation, or null
121: * @throws COSVisitorException If an error occurs while visiting this object.
122: */
123: public Object accept(ICOSVisitor visitor)
124: throws COSVisitorException {
125: return visitor.visitFromBoolean(this );
126: }
127:
128: /**
129: * Return a string representation of this object.
130: *
131: * @return The string value of this object.
132: */
133: public String toString() {
134: return String.valueOf(value);
135: }
136:
137: /**
138: * This will write this object out to a PDF stream.
139: *
140: * @param output The stream to write this object out to.
141: *
142: * @throws IOException If an error occurs while writing out this object.
143: */
144: public void writePDF(OutputStream output) throws IOException {
145: if (value) {
146: output.write(TRUE_BYTES);
147: } else {
148: output.write(FALSE_BYTES);
149: }
150: }
151: }
|