/[webpac]/trunk/openisis/org/openisis/XmlToRec.java
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/openisis/org/openisis/XmlToRec.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 237 - (hide annotations)
Mon Mar 8 17:43:12 2004 UTC (20 years, 3 months ago) by dpavlin
Original Path: openisis/current/org/openisis/XmlToRec.java
File size: 7104 byte(s)
initial import of openisis 0.9.0 vendor drop

1 dpavlin 237 /*
2     openisis - an open implementation of the CDS/ISIS database
3     Version 0.8.x (patchlevel see file Version)
4     Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org
5    
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10    
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15    
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19    
20     see README for more information
21     EOH */
22    
23    
24     package org.openisis;
25    
26     /**
27     convert Xmldocuments to Record
28     */
29    
30     import org.xml.sax.*;
31     import org.xml.sax.helpers.*;
32     import java.io.*;
33     import java.util.*;
34    
35    
36     public class XmlToRec {
37    
38     private XMLReader myReader;
39     private HashMap myTags;
40     private HashMap myAttributes;
41     private int myMaxTag;
42     private int myMaxRowId;
43     private OIHandler myHandler;
44    
45     public XmlToRec(XMLReader reader) {
46     myReader=reader;
47     myTags=new HashMap();
48     myAttributes=new HashMap();
49     myMaxTag=0;
50     myMaxRowId=0;
51     reader.setContentHandler(myHandler=new OIHandler());
52     }
53    
54     public Fdt getFdt() {
55     if (myTags.size()>0) {
56     Iterator it=myTags.entrySet().iterator();
57     Map.Entry e;
58     ArrayList fdl=new ArrayList();
59     List att;
60     String atts;
61     while(it.hasNext()) {
62     e=(Map.Entry)it.next();
63     att=(List)myAttributes.get(e.getKey());
64     if (att!=null && att.size()>0) {
65     atts="";
66     for(int i=0;i<att.size();i++) atts=atts+((char)('a'+i));
67     } else atts=null;
68     fdl.add(new Fdt.Fd(((Integer)e.getValue()).intValue(),Fdt.FTX,Integer.MAX_VALUE,true,(String)e.getKey(),atts,""));
69     }
70     Fdt.Fd fds[]=new Fdt.Fd[fdl.size()];
71     for(int i=0;i<fds.length;i++) fds[i]=(Fdt.Fd)fdl.get(i);
72     Fdt fdt=new Fdt(fds);
73     return fdt;
74     } else return null;
75     }
76    
77     public int getTag(List path) {
78     StringBuffer tp=new StringBuffer();
79     String s;
80     Integer ti;
81     /*for(int i=0;i<path.size();i++) tp.append(((String)path.get(i)).toLowerCase());
82     s=tp.toString();*/
83     s=(String)path.get(path.size()-1);
84     if ((ti=(Integer)myTags.get(s))!=null) {
85     return ti.intValue();
86     }
87     myMaxTag++;
88     myTags.put(s,new Integer(myMaxTag));
89     return myMaxTag;
90     }
91    
92     public char getAttTag(List path,String att) {
93     StringBuffer tp=new StringBuffer();
94     String s;
95     //Integer ti;
96     //for(int i=0;i<path.size();i++) tp.append(((String)path.get(i)).toLowerCase());
97     s=(String)path.get(path.size()-1);
98     ArrayList li;
99     if ((li=(ArrayList)myAttributes.get(s))==null) {
100     li=new ArrayList();
101     myAttributes.put(s,li);
102     }
103     att=att.toLowerCase();
104     int tag;
105     tag=li.indexOf(att);
106     if (tag<0) {
107     tag=li.size();
108     li.add(att);
109    
110     }
111     return (char)(((int)'a')+tag);
112     }
113    
114     public Rec toRec(Reader reader) {
115     try {
116     myReader.parse(new InputSource(reader));
117     } catch(Exception e) {
118     e.printStackTrace();
119     return null;
120     }
121     return myHandler.getRecord();
122     }
123    
124     public Rec toRec(String xml) {
125     return toRec(new StringReader(xml));
126     }
127    
128     public int getNextRowId() {
129     return ++myMaxRowId;
130     }
131    
132    
133     class OINode {
134     String value=null;
135     HashMap attributes=null;
136     OINode() {
137    
138     }
139     }
140    
141     class OIHandler extends DefaultHandler {
142    
143     private Rec myRec;
144     private ArrayList myPath;
145     private ArrayList myNodes;
146     private ArrayList mySubRec;
147    
148     public void startDocument()
149     throws SAXException {
150     myPath=new ArrayList();
151     myNodes=new ArrayList();
152     mySubRec=new ArrayList();
153     myRec=new Rec(getNextRowId());
154     }
155    
156     /*public void setRecord(Rec rec) {
157     myRec=rec;
158     }*/
159    
160     public Rec getRecord() {
161     return myRec;
162     }
163    
164     public void startElement(java.lang.String uri,
165     java.lang.String localName,
166     java.lang.String qName,
167     Attributes attributes) throws SAXException
168     {
169     myPath.add(localName);
170     OINode node;
171     StringBuffer value=new StringBuffer();
172     //myNodes.add(node=new OINode());
173     if (attributes.getLength()>0) {
174     //node.attributes=new HashMap();
175     for(int i=0;i<attributes.getLength();i++) {
176     //node.attributes.put(new Character(getAttTag(myPath,attributes.getLocalName(i))),attributes.getValue(i));
177     value.append('^');
178     value.append(new Character(getAttTag(myPath,attributes.getLocalName(i))));
179     value.append(attributes.getValue(i));
180     }
181     }
182     /*if (mySubRec.size()>0) {
183     Object tO=mySubRec.get(mySubRec.size()-1);
184     if (tO instanceof Field) {
185     mySubRec.remove(mySubRec.size()-1);
186     Rec r=new Rec(0);
187     r.append((Field)tO);
188     mySubRec.add(r);
189     }
190     }*/
191     Rec r=new Rec(0);
192     r.append(new Field(getTag(myPath),value.length()==0?null:value.toString()));
193     mySubRec.add(r);
194     super.startElement(uri,localName,qName,attributes);
195     }
196    
197    
198     public void endElement(java.lang.String uri,
199     java.lang.String localName,
200     java.lang.String qName) throws SAXException {
201     if (mySubRec.size()>=2) {
202     Object tO=mySubRec.get(mySubRec.size()-1);
203     Rec r=(Rec)mySubRec.get(mySubRec.size()-2);
204     if (tO instanceof Field) {
205     r.append((Field)tO);
206     } else {
207     //Rec r2=new Rec(r.getRowId());
208     //r2.wrap((Rec)tO,Integer.MAX_VALUE);
209     r.wrap((Rec)tO,Integer.MAX_VALUE);
210     }
211     Rec r2=new Rec(r.getRowId());
212     r2.append(r.getFields(0,1));
213     r2.wrap(new Rec(0,r.getFields(1,r.getLen())),Integer.MAX_VALUE);
214     //r2.wrap(r,Integer.MAX_VALUE);
215     mySubRec.set(mySubRec.size()-2,r2);
216     } else {
217     if (mySubRec.size()==1) {
218    
219     myRec=(Rec)mySubRec.get(mySubRec.size()-1);
220     //myRec=new Rec(r.getRowId());
221     //myRec.wrap(r,Integer.MAX_VALUE);
222     //myRec=(Rec)mySubRec.get(mySubRec.size()-1);
223     }
224     }
225     myPath.remove(myPath.size()-1);
226     //myNodes.remove(myNodes.size()-1);
227     mySubRec.remove(mySubRec.size()-1);
228     super.endElement(uri,localName,qName);
229     }
230    
231     public void characters(char[] ch,
232     int start,
233     int length)
234     throws SAXException {
235     //OINode node=(OINode)myNodes.get(myPath.size()-1);
236     Object tO=mySubRec.get(mySubRec.size()-1);
237     String value=null;
238     Field f=null;
239     Rec r=null;
240     if (tO instanceof Field) {
241     f=(Field)tO;
242     value=f.val;
243     } else {
244     r=(Rec)tO;
245     f=r.get(0);
246     value=f.val;
247     }
248     if (value==null || value.length()==0) {
249     value=new String(ch,start,length);
250     } else {
251     value=new String(ch,start,length)+value;
252     }
253     if (r==null) {
254     mySubRec.set(mySubRec.size()-1,new Field(f.tag,value));
255     } else {
256     r.set(0,new Field(f.tag,value));
257     }
258     }
259    
260     public void endDocument() throws SAXException {
261     //myRec=null;
262     super.endDocument();
263     }
264    
265     }
266    
267     }

  ViewVC Help
Powered by ViewVC 1.1.26