KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > KeyRefImpl


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.schema.model.impl;
20
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import org.netbeans.modules.xml.schema.model.Constraint;
24 import org.netbeans.modules.xml.schema.model.Element;
25 import org.netbeans.modules.xml.schema.model.KeyRef;
26 import org.netbeans.modules.xml.schema.model.SchemaComponent;
27 import org.netbeans.modules.xml.schema.model.visitor.FindReferredConstraintVisitor;
28 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
29 /**
30  *
31  * @author Vidhya Narayanan
32  * @author rico
33  */

34 public class KeyRefImpl extends ConstraintImpl implements KeyRef {
35     
36     public KeyRefImpl(SchemaModelImpl model) {
37         this(model,createNewComponent(SchemaElements.KEYREF,model));
38     }
39     
40     /**
41      * Creates a new instance of KeyRefImpl
42      */

43     public KeyRefImpl(SchemaModelImpl model, org.w3c.dom.Element JavaDoc el) {
44         super(model, el);
45     }
46     
47     /**
48      *
49      *
50      */

51     public Class JavaDoc<? extends SchemaComponent> getComponentType() {
52         return KeyRef.class;
53     }
54     
55     /**
56      *
57      */

58     public void setReferer(Constraint c) {
59         this.setAttribute(REFERER_PROPERTY, SchemaAttributes.REFER,
60                 c==null?null:new ConstraintWrapper(c));
61     }
62     
63     /**
64      *
65      */

66     public void accept(SchemaVisitor visitor) {
67         visitor.visit(this);
68     }
69     
70     
71     /**
72      *
73      */

74     public Constraint getReferer() {
75         String JavaDoc referValue = this.getAttribute(SchemaAttributes.REFER);
76         if(referValue == null)
77              return null;
78         //remove prefix, if any
79
String JavaDoc localName = getLocalName(referValue);
80         SchemaComponent parent = findOutermostParentElement();
81         FindReferredConstraintVisitor visitor =
82                 new FindReferredConstraintVisitor();
83         
84         return visitor.findReferredConstraint(parent, localName);
85     }
86     
87     /**
88      * Adapter class to enable the use of Constraint in setAttribute()
89      */

90     private static class ConstraintWrapper{
91         private Constraint c;
92         
93         public ConstraintWrapper(Constraint c){
94             this.c = c;
95         }
96         
97         public String JavaDoc toString(){
98             return c.getName();
99         }
100     }
101     
102     private String JavaDoc getLocalName(String JavaDoc uri) {
103         String JavaDoc localName = null;
104         try {
105             URI JavaDoc u = new URI JavaDoc(uri);
106             localName = u.getSchemeSpecificPart();
107         } catch (URISyntaxException JavaDoc ex) {
108         }
109         return localName;
110     }
111     
112     /**
113      * Look for the outermost <element> that encloses this keyRef. This is
114      * required to determine the effective scope where valid keys and uniques
115      * may be obtained. That is, the refer attribute may only refer to keys or
116      * uniques that are contained within the same element scope.
117      */

118     private SchemaComponent findOutermostParentElement(){
119         SchemaComponent element = null;
120         //go up the tree and look for the last instance of <element>
121
SchemaComponent sc = getParent();
122         while(sc != null){
123             if(sc instanceof Element){
124                 element = sc;
125             }
126         sc = sc.getParent();
127         }
128         return element;
129     }
130 }
131
Popular Tags