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.tmap.model.impl;
020:
021: import java.net.URI;
022: import java.net.URISyntaxException;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Map.Entry;
028: import org.netbeans.modules.xml.xam.dom.Utils;
029: import org.netbeans.modules.xslt.tmap.model.api.ExNamespaceContext;
030:
031: /**
032: *
033: * @author ads
034: * @author Vitaly Bychkov
035: * @version 1.0
036: */
037: public class ExNamespaceContextImpl implements ExNamespaceContext {
038: static final String DEFAULT_NS = "ns"; // NOI18N
039: private TMapComponentAbstract myComponent;
040:
041: public ExNamespaceContextImpl(TMapComponentAbstract component) {
042: myComponent = component;
043: }
044:
045: /*
046: * (non-Javadoc)
047: *
048: * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
049: */
050: public String getNamespaceURI(String prefix) {
051:
052: // TODO m
053: //// myElement.readLock();
054: //// try {
055: return myComponent.lookupNamespaceURI(prefix);
056: //// }
057: //// finally {
058: //// myElement.readUnlock();
059: //// }
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
066: */
067: public String getPrefix(String uri) {
068: // TODO m
069: //// myElement.readLock();
070: //// try {
071: String str = myComponent.getPeer().lookupPrefix(uri);
072: if (str != null && str.length() == 0) {
073: // it seems this is some buf in XDM when "" prefix is returned .
074: return null;
075: }
076: return str;
077: //// }
078: //// finally {
079: //// myElement.readUnlock();
080: //// }
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
087: */
088: public Iterator getPrefixes(String uri) {
089: // TODO m
090: //// myElement.readLock();
091: //// try {
092: assert uri != null;
093:
094: List<String> list = new LinkedList<String>();
095:
096: TMapComponentAbstract component = myComponent;
097: while (component != null) {
098: fillPrefixes(component, uri, list);
099: component = (TMapComponentAbstract) component.getParent();
100: }
101:
102: return list.iterator();
103: //// }
104: //// finally {
105: //// myElement.readUnlock();
106: //// }
107:
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see org.netbeans.modules.soa.model.bpel.api.support.ExNamespaceContext#getPrefixes()
114: */
115: public Iterator<String> getPrefixes() {
116: // TODO m
117: //// myElement.readLock();
118: //// try {
119: List<String> list = new LinkedList<String>();
120:
121: TMapComponentAbstract component = myComponent;
122: while (component != null) {
123: fillPrefixes(component, null, list);
124: component = (TMapComponentAbstract) component.getParent();
125: }
126:
127: return list.iterator();
128: //// }
129: //// finally {
130: //// myElement.readUnlock();
131: //// }
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.netbeans.modules.soa.model.bpel.api.support.ExNamespaceContext#addNamespace(java.lang.String)
138: */
139: public String addNamespace(String uri)
140: throws InvalidNamespaceException {
141: try {
142: new URI(uri);
143: } catch (URISyntaxException e) {
144: InvalidNamespaceException exc = new InvalidNamespaceException(
145: e.getMessage());
146: throw exc;
147: }
148: // TODO m
149: //// myComponent.writeLock();
150: //// try {
151: if (getPrefix(uri) != null) {
152: return getPrefix(uri);
153: }
154:
155: int i = findAppropriateIndex(null, uri);
156: i++;
157: String resultPrefix = DEFAULT_NS + i;
158:
159: TMapComponentAbstract component = getRoot();
160: component.addPrefix(resultPrefix, uri);
161: return resultPrefix;
162: //// }
163: //// finally {
164: //// myComponent.writeUnlock();
165: //// }
166: }
167:
168: /*
169: * (non-Javadoc)
170: *
171: * @see org.netbeans.modules.soa.model.bpel.api.support.ExNamespaceContext#addNamespace(java.lang.String,
172: * java.lang.String)
173: */
174: public void addNamespace(String prefix, String uri)
175: throws InvalidNamespaceException {
176: try {
177: new URI(uri);
178: } catch (URISyntaxException e) {
179: InvalidNamespaceException exc = new InvalidNamespaceException(
180: e.getMessage());
181: throw exc;
182: }
183: // TODO m
184: //// myElement.writeLock();
185: //// try {
186: if (!Utils.isValidNCName(prefix)) {
187: throw new InvalidNamespaceException("Prefix : '" + prefix + // NOI18N
188: "' is not acceptable as prefix fot namespace."); // NOI18N
189: }
190:
191: findAppropriateIndex(prefix, uri);
192:
193: getRoot().addPrefix(prefix, uri);
194: //// }
195: //// finally {
196: //// myElement.writeUnlock();
197: //// }
198: }
199:
200: private void fillPrefixes(TMapComponentAbstract component,
201: String uri, List<String> list) {
202: Map<String, String> map = component.getPrefixes();
203: for (Entry<String, String> entry : map.entrySet()) {
204: String key = entry.getKey();
205: String value = entry.getValue();
206: if (uri == null) {
207: list.add(key);
208: }
209: if ((value != null) && (value.equals(uri))) {
210: list.add(key);
211: }
212:
213: }
214: }
215:
216: private int findAppropriateIndex(String pref, String uri)
217: throws InvalidNamespaceException {
218: Iterator<String> iterator = getPrefixes();
219: int i = 0;
220: while (iterator.hasNext()) {
221: String str = (String) iterator.next();
222: if (pref != null) {
223: if (pref.equals(str)) {
224: String foundUri = getNamespaceURI(pref);
225: if (foundUri.equals(uri)) {
226: return 0;
227: } else {
228: throw new InvalidNamespaceException(
229: "Element's scope already have "
230: + "prefix "// NOI18N
231: + pref
232: + " and it declared "// NOI18N
233: + "with different namespace uri"); // NOI18N
234: }
235: }
236: }
237: if (str.startsWith(DEFAULT_NS)) {
238: String end = str.substring(2);
239: try {
240: int ind = Integer.parseInt(end);
241: if (ind > i) {
242: i = ind;
243: }
244: } catch (NumberFormatException e) {
245: // we don't care about it.
246: }
247: }
248:
249: }
250: return i;
251: }
252:
253: private TMapComponentAbstract getRoot() {
254: TMapComponentAbstract component = myComponent;
255: while (component.getParent() != null) {
256: component = (TMapComponentAbstract) component.getParent();
257: }
258: return component;
259: }
260:
261: }
|