/Users/lyon/j4p/src/xml/Utils.java

1    /* 
2     * @author Douglas A. Lyon 
3     * @version  Nov 6, 2002.8:03:04 AM 
4     */ 
5    package xml; 
6     
7     
8    import futils.FileFilter; 
9    import futils.Futil; 
10   import gui.In; 
11   import graphics.NumImage; 
12   import ip.gui.frames.ImageFrame; 
13   import j2d.ShortImageBean; 
14   import utils.Base64; 
15   import utils.StringUtils; 
16   import xml.adbk.Address; 
17    
18   import javax.xml.parsers.ParserConfigurationException; 
19   import javax.xml.parsers.SAXParser; 
20   import javax.xml.parsers.SAXParserFactory; 
21   import java.awt.Image; 
22   import java.beans.XMLDecoder; 
23   import java.beans.XMLEncoder; 
24   import java.io.ByteArrayInputStream; 
25   import java.io.ByteArrayOutputStream; 
26   import java.io.File; 
27   import java.io.FileInputStream; 
28   import java.io.IOException; 
29   import java.io.ObjectOutputStream; 
30   import java.io.Serializable; 
31    
32   import gui.In; 
33    
34    
35   public class Utils { 
36       static String Address761092 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + 
37               "<java version=\"1.4.2_03\" class=\"java.beans.XMLDecoder\"> " + 
38               " <object class=\"xml.adbk.Address\"> " + 
39               "  <void property=\"address1\"> " + 
40               "   <string>1313 Mocking bird lane</string> " + 
41               "  </void> " + 
42               "  <void property=\"city\"> " + 
43               "   <string>munsterville</string> " + 
44               "  </void> " + 
45               "  <void property=\"state\"> " + 
46               "   <string>ny</string> " + 
47               "  </void> " + 
48               "  <void property=\"zip\"> " + 
49               "   <string>12181</string> " + 
50               "  </void> " + 
51               " </object> " + 
52               "</java>"; 
53    
54       static String ShortImageBean12960684 = 
55               "H4sIAAAAAAAAAO2a+3NVVxXHF1hrIS8gQKiFqkChgsKUadJatEoFBaG+ghMtpZSSEPK+uUkgCRAeoWDq2EKhQCEPEkJSIA0JIQTo+LszzuiMf4EzjuMPjqM/6y9+11p777PPuTeZ+lN1Zuc7yT335N6zPmfttdd+nDXyd/p8U5oWVG8oX1d6oCHdvK1ub2XFKxV76//417V/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K/Kc2Y37S57J39HQvnjF6E/0kLp9kUkhqhOQE7FVKdBySfsMt45NkPsuInnn1mudw0d1Gzym7PYucPbrdWc0mH0JMwjNQHmXPoB8xCc9DWkzbNMnozK1TbzQTiX+3V0zGumqO7DFbvWIoryLGL2LGfx7qEZKDGHX2xEh0Jp8ys7UqidnyLCTRE8m4t9niZUgt6/Flc2ylfBfQe1k90of2CAk/hWQSu6aod6qit0RVGXGi3u8zeVJlbV+CLpvXuPSskumZi9BH6DHl9CbE4w6TpDJI3pC1+j7xS5wk3gLWLl/3g4QuONkzF430G+yXco+kRnYZ7Wq8Vsa9mhlItAVs1EUcF7LqvChOdMHwnMMM5GfQOox2TMIeaJQ4tSQcrUyzV/R6guSSY/FJrAf4Ps8bvZ/Q+YSnzuEuX6YXhOSkPFWvlxVzk0RsrWSQKiHRHJskuehINCajVlF752J6DzrrsSihvv6aKrDW2QSSbUJSKbsD3F8sSbXRAfmbJNHWvmx6h3pD75Upzhqdk+P3sOa0OuvoLNVpIdmMcWcL8lqHRGydkLAfqkRKsl98sjdB8pn+NKUpp7Klap0pZPnzP3/aturffZ/Mplmv2WKX8taUAj/GwP876KFgJRSshIKVULASClZCwUooWAkFK6FghULBSihYCQUroWAlFKyEgpVQsBIKVkLBSihYCQUroWAlFKyEgpVQsBIKVkLBSihYCQUroWAlFKyEgpVQsBIKVv4PC1ZCJUqoRAmVKKESJVSihEqUUIkSKlFCJUrsqXKoRAmVKKESJVSihEqUUIkStU6oRAmVKKESJVSihEqUUIkSKlFCJUqoRAmVKKESJVSihEqUUIkSKlFCJUqoRAmVKKES5b/8+Q8gBOahPWgAAA=="; 
56    
57       public static Object decodeXml(String xml) { 
58           ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes()); 
59           XMLDecoder d = new XMLDecoder(bais); 
60           return d.readObject(); 
61       } 
62    
63       public static void main(String[] args) { 
64           try { 
65               printCompactJavas(); 
66           } catch (IOException e) { 
67               e.printStackTrace(); 
68           } 
69       } 
70    
71       private static void printCompactJavas() throws IOException { 
72           File f = Futil.getReadDirFile("select a resource directory"); 
73           if (f == null) return; 
74           File fa[] = f.listFiles(new FileFilter()); 
75           for (int i = 0; i < fa.length; i++) 
76               System.out.println(getCompactJava(fa[i], fa[i].getName())); 
77    
78       } 
79    
80       private static void testCodec() { 
81           testBase64GzipEncoding(); 
82           testBase64GzipDecoding(); 
83       } 
84    
85       private static void testReadWriteXml() { 
86           Address a = Address.getSample(); 
87           writeXml(a); 
88           Object o = readXml(); 
89           System.out.println("read:" + o); 
90       } 
91    
92       private static void testXmlImageEncoding() { 
93           ShortImageBean sib = new ShortImageBean(NumImage.getImage()); 
94           System.out.println(getJava(sib)); 
95       } 
96    
97       public static String encodeImage(Image img) { 
98           ShortImageBean sib = new ShortImageBean(img); 
99           return getCompactJava(sib); 
100      } 
101   
102      public static Image decodeImage(String s) { 
103          Object o = Base64.decodeToObject(s); 
104          ShortImageBean sib = (ShortImageBean) o; 
105          return sib.getImage(); 
106      } 
107   
108      private static void testBase64GzipDecoding() { 
109          Object o = Base64.decodeToObject(ShortImageBean12960684); 
110          ShortImageBean sibTest = (ShortImageBean) o; 
111          ImageFrame imf = new ImageFrame("sib test"); 
112          imf.setImage(sibTest.getImage()); 
113          imf.setSize(200, 200); 
114          imf.show(); 
115          System.out.println(ShortImageBean12960684); 
116      } 
117   
118      private static void testBase64GzipEncodeDecoding() { 
119          ShortImageBean sib = new ShortImageBean(NumImage.getImage()); 
120          String s = 
121                  Base64.encodeObject(sib, Base64.GZIP | Base64.DONT_BREAK_LINES); 
122          Object o = Base64.decodeToObject(s); 
123          ShortImageBean sibTest = (ShortImageBean) o; 
124          ImageFrame imf = new ImageFrame("sib test"); 
125          imf.setImage(sibTest.getImage()); 
126          imf.setSize(200, 200); 
127          imf.show(); 
128          System.out.println(s); 
129      } 
130   
131      private static void testBase64GzipEncoding() { 
132          ShortImageBean sib = new ShortImageBean(NumImage.getImage()); 
133          System.out.println(getCompactJava(sib)); 
134      } 
135   
136      private static void testXmlDecoder() { 
137          Object o = decodeXml(Address761092); 
138          System.out.println("decode shows:" + o); 
139          Address a = (Address) o; 
140          a.toXml(); 
141      } 
142   
143      private static void testGetJava() { 
144          Serializable s = Address.getSample(); 
145          System.out.println(getJava(s)); 
146      } 
147   
148      public static String getUnqualifiedClassName(Object o) { 
149          String s = o.getClass().getName(); 
150          int i = s.lastIndexOf('.'); 
151          if (i == -1) return s; 
152          return s.substring(i + 1); 
153      } 
154   
155      public static String getCompactJava() { 
156          File f = Futil.getReadFile("select a resource file"); 
157          return getCompactJava(f); 
158      } 
159   
160      private static String getCompactJava(File f, String variableName) throws IOException { 
161          FileInputStream fis = new FileInputStream(f); 
162          int n = fis.available(); 
163          byte b[] = new byte[n]; 
164          fis.read(b); 
165          fis.close(); 
166          return getCompactJava(b, variableName); 
167      } 
168   
169      /** 
170       * Input a serializable object and get back a very 
171       * long string that is a base64 encoded gzipped version 
172       * of the serialized object suitable for compilation into 
173       * a Java program. 
174       * 
175       * @param object 
176       * @return 
177       */ 
178      public static String getCompactJava(Serializable object) { 
179          String instanceName = getUnqualifiedClassName(object) 
180                  + object.hashCode(); 
181          String s = "static String " + 
182                  instanceName + 
183                  "=\n\"" + 
184                  Base64.encodeObject(object, Base64.GZIP | Base64.DONT_BREAK_LINES) 
185                  + "\";"; 
186          return s; 
187      } 
188   
189      private static String getJavaVariable(String s) { 
190   
191          s = StringUtils.replaceAll(s,".","Dot"); 
192          s = StringUtils.replaceAll(s,"-","Dash"); 
193          return s; 
194      } 
195   
196      public static String getCompactJava(Serializable object, String variableName) {      
197          String javaName = getJavaVariable(variableName); 
198          String s = 
199                  "  String " + javaName+"Name" + "=\"" + variableName + "\";\n" + 
200                  "  String " + 
201                  javaName + 
202                  "=\n\"" + 
203                  Base64.encodeObject(object, Base64.GZIP | Base64.DONT_BREAK_LINES) 
204                  + "\";\n"+ 
205                  "Object o = Base64.decodeToObject(" + 
206                  javaName + 
207                  ");\n"; 
208          In.message("paste into a java program"); 
209          return s; 
210      } 
211   
212      public static String getJava(Serializable object) { 
213          String instanceName = getUnqualifiedClassName(object) 
214                  + object.hashCode(); 
215          String s = toXml(object); 
216          s = s + "</java>"; 
217          s = StringUtils.replaceAllSb(s, "\"", "\\\""); 
218          s = StringUtils.replaceAllSb(s, "\n", "\"+\n\""); 
219          StringBuffer sb = new StringBuffer(s); 
220          sb.insert(0, "private static String " + instanceName + " =\""); 
221          sb.append("\";"); 
222          return sb.toString(); 
223      } 
224   
225      public static String getHexString(Serializable object) { 
226          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
227          try { 
228              ObjectOutputStream oos = new ObjectOutputStream(baos); 
229              oos.writeObject(object); 
230              oos.flush(); 
231          } catch (IOException e) { 
232              e.printStackTrace(); 
233          } 
234          byte b[] = baos.toByteArray(); 
235          StringBuffer sb = new StringBuffer("=\""); 
236   
237          for (int i = 0; i < b.length; i++) { 
238              sb.append(Integer.toHexString(b[i])); 
239              if (i % 16 == 0) 
240                  sb.append("\"+\n\""); 
241          } 
242          return sb.toString(); 
243      } 
244   
245      public static String toXml(Serializable object) { 
246          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
247          XMLEncoder e = new XMLEncoder(baos); 
248          e.writeObject(object); 
249          e.flush(); 
250          return baos.toString(); 
251      } 
252   
253      public static Object readXml() { 
254          XMLDecoder e = 
255                  new XMLDecoder(Futil.getFileInputStream("select an xml file")); 
256          return e.readObject(); 
257      } 
258   
259      /** 
260       * write out <code>object</code> 
261       * as XML. 
262       * 
263       * @param object 
264       */ 
265      public static void writeXml(Serializable object) { 
266          XMLEncoder e = new XMLEncoder( 
267                  futils.Futil.getFileOutputStream("select xml output file")); 
268          e.writeObject(object); 
269          e.close(); 
270      } 
271   
272      /** 
273       * getParser returns a SAXParser This was 
274       * changed to reflect the latest jaxp distro. 
275       * Since it was subject to deprecation before 
276       * it is wise to isolate the feature here. 
277       * This is an example of defensive 
278       * programming. 
279       */ 
280      public static SAXParser getParser() 
281              throws 
282              org.xml.sax.SAXException, 
283              ParserConfigurationException { 
284          // The SAXParserFactory is a a 
285          // factory class that enables productions 
286          // of XML parsers: 
287          SAXParserFactory spf 
288                  = SAXParserFactory.newInstance(); 
289   
290          // setValidating (true) specifies that 
291          // the parser will validate documents 
292          // during parsing: 
293   
294          spf.setValidating(true); 
295          return spf.newSAXParser(); 
296      } 
297  } 
298