Debian Codegraph @

texmf-dist/source/latex/pax/src/StringVisitor.java

raw
1 /* StringVisitor.java
2 Copyright (C) 2006-2008, 2011, 2012 Heiko Oberdiek
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 MA 02110-1301 USA
18
19 This file is part of PDFAnnotExtractor. See README.
20 */
21 package pax;
22
23 import java.io.*;
24 import java.util.*;
25 import org.apache.pdfbox.cos.*;
26 import org.apache.pdfbox.exceptions.*;
27
28 public class StringVisitor implements ICOSVisitor {
29
30 protected StringBuffer buf;
31 boolean nodelim;
32
33 public StringVisitor() {
34 buf = new StringBuffer();
35 nodelim = false;
36 }
37
38 public Object visitFromNull(COSNull obj) {
39 if (nodelim) {
40 buf.append(' ');
41 }
42 buf.append("null");
43 nodelim = true;
44 return buf;
45 }
46
47 public Object visitFromBoolean(COSBoolean obj) {
48 if (nodelim) {
49 buf.append(' ');
50 }
51 buf.append(obj.getValue());
52 nodelim = true;
53 return buf;
54 }
55
56 public Object visitFromInt(COSInteger obj) {
57 if (nodelim) {
58 buf.append(' ');
59 }
60 buf.append(obj.intValue());
61 nodelim = true;
62 return buf;
63 }
64
65 public Object visitFromFloat(COSFloat obj) {
66 if (nodelim) {
67 buf.append(' ');
68 }
69 String s = "" + obj.floatValue();
70 if (s.endsWith(".0")) {
71 buf.append(s.substring(0, s.length() - ".0".length()));
72 }
73 else {
74 buf.append(s);
75 }
76 nodelim = true;
77 return buf;
78 }
79
80 public Object visitFromName(COSName obj) {
81 ByteArrayOutputStream a = new ByteArrayOutputStream();
82 try {
83 obj.writePDF(a);
84 }
85 catch (IOException e) {}
86 buf.append(a.toString());
87 nodelim = true;
88 return buf;
89 }
90
91 public Object visitFromString(COSString obj) {
92 buf.append("\\<");
93 buf.append(obj.getHexString());
94 buf.append("\\>");
95 nodelim = false;
96 return buf;
97 }
98
99 public Object visitFromArray(COSArray obj) throws COSVisitorException {
100 buf.append('[');
101 nodelim = false;
102 for (int i = 0; i < obj.size(); i++) {
103 obj.getObject(i).accept(this);
104 }
105 buf.append(']');
106 nodelim = false;
107 return buf;
108 }
109
110 public Object visitFromDictionary(COSDictionary obj) throws COSVisitorException {
111 buf.append("<<");
112 nodelim = false;
113 Iterator iter = obj.keyList().iterator();
114 while (iter.hasNext()) {
115 COSName key = (COSName)iter.next();
116 COSBase value = obj.getDictionaryObject(key);
117 key.accept(this);
118 value.accept(this);
119 }
120 buf.append(">>");
121 nodelim = false;
122 return buf;
123 }
124
125 public Object visitFromStream(COSStream obj) throws COSVisitorException {
126 throw new COSVisitorException(new Exception("Unsupported stream object"));
127 }
128 public Object visitFromDocument(COSDocument obj) throws COSVisitorException {
129 throw new COSVisitorException(new Exception("Unsupported documetn object"));
130 }
131 }