KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > EntityParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beansdev;
21
22 import java.io.*;
23 import java.util.*;
24 /**
25  * EntityParser.java - parses the DTD file for entity declarations and creates new Reader
26  * that replaces the entity references with values
27  *
28  * Created on June 11, 2005 AM
29  * @author mkuchtiak
30  */

31 public class EntityParser {
32     private java.util.Map JavaDoc entityMap;
33     private File fileName;
34     /** Creates a new instance of EntityParser */
35     public EntityParser(File fileName) {
36         this.fileName=fileName;
37         entityMap = new java.util.HashMap JavaDoc();
38     }
39     /** Parses file for ENTITY declaration, creates map with entities
40      */

41     public void parse() throws IOException {
42         BufferedReader br = new BufferedReader(new FileReader(fileName));
43         String JavaDoc line = null;
44         while ((line=br.readLine())!=null) {
45             int startPos = line.indexOf("<!ENTITY ");
46             if (startPos>=0) addEntity(br,line.substring(startPos+9));
47         }
48         br.close();
49     }
50     
51     private void addEntity(BufferedReader br, String JavaDoc line) throws IOException {
52         StringTokenizer tok = new StringTokenizer(line);
53         if (!tok.hasMoreTokens()) return;
54         String JavaDoc percentage = tok.nextToken();
55         if (!"%".equals(percentage)) return; //incorrect ENTITY declaration (missing %)
56
if (!tok.hasMoreTokens()) return; //incorrect ENTITY declaration (missing entity name)
57

58     // cut the first part including entity key
59
String JavaDoc key = tok.nextToken();
60         int valueStartPos = line.indexOf(key)+key.length();
61         String JavaDoc rest = line.substring(valueStartPos);
62     
63     // looking for starting quotes
64
valueStartPos = rest.indexOf("\"");
65     if (valueStartPos<0) return;
66     
67     // looking for entity value
68
rest = rest.substring(valueStartPos+1);
69     String JavaDoc value = resolveValue (rest,br);
70
71         // write ENTITY into map
72
if (value!=null) {
73         int refStart = value.indexOf("%");
74         int refEnd = value.indexOf(";");
75         if (refStart>=0 && refEnd>refStart) { //references other entity
76
String JavaDoc entityKey = value.substring(refStart+1,refEnd);
77                 String JavaDoc val = (String JavaDoc)entityMap.get(entityKey);
78         if (val!=null) {
79             String JavaDoc newValue = value.substring(0,refStart)+val+value.substring(refEnd+1);
80             System.out.println("found ENTITY: % "+key+" \""+newValue+"\"");
81             entityMap.put(key,newValue);
82         }
83             } else {
84                 System.out.println("found ENTITY: % "+key+" \""+value+"\"");
85                 entityMap.put(key,value);
86             }
87         }
88     }
89     
90     private String JavaDoc resolveValue(String JavaDoc lineRest, BufferedReader br) throws IOException {
91     // looking for closing quotes
92
int index = lineRest.indexOf("\"");
93     if (index>=0) return lineRest.substring(0,index);
94     // value across multiple lines
95
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(lineRest);
96         buf.append("\n");
97     int ch=br.read();
98         while ( ch!=(int)'"' && ch!=(int)'>' && ch!=-1 ) {
99         buf.append((char)ch);
100         ch=br.read();
101         }
102     return buf.toString();
103     }
104
105     private boolean containsBlank(String JavaDoc s) {
106         for (int i=0;i<s.length();i++) {
107             if (' '==s.charAt(i)) return true;
108         }
109         return false;
110     }
111
112     /** Creates a StringReader that removes all ENTITY declarations
113      * and replaces entity references with corresponding values
114      */

115     public Reader getReader() throws IOException {
116         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
117         BufferedReader br = new BufferedReader(new FileReader(fileName));
118         String JavaDoc line = null;
119         while ((line=br.readLine())!=null) {
120             // removing line(s) with entity declaration
121
if (line.indexOf("<!ENTITY ")>=0) line = removeEntityDeclaration(line,br);
122             // searches for entity reference and replace it with value
123
int pos = line.indexOf("%");
124             if (pos>=0) {
125                 StringTokenizer tok = new StringTokenizer(line.substring(pos),";%");
126                 while (tok.hasMoreTokens()) {
127                     String JavaDoc key = tok.nextToken();
128                     if (key.length()>0 && !containsBlank(key)) {
129                         String JavaDoc value = (String JavaDoc)entityMap.get(key);
130                         if (value!=null) line = line.replaceAll("%"+key+";",value);
131                     }
132                 }
133             }
134             if (line.length()>0) buf.append(line);
135         }
136         br.close();
137         return new StringReader(buf.toString());
138     }
139     
140     /** Removing line(s) containing ENTITY declaration
141      */

142     private String JavaDoc removeEntityDeclaration(String JavaDoc line,BufferedReader br) throws IOException {
143         int start = line.indexOf("<!ENTITY ");
144         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
145         if (start>0) buf.append(line.substring(0, start));
146         int endPos = line.indexOf(">", start);
147         if (endPos>0) {
148             buf.append(line.substring(endPos+1));
149             return buf.toString();
150         }
151         String JavaDoc ln=null;
152         while (endPos<0 && (ln=br.readLine())!=null) {
153             endPos = ln.indexOf(">");
154             if (endPos>=0) {
155                 buf.append(ln.substring(endPos+1));
156             }
157         }
158         return buf.toString();
159     }
160     
161 }
162
Popular Tags