KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > property > model > impl > PropertyModelFactoryImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * PropertyModelFactoryImpl.java
22  *
23  * Created on January 26, 2007, 11:36 AM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.property.model.impl;
30
31 import java.net.URL JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 import javax.xml.XMLConstants JavaDoc;
40 import javax.xml.namespace.QName JavaDoc;
41 import javax.xml.transform.dom.DOMSource JavaDoc;
42 import javax.xml.validation.SchemaFactory JavaDoc;
43 import javax.xml.validation.Validator JavaDoc;
44
45 import org.netbeans.modules.xml.schema.model.Annotation;
46 import org.netbeans.modules.xml.schema.model.AppInfo;
47 import org.netbeans.modules.xml.schema.model.GlobalElement;
48 import org.netbeans.modules.xml.schema.model.Schema;
49 import org.netbeans.modules.xml.schema.model.SchemaModel;
50 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
51 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementsFactory;
52 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.XMLSchemaFileInfo;
53 import org.netbeans.modules.xml.wsdl.ui.property.model.ElementProperties;
54 import org.netbeans.modules.xml.wsdl.ui.property.model.PropertyModelException;
55 import org.netbeans.modules.xml.wsdl.ui.property.model.PropertyModelFactory;
56 import org.w3c.dom.Element JavaDoc;
57 import org.w3c.dom.Node JavaDoc;
58 import org.w3c.dom.NodeList JavaDoc;
59 import org.xml.sax.ErrorHandler JavaDoc;
60 import org.xml.sax.SAXException JavaDoc;
61 import org.xml.sax.SAXParseException JavaDoc;
62
63 /**
64  *
65  * @author radval
66  */

67 public class PropertyModelFactoryImpl extends PropertyModelFactory {
68    
69     private Logger JavaDoc logger = Logger.getLogger(PropertyModelFactoryImpl.class.getName());
70     
71     private Map JavaDoc<QName JavaDoc, ElementProperties> elementToPropertyMap = new HashMap JavaDoc<QName JavaDoc, ElementProperties>();
72     
73     private Exception JavaDoc lastError;
74     
75     private QName JavaDoc lastElement;
76     
77     private MyErrorHandler errorHandler = new MyErrorHandler();
78         
79     private javax.xml.validation.Schema JavaDoc propertySchema = null;
80     
81     private URL JavaDoc propertySchemaUrl = PropertyModelFactoryImpl.class.getResource("/org/netbeans/modules/xml/wsdl/ui/property/model/propertyCustomization.xsd");
82         
83     
84     /** Creates a new instance of PropertyModelFactoryImpl */
85     public PropertyModelFactoryImpl() throws PropertyModelException {
86         try {
87             SchemaFactory JavaDoc sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
88             sf.setErrorHandler(errorHandler);
89             propertySchema = sf.newSchema(propertySchemaUrl);
90         } catch(Exception JavaDoc ex) {
91             throw new PropertyModelException(ex);
92         }
93     }
94
95     @Override JavaDoc
96     public ElementProperties getElementProperties(QName JavaDoc elementQName) throws PropertyModelException {
97         ElementProperties ep = elementToPropertyMap.get(elementQName);
98
99         if(ep != null) {
100             return ep;
101         }
102         
103         try {
104             WSDLExtensibilityElements elements = WSDLExtensibilityElementsFactory.getInstance().getWSDLExtensibilityElements();
105             XMLSchemaFileInfo info = elements.getXMLSchemaFileInfo(elementQName.getNamespaceURI());
106             if(info != null) {
107                 Schema s = info.getSchema();
108                 if(s != null) {
109                     GlobalElement ge = findElement(elementQName, s);
110                     if(ge != null) {
111                         processGlobalElementAnnotation(elementQName, ge);
112                     }
113                 }
114             }
115         } catch(Throwable JavaDoc ex) {
116             throw new PropertyModelException(ex);
117         }
118         
119         ep = elementToPropertyMap.get(elementQName);
120         return ep;
121     }
122     
123     private GlobalElement findElement(QName JavaDoc elementQName, Schema schema) throws Exception JavaDoc {
124         GlobalElement ge = null;
125         SchemaModel sModel = schema.getModel();
126         ge = sModel.findByNameAndType(elementQName.getLocalPart(), GlobalElement.class);
127         return ge;
128     }
129     
130     void processGlobalElementAnnotation(QName JavaDoc elementQName, GlobalElement ge) throws Exception JavaDoc {
131         Annotation a = ge.getAnnotation();
132         if(a != null) {
133             Collection JavaDoc<AppInfo> appInfos = a.getAppInfos();
134             processAppInfos(elementQName, appInfos);
135         }
136     }
137     
138     void processAppInfos(QName JavaDoc elementQName, Collection JavaDoc<AppInfo> appInfos) throws Exception JavaDoc {
139         Iterator JavaDoc<AppInfo> it = appInfos.iterator();
140         while(it.hasNext()) {
141             AppInfo ainfo = it.next();
142             ElementProperties ep = processAppInfo(elementQName, ainfo);
143             if(ep != null) {
144                 elementToPropertyMap.put(elementQName, ep);
145             }
146         }
147     }
148     
149     ElementProperties processAppInfo(QName JavaDoc elementQName, AppInfo appInfo) throws Exception JavaDoc {
150        ElementProperties ep = null;
151        
152        Element appInfoElement = appInfo.getAppInfoElement();
153        if(appInfoElement != null) {
154            NodeList JavaDoc list = appInfoElement.getChildNodes();
155            Node JavaDoc elementProperties = null;
156            if (list != null) {
157                for (int i = 0; i < list.getLength(); i++) {
158                        Node JavaDoc node = list.item(i);
159                        if (node.getNodeType() == Node.ELEMENT_NODE) {
160                            if (node.getNamespaceURI().equals(PropertyModelFactory.PROP_NAMESPACE) && node.getLocalName().equals("ElementProperties")) {
161                                elementProperties = node;
162                                break;
163                            }
164                        }
165                }
166            }
167            if (elementProperties != null) {
168                lastElement = elementQName;
169                
170 /* Validator v = propertySchema.newValidator();
171                v.setErrorHandler(errorHandler);
172                DOMSource s = new DOMSource(elementProperties);
173                v.validate(s);*/

174             
175                ep = new ElementProperties();
176                ep.readNode(elementProperties);
177            }
178            
179        }
180        
181        if(lastError != null) {
182            throw new PropertyModelException("Exception occured while parsing annotation for element "+ elementQName);
183        }
184        
185        return ep;
186        
187     }
188     
189     class MyErrorHandler implements ErrorHandler JavaDoc {
190         
191         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
192             lastError = exception;
193             logger.log(Level.SEVERE, "Exception occured while parsing annotation for element " + lastElement, exception );
194         }
195
196         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
197             lastError = exception;
198             logger.log(Level.SEVERE, "Exception occured while parsing annotation for element " + lastElement, exception );
199         }
200
201         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
202         }
203         
204
205
206     }
207 }
208
Popular Tags