1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui.schema.visitor; 30 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.Locale ; 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 50 public class SchemaDocumentationFinderVisitor extends AbstractXSDVisitor { 51 52 private StringBuffer mDocumentationBuf = new StringBuffer (40); 53 54 private static StringAttribute xmlLangAttribute = new StringAttribute("xml:lang"); 55 56 private String xmlLangValue = ""; 57 58 59 public SchemaDocumentationFinderVisitor() { 60 xmlLangValue = convertLocaleToXmlLangValue(); 61 } 62 63 public String 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  124 public void visit(GlobalComplexType gct) { 125 Annotation ann = gct.getAnnotation(); 126 if(ann != null) { 127 visit(ann); 128 } 129 } 130 131 @Override  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  142 public void visit(LocalComplexType type) { 143 Annotation ann = type.getAnnotation(); 144 if(ann != null) { 145 visit(ann); 146 } 147 } 148 149 @Override  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 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 <Documentation> docs = ann.getDocumentationElements(); 172 if (docs != null) { 173 Iterator <Documentation> iter = docs.iterator(); 174 while(iter.hasNext()) { 175 Documentation doc = iter.next(); 176 String xmlLang = doc.getAttribute(xmlLangAttribute); 177 if(xmlLang != null && xmlLang.equals(xmlLangValue)) { 179 visit(doc); 180 foundLocalizedDocumentation = true; 181 break; 182 } 183 } 184 185 if(!foundLocalizedDocumentation) { 188 if(docs.size() > 0) { 190 visitDefaultDocumentation(docs); 191 } 192 } 193 } 194 } 195 196 private String convertLocaleToXmlLangValue() { 197 StringBuffer xmlLangValue = new StringBuffer (15); 198 199 Locale l = Locale.getDefault(); 200 String language = l.getLanguage(); 201 String country = l.getCountry(); 202 String 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 <Documentation> docs) { 222 if (docs != null) { 224 Iterator <Documentation> iter = docs.iterator(); 225 while(iter.hasNext()) { 226 Documentation doc = iter.next(); 227 String 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 |