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.spi;
021:
022: import java.util.Collections;
023: import java.util.List;
024: import java.util.Set;
025: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext;
026: import org.netbeans.modules.xml.schema.model.GlobalElement;
027: import org.netbeans.modules.xml.schema.model.GlobalType;
028: import org.netbeans.modules.xml.schema.model.SchemaComponent;
029: import org.netbeans.modules.xml.xpath.ext.LocationStep;
030:
031: /**
032: * The schema context, which contains only one Schema component.
033: * @author nk160297
034: */
035: public class SimpleSchemaContext implements XPathSchemaContext {
036:
037: private XPathSchemaContext mParentContext;
038: private SchemaCompPair mSchemaCompPair;
039:
040: /**
041: * Constructs a simple schema context based on the list of
042: * SchemaComponent objects.
043: * @param parentContext can be null. It not null then is will
044: * be assigned as parent to the first SimpleSchemaContext in the chain.
045: * @param pathList
046: * @return
047: */
048: public static XPathSchemaContext constructSimpleSchemaContext(
049: XPathSchemaContext parentContext,
050: List<SchemaComponent> pathList) {
051: //
052: XPathSchemaContext result = parentContext;
053: for (SchemaComponent sComp : pathList) {
054: result = new SimpleSchemaContext(result, sComp);
055: }
056: //
057: return result;
058: }
059:
060: public SimpleSchemaContext(XPathSchemaContext parentContext,
061: SchemaCompPair schemaCompPair) {
062: mParentContext = parentContext;
063: mSchemaCompPair = schemaCompPair;
064: }
065:
066: /**
067: * Creates context for a global Type or global Element.
068: */
069: public SimpleSchemaContext(SchemaComponent sComp) {
070: this (null, sComp);
071: }
072:
073: public SimpleSchemaContext(XPathSchemaContext parentContext,
074: SchemaComponent sComp) {
075: //
076: if (parentContext == null) {
077: mParentContext = null;
078: mSchemaCompPair = new SchemaCompPair(sComp, null);
079: } else {
080: mParentContext = parentContext;
081: SchemaComponent parentComp = Utilities
082: .getSchemaComp(mParentContext);
083: assert parentComp != null;
084: mSchemaCompPair = new SchemaCompPair(sComp, parentComp);
085: }
086: }
087:
088: public XPathSchemaContext getParentContext() {
089: return mParentContext;
090: }
091:
092: public Set<SchemaCompPair> getSchemaCompPairs() {
093: return Collections.singleton(mSchemaCompPair);
094: }
095:
096: public Set<SchemaCompPair> getUsedSchemaCompPairs() {
097: return getSchemaCompPairs();
098: }
099:
100: public void setUsedSchemaComp(Set<SchemaComponent> compSet) {
101: // Ignore the set because there is only one schema component
102: // in this context and it always is implied as used!
103: }
104:
105: @Override
106: public String toString() {
107: StringBuffer sb = new StringBuffer();
108: //
109: if (mParentContext != null) {
110: sb.append(mParentContext.toString());
111: }
112: sb.append(LocationStep.STEP_SEPARATOR);
113: //
114: if (mSchemaCompPair != null) {
115: SchemaComponent sComp = mSchemaCompPair.getComp();
116: SchemaCompPair.appendCompName(sb, sComp);
117: }
118: //
119: return sb.toString();
120: }
121:
122: @Override
123: public boolean equals(Object obj) {
124: if (obj instanceof SimpleSchemaContext) {
125: //
126: // Optimized comparison for this simple case
127: SimpleSchemaContext other = (SimpleSchemaContext) obj;
128: SchemaComponent sComp1 = this .mSchemaCompPair.getComp();
129: SchemaComponent sComp2 = other.mSchemaCompPair.getComp();
130: //
131: if (sComp1 != sComp2) {
132: return false;
133: }
134: //
135: return true;
136: } else if (obj instanceof XPathSchemaContext) {
137: return XPathSchemaContext.Utilities.equals(this ,
138: (XPathSchemaContext) obj);
139: }
140: //
141: return false;
142: }
143:
144: public boolean equalsChain(XPathSchemaContext other) {
145: if (equals(other)) {
146: //
147: // Compare parent contexts
148: XPathSchemaContext parentCont1 = this .mParentContext;
149: XPathSchemaContext parentCont2 = other.getParentContext();
150: if (parentCont1 != null && parentCont2 != null) {
151: boolean result = parentCont1.equalsChain(parentCont2);
152: if (!result) {
153: return false;
154: }
155: } else if ((parentCont1 == null && parentCont2 != null)
156: || (parentCont1 != null && parentCont2 == null)) {
157: return false;
158: }
159: //
160: return true;
161: }
162: return false;
163: }
164:
165: }
|