KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > hyphenation > PatternInternalParser


1 /*
2  * $Id: PatternInternalParser.java,v 1.35 2005/03/15 08:17:01 blowagie Exp $
3  * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
4  * For license details please refer to http://xml.apache.org/fop
5  */

6
7 package com.lowagie.text.pdf.hyphenation;
8
9 import com.lowagie.text.ExceptionConverter;
10 import java.io.*;
11 import java.util.ArrayList;
12 import java.util.StringTokenizer;
13
14 public class PatternInternalParser implements PatternConsumer {
15
16     PatternConsumer consumer;
17
18     public PatternInternalParser() {
19     }
20
21     public PatternInternalParser(PatternConsumer consumer) {
22         this.consumer = consumer;
23     }
24
25     public void setConsumer(PatternConsumer consumer) {
26         this.consumer = consumer;
27     }
28     
29     protected String getHyphString(InputStream is) throws IOException{
30         InputStreamReader isr = new InputStreamReader(is, "UTF8");
31         char c[] = new char[4000];
32         StringBuffer buf = new StringBuffer();
33         while (true) {
34             int n = isr.read(c);
35             if (n < 0)
36                 break;
37             buf.append(c, 0, n);
38         }
39         isr.close();
40         return buf.toString();
41     }
42
43     public void parse(String filename) {
44         InputStream is;
45         try {
46             is = new FileInputStream(filename);
47         }
48         catch (IOException ioe) {
49             throw new ExceptionConverter(ioe);
50         }
51         parse(is);
52     }
53     
54     public void parse(InputStream is) {
55         String hyphs;
56         try {
57             hyphs = getHyphString(is);
58         }
59         catch (IOException ioe) {
60             throw new ExceptionConverter(ioe);
61         }
62         parseString(hyphs);
63     }
64
65     public void parseString(String hyphs) {
66         StringTokenizer tk = new StringTokenizer(hyphs);
67         readClasses(tk);
68         readExceptions(tk);
69         readPatterns(tk);
70     }
71
72     protected void readClasses(StringTokenizer tk) {
73         String token = "";
74         while (tk.hasMoreTokens()) {
75             token = tk.nextToken();
76             if (token.equals("*"))
77                 break;
78             consumer.addClass(token);
79         }
80     }
81
82     protected void readExceptions(StringTokenizer tk) {
83         String token = "";
84         while (tk.hasMoreTokens()) {
85             token = tk.nextToken();
86             if (token.equals("*"))
87                 break;
88             String word = token;
89             ArrayList vec = new ArrayList();
90             while (tk.hasMoreTokens()) {
91                 token = tk.nextToken();
92                 if (token.equals("{")) {
93                     String t1 = tk.nextToken();
94                     if (t1.equals("N"))
95                         t1 = null;
96                     String t2 = tk.nextToken();
97                     if (t2.equals("N"))
98                         t2 = null;
99                     String t3 = tk.nextToken();
100                     if (t3.equals("N"))
101                         t3 = null;
102                     Hyphen hy = new Hyphen(t2, t1, t3);
103                     vec.add(hy);
104                 }
105                 else if (token.equals("#")) {
106                     break;
107                 }
108                 else
109                     vec.add(token);
110             }
111             consumer.addException(word, vec);
112         }
113     }
114     
115     protected void readPatterns(StringTokenizer tk) {
116         String token = "";
117         while (tk.hasMoreTokens()) {
118             token = tk.nextToken();
119             consumer.addPattern(token, tk.nextToken());
120         }
121     }
122     
123     // PatternConsumer implementation for testing purposes
124
public void addClass(String c) {
125         System.out.println("class: " + c);
126     }
127
128     public void addException(String w, ArrayList e) {
129         System.out.println("exception: " + w + " : " + e.toString());
130     }
131
132     public void addPattern(String p, String v) {
133         System.out.println("pattern: " + p + " : " + v);
134     }
135
136 /* public static void main(String[] args) throws Exception {
137         if (args.length > 0) {
138             PatternInternalParser pp = new PatternInternalParser();
139             pp.setConsumer(pp);
140             pp.parse(args[0]);
141         }
142     }*/

143
144 }
145
Popular Tags