001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.freeform;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.nio.charset.Charset;
047: import java.nio.charset.IllegalCharsetNameException;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.Set;
052: import org.netbeans.spi.project.support.ant.AntProjectEvent;
053: import org.netbeans.spi.project.support.ant.AntProjectHelper;
054: import org.netbeans.spi.project.support.ant.AntProjectListener;
055: import org.netbeans.spi.queries.FileEncodingQueryImplementation;
056: import org.openide.filesystems.FileObject;
057: import org.w3c.dom.Element;
058: import org.netbeans.modules.ant.freeform.spi.support.Util;
059: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
060: import org.openide.util.Exceptions;
061:
062: /**
063: * Implementation of FileEncodingQuery for Freeform project, its instance can be
064: * obtained from project lookup
065: *
066: * @author Milan Kubec
067: */
068: public class FreeformFileEncodingQueryImpl extends
069: FileEncodingQueryImplementation implements AntProjectListener,
070: PropertyChangeListener {
071:
072: private AntProjectHelper helper;
073: private PropertyEvaluator evaluator;
074: private Map<FileObject, Charset> encodingsCache;
075:
076: public FreeformFileEncodingQueryImpl(AntProjectHelper aph,
077: PropertyEvaluator eval) {
078: helper = aph;
079: evaluator = eval;
080: evaluator.addPropertyChangeListener(this );
081: }
082:
083: public Charset getEncoding(FileObject file) {
084:
085: synchronized (this ) {
086:
087: if (encodingsCache == null) {
088: computeEncodingsCache();
089: }
090:
091: if (encodingsCache.size() == 0) {
092: return null;
093: }
094:
095: Set<FileObject> roots = encodingsCache.keySet();
096: FileObject parent = getNearestParent(roots, file);
097: if (parent != null) {
098: return encodingsCache.get(parent);
099: }
100: }
101:
102: return null;
103:
104: }
105:
106: private FileObject getNearestParent(Set<FileObject> parents,
107: FileObject file) {
108: while (file != null) {
109: if (parents.contains(file)) {
110: return file;
111: }
112: file = file.getParent();
113: }
114: return null;
115: }
116:
117: private void computeEncodingsCache() {
118: Map<FileObject, Charset> cache = new HashMap<FileObject, Charset>(
119: 3);
120: Element data = Util.getPrimaryConfigurationData(helper);
121: Element foldersEl = Util.findElement(data, "folders",
122: Util.NAMESPACE); // NOI18N
123: if (foldersEl != null) {
124: for (Element sourceFolderEl : Util
125: .findSubElements(foldersEl)) {
126: if (!sourceFolderEl.getLocalName().equals(
127: "source-folder")) { // NOI18N
128: continue;
129: }
130: FileObject srcRoot = null;
131: Element locationEl = Util.findElement(sourceFolderEl,
132: "location", Util.NAMESPACE); // NOI18N
133: if (locationEl != null) {
134: String location = evaluator.evaluate(Util
135: .findText(locationEl));
136: if (location != null) {
137: srcRoot = helper.resolveFileObject(location);
138: }
139: }
140: Element encodingEl = Util.findElement(sourceFolderEl,
141: "encoding", Util.NAMESPACE); // NOI18N
142: if (encodingEl != null && srcRoot != null) {
143: String encoding = evaluator.evaluate(Util
144: .findText(encodingEl));
145: Charset charset = null;
146: if (encoding != null) {
147: try {
148: charset = Charset.forName(encoding);
149: } catch (IllegalCharsetNameException icne) {
150: Exceptions.printStackTrace(icne);
151: }
152: cache.put(srcRoot, charset);
153: }
154: }
155: }
156: }
157: if (cache.size() > 0) {
158: encodingsCache = cache;
159: } else {
160: encodingsCache = Collections
161: .<FileObject, Charset> emptyMap();
162: }
163: }
164:
165: // ---
166:
167: public void configurationXmlChanged(AntProjectEvent ev) {
168: invalidateCache();
169: }
170:
171: public void propertiesChanged(AntProjectEvent ev) {
172: invalidateCache();
173: }
174:
175: public void propertyChange(PropertyChangeEvent evt) {
176: invalidateCache();
177: }
178:
179: private synchronized void invalidateCache() {
180: synchronized (this) {
181: encodingsCache = null;
182: }
183: }
184:
185: }
|