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
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.
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.
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
20 see README for more information
27 convert Xmldocuments to Record
31 import org.xml.sax.helpers.*;
36 public class XmlToRec {
38 private XMLReader myReader;
39 private HashMap myTags;
40 private HashMap myAttributes;
42 private int myMaxRowId;
43 private OIHandler myHandler;
45 public XmlToRec(XMLReader reader) {
48 myAttributes=new HashMap();
51 reader.setContentHandler(myHandler=new OIHandler());
55 if (myTags.size()>0) {
56 Iterator it=myTags.entrySet().iterator();
58 ArrayList fdl=new ArrayList();
62 e=(Map.Entry)it.next();
63 att=(List)myAttributes.get(e.getKey());
64 if (att!=null && att.size()>0) {
66 for(int i=0;i<att.size();i++) atts=atts+((char)('a'+i));
68 fdl.add(new Fdt.Fd(((Integer)e.getValue()).intValue(),Fdt.FTX,Integer.MAX_VALUE,true,(String)e.getKey(),atts,""));
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);
77 public int getTag(List path) {
78 StringBuffer tp=new StringBuffer();
81 /*for(int i=0;i<path.size();i++) tp.append(((String)path.get(i)).toLowerCase());
83 s=(String)path.get(path.size()-1);
84 if ((ti=(Integer)myTags.get(s))!=null) {
88 myTags.put(s,new Integer(myMaxTag));
92 public char getAttTag(List path,String att) {
93 StringBuffer tp=new StringBuffer();
96 //for(int i=0;i<path.size();i++) tp.append(((String)path.get(i)).toLowerCase());
97 s=(String)path.get(path.size()-1);
99 if ((li=(ArrayList)myAttributes.get(s))==null) {
101 myAttributes.put(s,li);
103 att=att.toLowerCase();
111 return (char)(((int)'a')+tag);
114 public Rec toRec(Reader reader) {
116 myReader.parse(new InputSource(reader));
117 } catch(Exception e) {
121 return myHandler.getRecord();
124 public Rec toRec(String xml) {
125 return toRec(new StringReader(xml));
128 public int getNextRowId() {
135 HashMap attributes=null;
141 class OIHandler extends DefaultHandler {
144 private ArrayList myPath;
145 private ArrayList myNodes;
146 private ArrayList mySubRec;
148 public void startDocument()
149 throws SAXException {
150 myPath=new ArrayList();
151 myNodes=new ArrayList();
152 mySubRec=new ArrayList();
153 myRec=new Rec(getNextRowId());
156 /*public void setRecord(Rec rec) {
160 public Rec getRecord() {
164 public void startElement(java.lang.String uri,
165 java.lang.String localName,
166 java.lang.String qName,
167 Attributes attributes) throws SAXException
169 myPath.add(localName);
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));
178 value.append(new Character(getAttTag(myPath,attributes.getLocalName(i))));
179 value.append(attributes.getValue(i));
182 /*if (mySubRec.size()>0) {
183 Object tO=mySubRec.get(mySubRec.size()-1);
184 if (tO instanceof Field) {
185 mySubRec.remove(mySubRec.size()-1);
192 r.append(new Field(getTag(myPath),value.length()==0?null:value.toString()));
194 super.startElement(uri,localName,qName,attributes);
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) {
207 //Rec r2=new Rec(r.getRowId());
208 //r2.wrap((Rec)tO,Integer.MAX_VALUE);
209 r.wrap((Rec)tO,Integer.MAX_VALUE);
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);
217 if (mySubRec.size()==1) {
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);
225 myPath.remove(myPath.size()-1);
226 //myNodes.remove(myNodes.size()-1);
227 mySubRec.remove(mySubRec.size()-1);
228 super.endElement(uri,localName,qName);
231 public void characters(char[] ch,
234 throws SAXException {
235 //OINode node=(OINode)myNodes.get(myPath.size()-1);
236 Object tO=mySubRec.get(mySubRec.size()-1);
240 if (tO instanceof Field) {
248 if (value==null || value.length()==0) {
249 value=new String(ch,start,length);
251 value=new String(ch,start,length)+value;
254 mySubRec.set(mySubRec.size()-1,new Field(f.tag,value));
256 r.set(0,new Field(f.tag,value));
260 public void endDocument() throws SAXException {