View Javadoc

1   // Copyright 2000, CERN, Geneva, Switzerland and University of Santa Cruz, California, U.S.A.
2   package org.freehep.graphics2d;
3   
4   
5   /**
6    * 
7    * @author Mark Donszelmann
8    * @version $Id: TagHandler.java 8584 2006-08-10 23:06:37Z duns $
9    */
10  public class TagHandler {
11  
12      public TagHandler() {
13      }
14  
15      /**
16       * parses string and calls methods for every tag and every not recognized
17       * entity The characters < and > have to be written as &lt; and &gt; while
18       * the & is written as &amp;
19       * 
20       * The following three methods are called: defaultEntity(entity) for &amp;
21       * &lt; &gt; &quot; &apos; entity(entity) for all other entities
22       * openTag(tag) for all <tags> endTag(tag) for all </tags> text(text) for
23       * all text
24       * 
25       * The startTag, endTag and text methods returns a string which is added to
26       * the fully parsed string.
27       * 
28       * Strings returned from the entity methods will show up in the text methods
29       * parameter.
30       * 
31       * It returns the fully parsed string, including any additions made by the
32       * three methods above.
33       */
34      public String parse(TagString string) {
35          String src = string.toString();
36          StringBuffer parsedString = new StringBuffer();
37          StringBuffer textString = new StringBuffer();
38          int i = 0;
39          int p = 0;
40          try {
41              while (i < src.length()) {
42                  switch (src.charAt(i)) {
43                  case '&':
44                      // handle entities
45                      // look for closing ';'
46                      i++;
47                      p = i;
48                      while (src.charAt(i) != ';') {
49                          i++;
50                      }
51                      String ent = src.substring(p, i);
52                      if (ent.equals("amp") || ent.equals("gt")
53                              || ent.equals("lt") || ent.equals("quot")
54                              || ent.equals("apos")) {
55                          textString.append(defaultEntity(ent));
56                      } else {
57                          textString.append(entity(ent));
58                      }
59                      break;
60  
61                  case '<':
62                      // handle tags
63  
64                      // handle any outstanding text
65                      if (textString.length() > 0) {
66                          parsedString.append(text(textString.toString()));
67                          textString = new StringBuffer();
68                      }
69  
70                      // look for closing '>'
71                      i++;
72                      p = i;
73                      while (src.charAt(i) != '>') {
74                          i++;
75                      }
76  
77                      if (src.charAt(p) == '/') {
78                          parsedString.append(closeTag(src.substring(p + 1, i)));
79                      } else {
80                          parsedString.append(openTag(src.substring(p, i)));
81                      }
82                      break;
83                  default:
84                      // just move the pointer
85                      textString.append(src.charAt(i));
86                      break;
87                  } // switch
88                  i++;
89              } // while
90  
91          } catch (ArrayIndexOutOfBoundsException aoobe) {
92              // just abort, but give most of the string
93              parsedString.append("!PARSEERROR!");
94          }
95  
96          // final part
97          if (textString.length() > 0) {
98              parsedString.append(text(textString.toString()));
99          }
100         return parsedString.toString();
101     }
102 
103     protected String defaultEntity(String entity) {
104         StringBuffer dst = new StringBuffer();
105         if (entity.equals("amp")) {
106             dst.append('&');
107         } else if (entity.equals("gt")) {
108             dst.append('>');
109         } else if (entity.equals("lt")) {
110             dst.append('<');
111         } else if (entity.equals("quot")) {
112             dst.append('"');
113         } else if (entity.equals("apos")) {
114             dst.append('\'');
115         }
116         return dst.toString();
117     }
118 
119     protected String entity(String entity) {
120         StringBuffer dst = new StringBuffer();
121         dst.append('&');
122         dst.append(entity);
123         dst.append(';');
124         return dst.toString();
125     }
126 
127     protected String openTag(String tag) {
128         StringBuffer dst = new StringBuffer();
129         dst.append('<');
130         dst.append(tag);
131         dst.append('>');
132         return dst.toString();
133     }
134 
135     protected String closeTag(String tag) {
136         StringBuffer dst = new StringBuffer();
137         dst.append("</");
138         dst.append(tag);
139         dst.append('>');
140         return dst.toString();
141     }
142 
143     protected String text(String text) {
144         return text;
145     }
146 
147     public static void main(String[] args) {
148         String text = "&lt;Vector<sup><b>Graphics</b></sup> &amp; Card<i><sub>Adapter</sub></i>&gt;";
149 
150         TagString s = new TagString(text);
151         TagHandler handler = new TagHandler();
152         System.out.println("\"" + s + "\"");
153         System.out.println(handler.parse(s));
154     }
155 }