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: package org.netbeans.modules.xslt.model.impl;
020:
021: import java.util.Collection;
022: import java.util.LinkedHashSet;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.StringTokenizer;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.netbeans.modules.xml.xam.Model;
030: import org.netbeans.modules.xml.xam.Reference;
031: import org.netbeans.modules.xslt.model.AttributeSet;
032: import org.netbeans.modules.xslt.model.ReferenceableXslComponent;
033: import org.netbeans.modules.xslt.model.Stylesheet;
034: import org.netbeans.modules.xslt.model.XslModel;
035: import org.netbeans.modules.xslt.model.XslReference;
036:
037: /**
038: * This is custom resolver for AttributeSet reference list.
039: *
040: * @author ads
041: *
042: */
043: class AttributeSetReferenceListResolver implements
044: ReferenceListResolveFactory {
045:
046: /* (non-Javadoc)
047: * @see org.netbeans.modules.xslt.model.impl.ReferenceListResolveFactory#isApplicable(java.lang.Class)
048: */
049: public boolean isApplicable(Class referenceType) {
050: return AttributeSet.class.isAssignableFrom(referenceType);
051: }
052:
053: /* (non-Javadoc)
054: * @see org.netbeans.modules.xslt.model.impl.ReferenceListResolveFactory#resolve(org.netbeans.modules.xslt.model.impl.XslComponentImpl, java.lang.Class, java.lang.String)
055: */
056: public <T extends ReferenceableXslComponent> List<XslReference<T>> resolve(
057: AttributeAccess access, Class<T> clazz, String value) {
058: StringTokenizer tokenizer = new StringTokenizer(value, " ");
059: List<Reference<T>> references = new LinkedList<Reference<T>>();
060: while (tokenizer.hasMoreTokens()) {
061: String next = tokenizer.nextToken();
062: Collection<Reference<T>> collection = find(clazz, next,
063: access);
064: references.addAll(collection);
065: }
066: return null;
067: }
068:
069: @SuppressWarnings("unchecked")
070: private <T extends ReferenceableXslComponent> Collection<Reference<T>> find(
071: Class<T> clazz, String next, AttributeAccess access) {
072: assert AttributeSet.class.isAssignableFrom(clazz);
073: LinkedHashSet<XslModel> list = Utilities
074: .getAvailibleModels(access.getComponent().getModel());
075: Collection<Reference<T>> collection = new LinkedList<Reference<T>>();
076: QName qName = getQName(next, access);
077: for (XslModel model : list) {
078: if (Model.State.VALID.equals(model.getState())) {
079: Stylesheet stylesheet = model.getStylesheet();
080: if (stylesheet == null) {
081: continue;
082: }
083: List<AttributeSet> children = stylesheet
084: .getChildren(AttributeSet.class);
085: Collection<AttributeSet> result = find(children, qName);
086: for (AttributeSet set : result) {
087: XslReference<AttributeSet> ref = new GlobalReferenceImpl<AttributeSet>(
088: set, AttributeSet.class, access
089: .getComponent());
090: if (result != null) {
091: collection.add((Reference<T>) ref);
092: }
093: }
094: }
095: }
096: return null;
097: }
098:
099: private QName getQName(String value, AttributeAccess access) {
100: assert value != null;
101: String[] parts = value.split(":"); //NOI18N
102: String prefix = null;
103: String localPart;
104: if (parts.length == 2) {
105: prefix = parts[0];
106: localPart = parts[1];
107: } else {
108: localPart = parts[0];
109: }
110: String ns = access.getComponent().lookupNamespaceURI(prefix);
111: return new QName(ns, localPart, prefix);
112: }
113:
114: private Collection<AttributeSet> find(List<AttributeSet> children,
115: QName qName) {
116: Collection<AttributeSet> collection = new LinkedList<AttributeSet>();
117: assert qName != null;
118: for (AttributeSet set : children) {
119: QName name = set.getName();
120: if (name == null) {
121: continue;
122: }
123: String localPart = name.getLocalPart();
124: String ns = name.getNamespaceURI();
125: if (qName.getLocalPart().equals(localPart)
126: && Utilities.equals(qName.getNamespaceURI(), ns)) {
127: collection.add(set);
128: }
129: }
130: return collection;
131: }
132:
133: }
|