| 1 |
/* Entry.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 |
|
| 26 |
public class Entry implements Constants { |
| 27 |
|
| 28 |
protected Writer out; |
| 29 |
|
| 30 |
protected String cmd; |
| 31 |
protected List args; |
| 32 |
protected Map kvs; |
| 33 |
protected boolean withKV; |
| 34 |
protected List delayedList; |
| 35 |
|
| 36 |
public Entry(Writer out) { |
| 37 |
this.out = out; |
| 38 |
cmd = null; |
| 39 |
args = new Vector(); |
| 40 |
kvs = null; |
| 41 |
withKV = false; |
| 42 |
delayedList = new Vector(); |
| 43 |
} |
| 44 |
|
| 45 |
public void clear() { |
| 46 |
cmd = null; |
| 47 |
args.clear(); |
| 48 |
kvs = null; |
| 49 |
withKV = false; |
| 50 |
} |
| 51 |
|
| 52 |
public void setCmd(String cmd) { |
| 53 |
clear(); |
| 54 |
this.cmd = cmd; |
| 55 |
} |
| 56 |
|
| 57 |
public String getCmd() { |
| 58 |
return cmd; |
| 59 |
} |
| 60 |
|
| 61 |
public void withKV() { |
| 62 |
this.withKV = true; |
| 63 |
if (kvs == null) { |
| 64 |
kvs = new HashMap(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public void addArg(String arg) { |
| 69 |
args.add(arg); |
| 70 |
} |
| 71 |
|
| 72 |
public void putKV(String key, String value) { |
| 73 |
withKV(); |
| 74 |
if (value == null) { |
| 75 |
return; |
| 76 |
} |
| 77 |
kvs.put(key, value); |
| 78 |
} |
| 79 |
|
| 80 |
public boolean isEmptyKV() { |
| 81 |
return !withKV || kvs.isEmpty(); |
| 82 |
} |
| 83 |
|
| 84 |
public void writeDelayed() throws EntryWriteException { |
| 85 |
Writer saved = out; |
| 86 |
try { |
| 87 |
out = new StringWriter(); |
| 88 |
write(); |
| 89 |
delayedList.add(out.toString()); |
| 90 |
} |
| 91 |
catch (Exception e) { throw new EntryWriteException(e); } |
| 92 |
finally { |
| 93 |
out = saved; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public void write() throws EntryWriteException { |
| 98 |
try { |
| 99 |
out.write(ENTRY_BEG); |
| 100 |
|
| 101 |
// write command |
| 102 |
out.write(CMD_BEG); |
| 103 |
out.write(cmd); |
| 104 |
out.write(CMD_END); |
| 105 |
|
| 106 |
// write arguments |
| 107 |
Iterator it = args.iterator(); |
| 108 |
while (it.hasNext()) { |
| 109 |
String arg = (String)it.next(); |
| 110 |
out.write(ARG_BEG); |
| 111 |
out.write(arg); |
| 112 |
out.write(ARG_END); |
| 113 |
} |
| 114 |
|
| 115 |
// write key value pairs |
| 116 |
if (withKV) { |
| 117 |
if (kvs.isEmpty()) { |
| 118 |
out.write(KVS_EMPTY); |
| 119 |
} |
| 120 |
else { |
| 121 |
out.write(KVS_BEG); |
| 122 |
it = kvs.entrySet().iterator(); |
| 123 |
while (it.hasNext()) { |
| 124 |
Map.Entry kv = (Map.Entry)it.next(); |
| 125 |
String key = (String)kv.getKey(); |
| 126 |
String value = (String)kv.getValue(); |
| 127 |
out.write(KV_BEG); |
| 128 |
out.write(KEY_BEG); |
| 129 |
out.write(key); |
| 130 |
out.write(KEY_END); |
| 131 |
if (value != null) { |
| 132 |
out.write(VALUE_BEG); |
| 133 |
out.write(value); |
| 134 |
out.write(VALUE_END); |
| 135 |
} |
| 136 |
out.write(KV_END); |
| 137 |
} |
| 138 |
out.write(KVS_END); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
out.write(ENTRY_END); |
| 143 |
out.flush(); |
| 144 |
} |
| 145 |
catch (IOException e) { |
| 146 |
throw new EntryWriteException(e); |
| 147 |
} |
| 148 |
finally { |
| 149 |
clear(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public void flushDelayed() throws EntryWriteException { |
| 154 |
try { |
| 155 |
Iterator it = delayedList.iterator(); |
| 156 |
while (it.hasNext()) { |
| 157 |
out.write((String)it.next()); |
| 158 |
} |
| 159 |
out.flush(); |
| 160 |
} |
| 161 |
catch (IOException e) { |
| 162 |
throw new EntryWriteException(e); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public void close() throws EntryWriteException { |
| 167 |
try { |
| 168 |
out.close(); |
| 169 |
} |
| 170 |
catch (IOException e) { |
| 171 |
throw new EntryWriteException(e); |
| 172 |
} |
| 173 |
} |
| 174 |
} |