001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xml.xpath.ext.impl;
021:
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Set;
026: import org.netbeans.modules.xml.xpath.ext.XPathModel;
027: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext;
028: import org.netbeans.modules.xml.xpath.ext.spi.ExternalModelResolver;
029: import org.netbeans.modules.xml.xpath.ext.schema.FindAllChildrenSchemaVisitor;
030: import org.netbeans.modules.xml.schema.model.Schema;
031: import org.netbeans.modules.xml.schema.model.SchemaComponent;
032: import org.netbeans.modules.xml.schema.model.SchemaModel;
033:
034: /**
035: * The special context for wildcard location steps - '*', '*@', 'node()'.
036: *
037: * @author nk160297
038: */
039: public class WildcardSchemaContext implements XPathSchemaContext {
040:
041: private XPathModel mXPathModel;
042: private XPathSchemaContext mParentContext;
043: private boolean lookForElements;
044: private boolean lookForAttributes;
045:
046: // TODO replace to weak reference
047: private Set<SchemaCompPair> mSchemaCompPair = null;
048: private Set<SchemaComponent> mUsedSchemaCompSet;
049:
050: public WildcardSchemaContext(XPathSchemaContext parentContext,
051: XPathModel xPathModel, boolean lookForElements,
052: boolean lookForAttributes) {
053: mXPathModel = xPathModel;
054: mParentContext = parentContext; // it can be null in case of wildcard at root level
055: //
056: this .lookForElements = lookForElements;
057: this .lookForAttributes = lookForAttributes;
058: }
059:
060: public XPathSchemaContext getParentContext() {
061: return mParentContext;
062: }
063:
064: public synchronized Set<SchemaCompPair> getSchemaCompPairs() {
065: if (mSchemaCompPair == null) {
066: mSchemaCompPair = calculateSchemaComponents();
067: }
068: return mSchemaCompPair;
069: }
070:
071: private Set<SchemaCompPair> calculateSchemaComponents() {
072: HashSet<SchemaCompPair> result = new HashSet<SchemaCompPair>();
073: //
074: if (mParentContext == null) {
075: //
076: ExternalModelResolver exModelResolver = mXPathModel
077: .getExternalModelResolver();
078: //
079: if (exModelResolver != null) {
080: //
081: // Look for all available root elements (attributes)
082: // in all available models
083: Collection<SchemaModel> sModels = exModelResolver
084: .getVisibleModels();
085: for (SchemaModel sModel : sModels) {
086: Schema schema = sModel.getSchema();
087: FindAllChildrenSchemaVisitor visitor = new FindAllChildrenSchemaVisitor(
088: lookForElements, lookForAttributes);
089: visitor.lookForSubcomponents(schema);
090: //
091: List<SchemaComponent> foundComps = visitor
092: .getFound();
093: for (SchemaComponent foundComp : foundComps) {
094: SchemaCompPair newPair = new SchemaCompPair(
095: foundComp, null);
096: result.add(newPair);
097: }
098: }
099: }
100: } else {
101: Set<SchemaCompPair> parentCompPairSet = mParentContext
102: .getSchemaCompPairs();
103: for (SchemaCompPair parentCompPair : parentCompPairSet) {
104: SchemaComponent parentComp = parentCompPair.getComp();
105: FindAllChildrenSchemaVisitor visitor = new FindAllChildrenSchemaVisitor(
106: lookForElements, lookForAttributes);
107: visitor.lookForSubcomponents(parentComp);
108: //
109: List<SchemaComponent> foundComps = visitor.getFound();
110: for (SchemaComponent foundComp : foundComps) {
111: SchemaCompPair newPair = new SchemaCompPair(
112: foundComp, parentComp);
113: result.add(newPair);
114: }
115: }
116: }
117: //
118: return result;
119: }
120:
121: public Set<SchemaCompPair> getUsedSchemaCompPairs() {
122: HashSet<SchemaCompPair> resultSet = new HashSet<SchemaCompPair>();
123: //
124: if (mUsedSchemaCompSet != null) {
125: for (SchemaCompPair myCompPair : getSchemaCompPairs()) {
126: SchemaComponent myComponent = myCompPair.getComp();
127: for (SchemaComponent usdComp : mUsedSchemaCompSet) {
128: if (myComponent.equals(usdComp)) {
129: resultSet.add(myCompPair);
130: }
131: }
132: }
133: }
134: //
135: return resultSet;
136: }
137:
138: public void setUsedSchemaComp(Set<SchemaComponent> compSet) {
139: mUsedSchemaCompSet = compSet;
140: }
141:
142: @Override
143: public boolean equals(Object obj) {
144: if (obj instanceof XPathSchemaContext) {
145: return XPathSchemaContext.Utilities.equals(this ,
146: (XPathSchemaContext) obj);
147: }
148: //
149: return false;
150: }
151:
152: public boolean equalsChain(XPathSchemaContext obj) {
153: if (obj instanceof XPathSchemaContext) {
154: return XPathSchemaContext.Utilities.equalsChain(this ,
155: (XPathSchemaContext) obj);
156: }
157: //
158: return false;
159: }
160:
161: }
|