KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cos > COSBoolean


1 /**
2  * Copyright (c) 2003, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.cos;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import org.pdfbox.exceptions.COSVisitorException;
37
38 /**
39  * This class represents a boolean value in the PDF document.
40  *
41  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
42  * @version $Revision: 1.14 $
43  */

44 public class COSBoolean extends COSBase
45 {
46     /**
47      * The true boolean token.
48      */

49     public static final byte[] TRUE_BYTES = new byte[]{ 116, 114, 117, 101 }; //"true".getBytes( "ISO-8859-1" );
50
/**
51      * The false boolean token.
52      */

53     public static final byte[] FALSE_BYTES = new byte[]{ 102, 97, 108, 115, 101 }; //"false".getBytes( "ISO-8859-1" );
54

55     /**
56      * The PDF true value.
57      */

58     public static final COSBoolean TRUE = new COSBoolean( true );
59
60     /**
61      * The PDF false value.
62      */

63     public static final COSBoolean FALSE = new COSBoolean( false );
64
65     private boolean value;
66
67     /**
68      * Constructor.
69      *
70      * @param aValue The boolean value.
71      */

72     private COSBoolean(boolean aValue )
73     {
74         value = aValue;
75     }
76
77     /**
78      * This will get the value that this object wraps.
79      *
80      * @return The boolean value of this object.
81      */

82     public boolean getValue()
83     {
84         return value;
85     }
86
87     /**
88      * This will get the value that this object wraps.
89      *
90      * @return The boolean value of this object.
91      */

92     public Boolean JavaDoc getValueAsObject()
93     {
94         return (value?Boolean.TRUE:Boolean.FALSE);
95     }
96
97     /**
98      * This will get the boolean value.
99      *
100      * @param value Parameter telling which boolean value to get.
101      *
102      * @return The single boolean instance that matches the parameter.
103      */

104     public static COSBoolean getBoolean( boolean value )
105     {
106         return (value?TRUE:FALSE);
107     }
108
109     /**
110      * This will get the boolean value.
111      *
112      * @param value Parameter telling which boolean value to get.
113      *
114      * @return The single boolean instance that matches the parameter.
115      */

116     public static COSBoolean getBoolean( Boolean JavaDoc value )
117     {
118         return getBoolean( value.booleanValue() );
119     }
120
121     /**
122      * visitor pattern double dispatch method.
123      *
124      * @param visitor The object to notify when visiting this object.
125      * @return any object, depending on the visitor implementation, or null
126      * @throws COSVisitorException If an error occurs while visiting this object.
127      */

128     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
129     {
130         return visitor.visitFromBoolean(this);
131     }
132
133     /**
134      * Return a string representation of this object.
135      *
136      * @return The string value of this object.
137      */

138     public String JavaDoc toString()
139     {
140         return String.valueOf( value );
141     }
142     
143     /**
144      * This will write this object out to a PDF stream.
145      *
146      * @param output The stream to write this object out to.
147      *
148      * @throws IOException If an error occurs while writing out this object.
149      */

150     public void writePDF( OutputStream JavaDoc output ) throws IOException JavaDoc
151     {
152         if( value )
153         {
154             output.write( TRUE_BYTES );
155         }
156         else
157         {
158             output.write( FALSE_BYTES );
159         }
160     }
161 }
Popular Tags