1 19 20 package org.netbeans.modules.xml.schema.model.impl; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Set ; 25 import javax.xml.XMLConstants ; 26 import org.netbeans.modules.xml.schema.model.Annotation; 27 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 28 import org.netbeans.modules.xml.schema.model.SchemaComponent; 29 import org.netbeans.modules.xml.schema.model.impl.xdm.SyncUpdateVisitor; 30 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor; 31 import org.netbeans.modules.xml.xam.Component; 32 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 33 import org.netbeans.modules.xml.xam.dom.Attribute; 34 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 35 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 36 import org.netbeans.modules.xml.xam.dom.DocumentModelAccess; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.NodeList ; 40 41 46 public abstract class SchemaComponentImpl 47 extends AbstractDocumentComponent<SchemaComponent> 48 implements SchemaComponent, DocumentModelAccess.NodeUpdater { 49 50 public SchemaComponentImpl(SchemaModelImpl model, Element e) { 51 super(model, e); 52 } 53 54 public SchemaModelImpl getModel() { 55 return (SchemaModelImpl) super.getModel(); 56 } 57 public abstract Class <? extends SchemaComponent> getComponentType(); 58 59 protected String getNamespaceURI() { 60 return XMLConstants.W3C_XML_SCHEMA_NS_URI; 61 } 62 63 66 public abstract void accept(SchemaVisitor v); 67 68 protected static Element createNewComponent(SchemaElements type, SchemaModelImpl model) { 69 String qualified = "xsd:" + type.getName(); return model.getDocument().createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, qualified); 71 } 72 73 protected void populateChildren(List <SchemaComponent> children) { 74 NodeList nl = getPeer().getChildNodes(); 75 if (nl != null){ 76 for (int i = 0; i < nl.getLength(); i++) { 77 Node n = nl.item(i); 78 if (n instanceof Element ) { 79 SchemaComponent comp = (SchemaComponent)getModel().getFactory().create((Element )n, this); 80 if (comp != null) { 81 children.add(comp); 82 } 83 } 84 } 85 } 86 } 87 88 89 92 public final boolean fromSameModel(SchemaComponent other) { 93 return getModel().equals(other.getModel()); 94 } 95 96 99 public void setAnnotation(Annotation annotation) { 100 List <Class <? extends SchemaComponent>> types = new ArrayList <Class <? extends SchemaComponent>>(1); 101 types.add(SchemaComponent.class); 102 setChildBefore(Annotation.class, ANNOTATION_PROPERTY, annotation, types); 103 } 104 105 public Annotation getAnnotation() { 106 List <Annotation> annotations = getChildren(Annotation.class); 107 return annotations.isEmpty() ? null : annotations.iterator().next(); 108 } 109 110 119 protected Class getAttributeType(Attribute attribute) { 120 return attribute.getType(); 121 } 122 123 126 protected Class getAttributeMemberType(Attribute attribute) { 127 return attribute.getMemberType(); 128 } 129 130 protected Object getAttributeValueOf(Attribute attr, String s) { 131 if (s == null) { 132 return null; 133 } 134 Class c = getAttributeType(attr); 135 if (String .class.isAssignableFrom(c)) { 136 return s; 137 } else if (Boolean .class.isAssignableFrom(c)) { 138 return Boolean.valueOf(s); 139 } else if (Integer .class.isAssignableFrom(c)) { 140 return Integer.valueOf(s); 141 } else if (Enum .class.isAssignableFrom(c)) { 142 Class <Enum > enumClass = (Class <Enum >) c; 143 return Util.parse(enumClass, s); 144 } else if (Set .class.isAssignableFrom(c)) { 145 return Util.valuesOf(getAttributeMemberType(attr), s); 146 } 147 148 assert(false); return null; 150 } 151 152 protected <T extends ReferenceableSchemaComponent> GlobalReferenceImpl<T> resolveGlobalReference(Class <T>c, SchemaAttributes attrName){ 153 String v = getAttribute(attrName); 154 return v == null ? null : new GlobalReferenceImpl<T>(c, this, v); 155 } 156 157 protected Element checkNodeRef() { 158 Element e = (Element )getPeer(); 159 if (e == null) { 160 throw new IllegalArgumentException ("Valid Node reference must exist"); } 162 return e; 163 } 164 165 public <T extends ReferenceableSchemaComponent> NamedComponentReference<T> 166 createReferenceTo(T referenced, Class <T> type) { 167 return getModel().getFactory().createGlobalReference(referenced, type, this); 168 } 169 170 public void setId(String id) { 171 setAttribute(ID_PROPERTY, SchemaAttributes.ID, id); 172 } 173 174 public String getId() { 175 return getAttribute(SchemaAttributes.ID); 176 } 177 178 protected String getAttributeValue(SchemaAttributes attr) { 179 return getAttribute(attr); 180 } 181 182 public boolean canPaste(Component child) { 183 if (! (child instanceof DocumentComponent)) return false; 184 return new SyncUpdateVisitor().canAdd(this, (DocumentComponent) child); 185 } 186 187 public String lookupNamespaceURI(String prefix) { 188 return lookupNamespaceURI(prefix, true); 189 } 190 } 191 192 | Popular Tags |