2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.ant.freeform;
44
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeListener;
46
import java.nio.charset.Charset;
47
import java.nio.charset.IllegalCharsetNameException;
48
import java.util.Collections;
49
import java.util.HashMap;
52
import org.netbeans.spi.project.support.ant.AntProjectEvent;
53
import org.netbeans.spi.project.support.ant.AntProjectHelper;
54
import org.netbeans.spi.project.support.ant.AntProjectListener;
55
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
56
import org.openide.filesystems.FileObject;
57
import org.w3c.dom.Element;
58
import org.netbeans.modules.ant.freeform.spi.support.Util;
59
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
60
import org.openide.util.Exceptions;
63
* Implementation of FileEncodingQuery for Freeform project, its instance can be
64
* obtained from project lookup
68
public class FreeformFileEncodingQueryImpl extends FileEncodingQueryImplementation
69
implements AntProjectListener, PropertyChangeListener {
71
private AntProjectHelper helper;
72
private PropertyEvaluator evaluator;
73
private Map<FileObject,Charset> encodingsCache;
75
public FreeformFileEncodingQueryImpl(AntProjectHelper aph, PropertyEvaluator eval) {
78
evaluator.addPropertyChangeListener(this);
81
public Charset getEncoding(FileObject file) {
85
if (encodingsCache == null) {
86
computeEncodingsCache();
89
if (encodingsCache.size() == 0) {
93
Set<FileObject> roots = encodingsCache.keySet();
94
FileObject parent = getNearestParent(roots, file);
96
return encodingsCache.get(parent);
104
private FileObject getNearestParent(Set<FileObject> parents, FileObject file) {
105
while (file != null) {
106
if (parents.contains(file)) {
109
file = file.getParent();
114
private void computeEncodingsCache() {
115
Map<FileObject,Charset> cache = new HashMap<FileObject,Charset>(3);
116
Element data = Util.getPrimaryConfigurationData(helper);
117
Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); // NOI18N
118
if (foldersEl != null) {
119
for (Element sourceFolderEl : Util.findSubElements(foldersEl)) {
120
if (!sourceFolderEl.getLocalName().equals("source-folder")) { // NOI18N
123
FileObject srcRoot = null;
124
Element locationEl = Util.findElement(sourceFolderEl, "location", Util.NAMESPACE); // NOI18N
125
if (locationEl != null) {
126
String location = evaluator.evaluate(Util.findText(locationEl));
127
if (location != null) {
128
srcRoot = helper.resolveFileObject(location);
131
Element encodingEl = Util.findElement(sourceFolderEl, "encoding", Util.NAMESPACE); // NOI18N
132
if (encodingEl != null && srcRoot != null) {
133
String encoding = evaluator.evaluate(Util.findText(encodingEl));
134
Charset charset = null;
135
if (encoding != null) {
137
charset = Charset.forName(encoding);
138
} catch (IllegalCharsetNameException icne) {
139
Exceptions.printStackTrace(icne);
141
cache.put(srcRoot, charset);
146
if (cache.size() > 0) {
147
encodingsCache = cache;
149
encodingsCache = Collections.<FileObject,Charset>emptyMap();
155
public void configurationXmlChanged(AntProjectEvent ev) {
159
public void propertiesChanged(AntProjectEvent ev) {
163
public void propertyChange(PropertyChangeEvent evt) {
167
private synchronized void invalidateCache() {
168
synchronized (this) {
169
encodingsCache = null;