KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > transform > AttributeExtracter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * SomeClass.java
26  *
27  * Created on July 28, 2004, 12:35 AM
28  */

29
30 package com.sun.enterprise.tools.upgrade.transform;
31
32 /**
33  *
34  * @author prakash
35  */

36 import java.io.*;
37 import java.util.*;
38 public class AttributeExtracter {
39     
40     private String JavaDoc fileName;
41     private static AttributeExtracter attrExtracter;
42     private HashMap attributeMap;
43     /** Creates a new instance of SomeClass */
44     private AttributeExtracter(String JavaDoc fileName) {
45         this.fileName = fileName;
46         attributeMap = new HashMap();
47     }
48     public static AttributeExtracter getExtracter(String JavaDoc fileName){
49         if(attrExtracter == null){
50             attrExtracter = new AttributeExtracter(fileName);
51         }
52         return attrExtracter;
53     }
54     public java.util.List JavaDoc getAttributeList(String JavaDoc elementName){
55         if(attributeMap.get(elementName) != null)
56             return (List)attributeMap.get(elementName);
57         ArrayList attrList = new ArrayList();
58         try{
59             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
60             String JavaDoc readLine = null;
61             while((readLine = reader.readLine())!= null){
62                 String JavaDoc attrLine = this.getAttributeLine(elementName, readLine);
63                 if(attrLine == null)
64                     continue;
65                 if((attrLine.length() > 0) && (!attrLine.trim().equals("")))
66                     attrList.add(this.getAttributeFromLine(attrLine));
67                 this.extractAttributes(reader, attrList);
68             }
69             this.attributeMap.put(elementName, attrList);
70         }catch(Exception JavaDoc ex){
71             ex.printStackTrace();
72         }
73         return attrList;
74     }
75     private String JavaDoc getAttributeLine(String JavaDoc elementName, String JavaDoc line){
76         if(line.startsWith("<!ATTLIST")){
77             String JavaDoc atLine = line.substring("<!ATTLIST".length()).trim();
78             StringTokenizer stk = new StringTokenizer(atLine);
79             if(stk.nextToken().equals(elementName))
80                 return atLine.substring(elementName.length());
81         }
82         return null;
83     }
84     private String JavaDoc getAttributeFromLine(String JavaDoc attrLine){
85         StringTokenizer stk = new StringTokenizer(attrLine);
86         return stk.nextToken();
87     }
88     private void extractAttributes(BufferedReader reader, List attrList) throws Exception JavaDoc{
89         for(int i=0; i<50; i++){
90             String JavaDoc attrLine = reader.readLine().trim();
91             attrList.add(getAttributeFromLine(attrLine.trim()));
92             if(attrLine.endsWith(">"))
93                 break;
94         }
95     }
96     public static void main(String JavaDoc[] args){
97      /* //AttributeExtracter extracter = new AttributeExtracter("c:\\temp\\sundomain.dtd");
98         AttributeExtracter extracter = AttributeExtracter.getExtracter("/home/paradhya/junk/sundomain.dtd");
99         List list = extracter.getAttributeList("http-listener");
100         System.out.println(list);
101         for(int i=0; i<list.size(); i++){
102             System.out.println(list.get(i));
103         }
104       */

105     }
106 }
107
Popular Tags