1 23 24 29 30 package com.sun.enterprise.tools.upgrade.transform; 31 32 36 import java.io.*; 37 import java.util.*; 38 public class AttributeExtracter { 39 40 private String fileName; 41 private static AttributeExtracter attrExtracter; 42 private HashMap attributeMap; 43 44 private AttributeExtracter(String fileName) { 45 this.fileName = fileName; 46 attributeMap = new HashMap(); 47 } 48 public static AttributeExtracter getExtracter(String fileName){ 49 if(attrExtracter == null){ 50 attrExtracter = new AttributeExtracter(fileName); 51 } 52 return attrExtracter; 53 } 54 public java.util.List getAttributeList(String 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 readLine = null; 61 while((readLine = reader.readLine())!= null){ 62 String 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 ex){ 71 ex.printStackTrace(); 72 } 73 return attrList; 74 } 75 private String getAttributeLine(String elementName, String line){ 76 if(line.startsWith("<!ATTLIST")){ 77 String 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 getAttributeFromLine(String attrLine){ 85 StringTokenizer stk = new StringTokenizer(attrLine); 86 return stk.nextToken(); 87 } 88 private void extractAttributes(BufferedReader reader, List attrList) throws Exception { 89 for(int i=0; i<50; i++){ 90 String attrLine = reader.readLine().trim(); 91 attrList.add(getAttributeFromLine(attrLine.trim())); 92 if(attrLine.endsWith(">")) 93 break; 94 } 95 } 96 public static void main(String [] args){ 97 105 } 106 } 107 | Popular Tags |