KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SchemaUtility.java
22  *
23  * Created on April 17, 2006, 8:53 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.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.xml.namespace.QName JavaDoc;
34 import org.netbeans.modules.xml.schema.model.Attribute;
35 import org.netbeans.modules.xml.schema.model.Element;
36 import org.netbeans.modules.xml.xam.Nameable;
37 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
38
39 /**
40  *
41  * @author radval
42  */

43 public class SchemaUtility {
44     
45     /** Creates a new instance of SchemaUtility */
46     public SchemaUtility() {
47     }
48     
49     /**
50      * Find Attribute given attribute QName
51      * attribute QName should have namespace and local name
52      * prefix of attribute QName is ignored
53      **/

54     public static Attribute findAttribute(QName JavaDoc attrQName, Element element) {
55         Attribute attribute = null;
56         SchemaElementAttributeFinderVisitor seaFinder = new SchemaElementAttributeFinderVisitor(element);
57         element.accept(seaFinder);
58         
59         List JavaDoc<Attribute> attributes = seaFinder.getAttributes();
60         Iterator JavaDoc<Attribute> it = attributes.iterator();
61         
62         while(it.hasNext()) {
63             Attribute attr = it.next();
64             if(attr instanceof Nameable) {
65                 Nameable namedAttr = (Nameable) attr;
66                 String JavaDoc attrName = namedAttr.getName();
67                 if (attrName.equals(attrQName.getLocalPart())) return attr;
68                 
69                 QName JavaDoc aq = QName.valueOf(attrName);
70                 String JavaDoc ns = aq.getNamespaceURI();
71                 String JavaDoc prefix = aq.getPrefix();
72                 if(ns == null || ns.trim().equals("") && prefix != null) {
73                     ns = ((AbstractDocumentComponent) element).lookupNamespaceURI(prefix);
74                 }
75                 
76                 QName JavaDoc normalizedQName = new QName JavaDoc(ns, aq.getLocalPart());
77                         
78                 if(attrQName.equals(normalizedQName)) {
79                     attribute = attr;
80                     break;
81                 }
82             }
83         }
84         
85         return attribute;
86     }
87 }
88
Popular Tags