KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > objects > PdfLiteral


1 /*
2   Copyright 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright 2006,2007 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.objects;
28
29 import it.stefanochizzolini.clown.files.File;
30
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33
34 /**
35   PDF literal string object [PDF:1.6:3.2.3].
36 */

37 /*
38   NOTE: Text strings meaning is tied to their applicable encoding:
39     * text strings inside the document's content streams are to be intended in accordance to
40     the encoding of the currently-selected font;
41     * text strings outside the document's content streams can be represented either by
42     PDFDocEncoding [PDF:1.6:D] or Unicode encoding [PDF:1.6:3.8.1].
43 */

44 /*
45   NOTE: As literal objects are textual, their characters are unescaped when deserialized,
46   whilst escaped when serialized only.
47 */

48 public class PdfLiteral
49   extends PdfAtomicObject<String JavaDoc>
50   implements IPdfString
51 {
52   // <static>
53
// <fields>
54
/*
55     NOTE: Literal lexical conventions prescribe that the following reserved characters
56     are to be escaped when placed inside strings' character sequences:
57       - \n Line feed (LF)
58       - \r Carriage return (CR)
59       - \t Horizontal tab (HT)
60       - \b Backspace (BS)
61       - \f Form feed (FF)
62       - \( Left parenthesis
63       - \) Right parenthesis
64       - \\ Backslash
65   */

66   private static final Pattern JavaDoc UnescapedPattern = Pattern.compile("[\\n\\r\\t\\u0008\\f\\(\\)\\\\]");
67   // </fields>
68

69   // <interface>
70
// <public>
71
/**
72     Converts a string value to a PDF-formatted string literal.
73   */

74   public static String JavaDoc toPdf(
75     String JavaDoc value
76     )
77   {
78     /*
79       NOTE: Any reserved character MUST be escaped.
80     */

81     StringBuilder JavaDoc buffer = new StringBuilder JavaDoc("(");
82
83     int index = 0;
84     Matcher JavaDoc unescapedMatcher = UnescapedPattern.matcher(value);
85     while(unescapedMatcher.find())
86     {
87       int start = unescapedMatcher.start();
88       if(start > index)
89       {buffer.append(value.substring(index,start));}
90
91       buffer.append('\\');
92       char unescapedChar = unescapedMatcher.group(0).charAt(0);
93       switch(unescapedChar)
94       {
95         case '\n':
96           buffer.append('n');
97           break;
98         case '\r':
99           buffer.append('r');
100           break;
101         case '\t':
102           buffer.append('t');
103           break;
104         case '\b':
105           buffer.append('b');
106           break;
107         case '\f':
108           buffer.append('f');
109           break;
110         case '(':
111         case ')':
112         case '\\':
113           buffer.append(unescapedChar);
114           break;
115       }
116
117       index = unescapedMatcher.end();
118     }
119     if(index < value.length())
120     {buffer.append(value.substring(index));}
121
122     buffer.append(")");
123
124     return buffer.toString();
125   }
126   // </public>
127
// </interface>
128
// </static>
129

130   // <dynamic>
131
// <constructors>
132
public PdfLiteral(
133     )
134   {}
135
136   public PdfLiteral(
137     String JavaDoc value
138     )
139   {setValue(value);}
140   // </constructors>
141

142   // <interface>
143
// <public>
144
@Override JavaDoc
145   public Object JavaDoc clone(
146     File context
147     )
148   {
149     // Shallow copy.
150
PdfLiteral clone = (PdfLiteral)super.clone();
151
152     // Deep copy.
153
/* NOTE: No mutable object to be cloned. */
154
155     return clone;
156   }
157
158   @Override JavaDoc
159   public String JavaDoc toPdf(
160     )
161   {return PdfLiteral.toPdf(getValue());}
162
163   // <IPdfString>
164
public String JavaDoc getStringValue(
165     )
166   {return getValue();}
167
168   public void setStringValue(
169     String JavaDoc value
170     )
171   {setValue(value);}
172   // </IPdfString>
173
// </public>
174
// </interface>
175
// </dynamic>
176
// </class>
177
}
Popular Tags