KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > metadata > MetaHandler


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23
24 package org.xquark.xquery.metadata;
25
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.helpers.AttributesImpl JavaDoc;
32 import org.xquark.schema.Schema;
33 import org.xquark.schema.SchemaReader;
34 import org.xquark.util.DefaultXMLReader;
35 import org.xquark.xpath.XTreeReader;
36
37
38 public class MetaHandler
39 extends DefaultXMLReader {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42     
43     private MetaDataImpl metadata = null;
44     private MetaWrapper metawrapper = null;
45     private MetaCollection metacollection = null;
46     private AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
47     private String JavaDoc label = "ipo";
48     
49     public MetaHandler(MetaDataImpl metadata) { this.metadata = metadata; }
50     
51     public MetaHandler(MetaWrapper metawrapper) { this.metawrapper = metawrapper; }
52     
53     public MetaHandler(MetaCollection metacollection) { this.metacollection = metacollection; }
54     
55     /**
56      * SystemId is ignored and the XTree passed at construction time is parsed
57      */

58     public void parse(String JavaDoc systemId) throws SAXException JavaDoc, java.io.IOException JavaDoc {
59         if (metacollection != null) { browseMetaCollection(metacollection); }
60         else {
61             if (metawrapper != null) { browseMetaWrapper(metawrapper); }
62             else {
63                 if (metadata != null) { browseMetaData(metadata); }
64             }
65         }
66     }
67     
68     public void browseMetaCollection(MetaCollection metacollection) throws SAXException JavaDoc, java.io.IOException JavaDoc {
69         atts.addAttribute("", "name", "", "CDATA", metacollection.getCollectionName());
70         contentHandler.startElement("", "collection", "", atts);
71         atts.clear() ;
72         XTreeReader reader = new XTreeReader(metacollection.getXTree());
73         reader.setFeature(XTreeReader.FRAGMENT_FEATURE, true) ;
74         reader.setContentHandler(contentHandler) ;
75         reader.parse("");
76         contentHandler.endElement("", "collection", "");
77     }
78     
79     public void startPrint(String JavaDoc sourcename, String JavaDoc label) throws SAXException JavaDoc, java.io.IOException JavaDoc {
80         contentHandler.startDocument() ;
81         
82         atts.addAttribute("xmlns", "xsi", "", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");
83         atts.addAttribute("xmlns", label, "", "CDATA", "http://www.xquark.org/WRAPPERMETADATA");
84         atts.addAttribute("xsi", "schemaLocation", "", "CDATA", "http://www.xquark.org/XMLDBC/Metadata XMLDBCMetaData.xsd");
85         atts.addAttribute("", "source", "", "CDATA", sourcename);
86         contentHandler.startElement(label, "metadata", "", atts);
87         
88         atts.clear() ;
89         
90         contentHandler.startElement("", "schemas", "", atts);
91         Collection JavaDoc schemas;
92         
93         if (metawrapper != null) {
94             schemas = metawrapper.getMetaData().getSchemaManager().getSchemas();
95         }
96         else {
97             schemas = metadata.getSchemaManager().getSchemas();
98         }
99         
100         if (schemas != null) {
101             for (Iterator JavaDoc iterator = schemas.iterator(); iterator.hasNext();) {
102                 Schema schema = (Schema)iterator.next();
103                 if (schema == null) {
104                     continue;
105                 }
106                 String JavaDoc namespace = schema.getNamespace();
107                 if (namespace.startsWith("http://www.w3.org")) {
108                     continue;
109                 }
110                 //contentHandler.startElement("", "schemadefinition", "", atts);
111
SchemaReader schemaReader = new SchemaReader(null);
112                 schemaReader.setContentHandler(contentHandler) ;
113                 schemaReader.parse(schema, false);
114                 //contentHandler.endElement("", "schemadefinition", "");
115
}
116         }
117         
118         contentHandler.endElement("", "schemas", "");
119     }
120     
121     private void endPrint(String JavaDoc label) throws SAXException JavaDoc, java.io.IOException JavaDoc {
122         contentHandler.endElement(label, "metadata", "");
123     }
124     
125     public void browseMetaWrapper(MetaWrapper metawrapper) throws SAXException JavaDoc, java.io.IOException JavaDoc {
126         startPrint(metawrapper.getSourceName(), label);
127         atts.clear() ;
128         contentHandler.startElement("", "collections", "", atts);
129         Iterator JavaDoc values = metawrapper.getMetaCollections().values().iterator();
130         
131         while (values.hasNext()) {
132             MetaCollection metacollection = (MetaCollection)values.next();
133             browseMetaCollection(metacollection);
134         }
135         
136         contentHandler.endElement("", "collections", "");
137         endPrint(label);
138     }
139     
140     public void browseMetaData(MetaDataImpl metadata) throws SAXException JavaDoc, java.io.IOException JavaDoc {
141         atts.clear() ;
142         startPrint("ACCESSOR-SOURCE", label);
143         contentHandler.startElement("", "collections", "", atts);
144         
145         for (Iterator JavaDoc e = metadata.getMetaWrappers().values().iterator(); e.hasNext();) {
146             MetaWrapper wrapperi = (MetaWrapper)e.next();
147             HashMap JavaDoc metacollections = wrapperi.getMetaCollections();
148             Iterator JavaDoc values = metacollections.values().iterator();
149             
150             while (values.hasNext()) {
151                 MetaCollection metacollection = (MetaCollection)values.next();
152                 browseMetaCollection(metacollection);
153             }
154         }
155         
156         contentHandler.endElement("", "collections", "");
157         endPrint(label);
158     }
159     
160 }
161
162
163
164
Popular Tags