View Javadoc

1   // Copyright 2004, FreeHEP.
2   package org.freehep.graphicsio.pdf;
3   
4   import java.text.DecimalFormat;
5   import java.util.Calendar;
6   
7   import org.freehep.util.ScientificFormat;
8   
9   /**
10   * Utility functions for the PDFWriter. This class handles escaping of strings,
11   * formatting of dates, ...
12   * <p>
13   * 
14   * @author Mark Donszelmann
15   * @version $Id: PDFUtil.java 8584 2006-08-10 23:06:37Z duns $
16   */
17  public class PDFUtil implements PDFConstants {
18  
19      // static class
20      private PDFUtil() {
21      }
22  
23      public static String escape(String string) {
24          StringBuffer escape = new StringBuffer();
25  
26          for (int i = 0; i < string.length(); i++) {
27              char c = string.charAt(i);
28              switch (c) {
29              case '(':
30              case ')':
31              case '\\':
32                  escape.append('\\');
33                  escape.append(c);
34                  break;
35              default:
36                  escape.append(c);
37                  break;
38              }
39          }
40          return escape.toString();
41      }
42  
43      public static String date(Calendar date) {
44          int offset = date.get(Calendar.ZONE_OFFSET)
45                  + date.get(Calendar.DST_OFFSET);
46  
47          String tz;
48          if (offset == 0) {
49              tz = "Z";
50          } else {
51              DecimalFormat fmt = new DecimalFormat("00");
52              int tzh = Math.abs(offset / 3600000);
53              int tzm = Math.abs(offset % 3600000);
54              if (offset > 0) {
55                  tz = "+" + fmt.format(tzh) + "'" + fmt.format(tzm) + "'";
56              } else {
57                  tz = "-" + fmt.format(tzh) + "'" + fmt.format(tzm) + "'";
58              }
59          }
60          return "(D:" + dateFormat.format(date.getTime()) + tz + ")";
61      }
62  
63      private static final ScientificFormat scientific = new ScientificFormat(5,
64              100, false);
65  
66      public static String fixedPrecision(double v) {
67          return scientific.format(v);
68      }
69  
70  }