KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > schema > visitor > SchemaDocumentationFinderVisitor


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  * SchemaDocumentationFinderVisitor.java
22  *
23  * Created on April 17, 2006, 8:29 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.schema.visitor;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Locale JavaDoc;
34 import org.netbeans.modules.xml.schema.model.Annotation;
35 import org.netbeans.modules.xml.schema.model.Documentation;
36 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
37 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
38 import org.netbeans.modules.xml.schema.model.GlobalElement;
39 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
40 import org.netbeans.modules.xml.schema.model.LocalAttribute;
41 import org.netbeans.modules.xml.schema.model.LocalComplexType;
42 import org.netbeans.modules.xml.schema.model.LocalElement;
43 import org.netbeans.modules.xml.schema.model.LocalSimpleType;
44 import org.netbeans.modules.xml.wsdl.ui.model.StringAttribute;
45
46 /**
47  *
48  * @author radval
49  */

50 public class SchemaDocumentationFinderVisitor extends AbstractXSDVisitor {
51     
52     private StringBuffer JavaDoc mDocumentationBuf = new StringBuffer JavaDoc(40);
53     
54     private static StringAttribute xmlLangAttribute = new StringAttribute("xml:lang");
55     
56     private String JavaDoc xmlLangValue = "";
57     
58     /** Creates a new instance of SchemaDocumentationFinderVisitor */
59     public SchemaDocumentationFinderVisitor() {
60         xmlLangValue = convertLocaleToXmlLangValue();
61     }
62     
63     public String JavaDoc getDocumentation() {
64         return this.mDocumentationBuf.toString().trim();
65     }
66     
67     public void visit(LocalAttribute la) {
68         Annotation ann = la.getAnnotation();
69         if(ann != null) {
70             visit(ann);
71         } else {
72             if (la.getType() != null ) {
73                 visit(la.getType().get());
74             } else if (la.getInlineType() != null) {
75                 visit(la.getInlineType());
76             }
77         }
78         
79     }
80     
81     public void visit(GlobalAttribute ga) {
82         Annotation ann = ga.getAnnotation();
83         if(ann != null) {
84             visit(ann);
85         } else {
86             if (ga.getType() != null ) {
87                 visit(ga.getType().get());
88             } else if (ga.getInlineType() != null) {
89                 visit(ga.getInlineType());
90             }
91         }
92     }
93     
94     public void visit(GlobalElement ge) {
95         Annotation ann = ge.getAnnotation();
96         if(ann != null) {
97             visit(ann);
98         } else {
99             if (ge.getType() != null ) {
100                 visit(ge.getType().get());
101             } else if (ge.getInlineType() != null) {
102                 visit(ge.getInlineType());
103             }
104         }
105     }
106     
107     public void visit(LocalElement le) {
108         Annotation ann = le.getAnnotation();
109         if(ann != null) {
110             visit(ann);
111         } else {
112             if (le.getType() != null ) {
113                 visit(le.getType().get());
114             } else if (le.getInlineType() != null) {
115                 visit(le.getInlineType());
116             }
117         }
118     }
119     
120     
121     
122     
123     @Override JavaDoc
124     public void visit(GlobalComplexType gct) {
125         Annotation ann = gct.getAnnotation();
126         if(ann != null) {
127             visit(ann);
128         }
129     }
130
131     @Override JavaDoc
132     public void visit(GlobalSimpleType gst) {
133         if(gst != null) {
134         Annotation ann = gst.getAnnotation();
135         if(ann != null) {
136             visit(ann);
137             }
138         }
139     }
140
141     @Override JavaDoc
142     public void visit(LocalComplexType type) {
143         Annotation ann = type.getAnnotation();
144         if(ann != null) {
145             visit(ann);
146         }
147     }
148
149     @Override JavaDoc
150     public void visit(LocalSimpleType type) {
151         Annotation ann = type.getAnnotation();
152         if(ann != null) {
153             visit(ann);
154         }
155     }
156
157     public void visit(Annotation ann) {
158         visitLocalizedDocumentation(ann);
159     }
160     
161     public void visit(Documentation doc) {
162         String JavaDoc content = doc.getContent();
163         if(content != null) {
164             mDocumentationBuf.append(content);
165         }
166     }
167     
168     private void visitLocalizedDocumentation(Annotation ann) {
169         boolean foundLocalizedDocumentation = false;
170         
171         Collection JavaDoc<Documentation> docs = ann.getDocumentationElements();
172         if (docs != null) {
173             Iterator JavaDoc<Documentation> iter = docs.iterator();
174             while(iter.hasNext()) {
175                 Documentation doc = iter.next();
176                 String JavaDoc xmlLang = doc.getAttribute(xmlLangAttribute);
177                 //find the first documentation matching current locale
178
if(xmlLang != null && xmlLang.equals(xmlLangValue)) {
179                     visit(doc);
180                     foundLocalizedDocumentation = true;
181                     break;
182                 }
183             }
184             
185             //no matching documentation found, so looks like xml:lang
186
//is not specified
187
if(!foundLocalizedDocumentation) {
188                 //get the first documentation and use is as default
189
if(docs.size() > 0) {
190                     visitDefaultDocumentation(docs);
191                 }
192             }
193         }
194     }
195     
196     private String JavaDoc convertLocaleToXmlLangValue() {
197          StringBuffer JavaDoc xmlLangValue = new StringBuffer JavaDoc(15);
198         
199         Locale JavaDoc l = Locale.getDefault();
200         String JavaDoc language = l.getLanguage();
201         String JavaDoc country = l.getCountry();
202         String JavaDoc variant = l.getVariant();
203         
204         if(language != null && !language.trim().equals("")) {
205             xmlLangValue.append(language);
206         }
207         
208         if(country != null && !country.trim().equals("")) {
209             xmlLangValue.append("-");
210             xmlLangValue.append(country);
211         }
212         
213         if(variant != null && !variant.trim().equals("")) {
214             xmlLangValue.append("-");
215             xmlLangValue.append(variant);
216         }
217         
218         return xmlLangValue.toString();
219     }
220     
221     private void visitDefaultDocumentation( Collection JavaDoc<Documentation> docs) {
222         //find documentation which does not have xml:lang
223
if (docs != null) {
224             Iterator JavaDoc<Documentation> iter = docs.iterator();
225             while(iter.hasNext()) {
226                 Documentation doc = iter.next();
227                 String JavaDoc xmlLang = doc.getAttribute(xmlLangAttribute);
228                 if(xmlLang == null || xmlLang.trim().equals("")) {
229                     visit(doc);
230                     break;
231                 }
232             }
233         }
234     }
235 }
236
237
Popular Tags