1 19 package org.netbeans.modules.xml.schema.model.impl; 20 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 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 34 public class KeyRefImpl extends ConstraintImpl implements KeyRef { 35 36 public KeyRefImpl(SchemaModelImpl model) { 37 this(model,createNewComponent(SchemaElements.KEYREF,model)); 38 } 39 40 43 public KeyRefImpl(SchemaModelImpl model, org.w3c.dom.Element el) { 44 super(model, el); 45 } 46 47 51 public Class <? extends SchemaComponent> getComponentType() { 52 return KeyRef.class; 53 } 54 55 58 public void setReferer(Constraint c) { 59 this.setAttribute(REFERER_PROPERTY, SchemaAttributes.REFER, 60 c==null?null:new ConstraintWrapper(c)); 61 } 62 63 66 public void accept(SchemaVisitor visitor) { 67 visitor.visit(this); 68 } 69 70 71 74 public Constraint getReferer() { 75 String referValue = this.getAttribute(SchemaAttributes.REFER); 76 if(referValue == null) 77 return null; 78 String localName = getLocalName(referValue); 80 SchemaComponent parent = findOutermostParentElement(); 81 FindReferredConstraintVisitor visitor = 82 new FindReferredConstraintVisitor(); 83 84 return visitor.findReferredConstraint(parent, localName); 85 } 86 87 90 private static class ConstraintWrapper{ 91 private Constraint c; 92 93 public ConstraintWrapper(Constraint c){ 94 this.c = c; 95 } 96 97 public String toString(){ 98 return c.getName(); 99 } 100 } 101 102 private String getLocalName(String uri) { 103 String localName = null; 104 try { 105 URI u = new URI (uri); 106 localName = u.getSchemeSpecificPart(); 107 } catch (URISyntaxException ex) { 108 } 109 return localName; 110 } 111 112 118 private SchemaComponent findOutermostParentElement(){ 119 SchemaComponent element = null; 120 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 |