Source code: com/sixlegs/image/png/AbstractTextChunk.java
1 // Copyright (C) 1998, 1999, 2001 Chris Nokleberg
2 // Please see included LICENSE.TXT
3
4 package com.sixlegs.image.png;
5
6 import java.io.CharArrayWriter;
7 import java.io.IOException;
8 import java.util.Hashtable;
9
10 abstract class AbstractTextChunk
11 extends KeyValueChunk
12 implements TextChunk
13 {
14 static private Hashtable special_keys = new Hashtable();
15 static {
16 special_keys.put("Title", Boolean.TRUE);
17 special_keys.put("Author", Boolean.TRUE);
18 special_keys.put("Description", Boolean.TRUE);
19 special_keys.put("Copyright", Boolean.TRUE);
20 special_keys.put("Creation Time", Boolean.TRUE);
21 special_keys.put("Software", Boolean.TRUE);
22 special_keys.put("Disclaimer", Boolean.TRUE);
23 special_keys.put("Warning", Boolean.TRUE);
24 special_keys.put("Source", Boolean.TRUE);
25 special_keys.put("Comment", Boolean.TRUE);
26 }
27
28 public String toString() { return getText(); }
29
30 public String getKeyword() { return key; }
31 public String getText() { return value; }
32 abstract public String getTranslatedKeyword();
33 abstract public String getLanguage();
34
35 AbstractTextChunk(int type) { super(type); }
36
37 protected String readKey()
38 throws IOException
39 {
40 String key = super.readKey();
41 if (special_keys.containsKey(key)) {
42 String lowerkey = key.toLowerCase();
43 Object replace = img.data.properties.get(lowerkey);
44 if (replace == null || ((Chunk)replace).type != iTXt)
45 img.data.properties.put(lowerkey, this);
46 }
47 img.data.textChunks.put(key, this);
48 return key;
49 }
50
51 protected String readValue()
52 throws IOException
53 {
54 return repairValue(super.readValue());
55 }
56
57 private static String repairValue(String val)
58 {
59 CharArrayWriter out_chars = new CharArrayWriter(val.length());
60 try {
61 char[] chs = val.toCharArray();
62 int i = 0, p = 0;
63 int L = chs.length;
64 String endl = System.getProperty("line.separator");
65 while (p < L) {
66 char ch = chs[p++];
67 switch (ch) {
68 case '\r':
69 if (p < L && chs[p + 1] == '\n') break;
70 case '\n':
71 out_chars.write(endl);
72 break;
73 case '\t':
74 out_chars.write('\t');
75 break;
76 default:
77 if (ch <= 31 || (ch >= 127 && ch <= 159)) {
78 out_chars.write('\\');
79 out_chars.write(Integer.toOctalString(ch));
80 } else {
81 out_chars.write(ch);
82 }
83 }
84 }
85 } catch (IOException e) { }
86 return out_chars.toString();
87 }
88
89 public String getChunkType()
90 {
91 return typeToString(type);
92 }
93 }