~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ant/freeform/src/org/netbeans/modules/ant/freeform/FreeformFileEncodingQueryImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.ant.freeform;
 
43
 
 
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;
 
50
import java.util.Map;
 
51
import java.util.Set;
 
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;
 
61
 
 
62
/**
 
63
 * Implementation of FileEncodingQuery for Freeform project, its instance can be 
 
64
 * obtained from project lookup
 
65
 * 
 
66
 * @author Milan Kubec
 
67
 */
 
68
public class FreeformFileEncodingQueryImpl extends FileEncodingQueryImplementation 
 
69
        implements AntProjectListener, PropertyChangeListener {
 
70
    
 
71
    private AntProjectHelper helper;
 
72
    private PropertyEvaluator evaluator;
 
73
    private Map<FileObject,Charset> encodingsCache;
 
74
    
 
75
    public FreeformFileEncodingQueryImpl(AntProjectHelper aph, PropertyEvaluator eval) {
 
76
        helper = aph;
 
77
        evaluator = eval;
 
78
        evaluator.addPropertyChangeListener(this);
 
79
    }
 
80
    
 
81
    public Charset getEncoding(FileObject file) {
 
82
        
 
83
        synchronized (this) {
 
84
            
 
85
            if (encodingsCache == null) {
 
86
                computeEncodingsCache();
 
87
            }
 
88
            
 
89
            if (encodingsCache.size() == 0) {
 
90
                return null;
 
91
            }
 
92
            
 
93
            Set<FileObject> roots = encodingsCache.keySet();
 
94
            FileObject parent = getNearestParent(roots, file);
 
95
            if (parent != null) {
 
96
                return encodingsCache.get(parent);
 
97
            }
 
98
        }
 
99
        
 
100
        return null;
 
101
        
 
102
    }
 
103
    
 
104
    private FileObject getNearestParent(Set<FileObject> parents, FileObject file) {
 
105
        while (file != null) {
 
106
             if (parents.contains(file)) {
 
107
                 return file;
 
108
             }
 
109
             file = file.getParent();
 
110
         }
 
111
        return null;
 
112
    }
 
113
    
 
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
 
121
                    continue;
 
122
                }
 
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);
 
129
                    }
 
130
                }
 
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) {
 
136
                        try {
 
137
                            charset = Charset.forName(encoding);
 
138
                        } catch (IllegalCharsetNameException icne) {
 
139
                            Exceptions.printStackTrace(icne);
 
140
                        }
 
141
                        cache.put(srcRoot, charset);
 
142
                    }
 
143
                }
 
144
            }
 
145
        }
 
146
        if (cache.size() > 0) {
 
147
            encodingsCache = cache;
 
148
        } else {
 
149
            encodingsCache = Collections.<FileObject,Charset>emptyMap();
 
150
        }
 
151
    }
 
152
    
 
153
    // ---
 
154
    
 
155
    public void configurationXmlChanged(AntProjectEvent ev) {
 
156
        invalidateCache();
 
157
    }
 
158
    
 
159
    public void propertiesChanged(AntProjectEvent ev) {
 
160
        invalidateCache();
 
161
    }
 
162
    
 
163
    public void propertyChange(PropertyChangeEvent evt) {
 
164
        invalidateCache();
 
165
    }
 
166
    
 
167
    private synchronized void invalidateCache() {
 
168
        synchronized (this) {
 
169
            encodingsCache = null;
 
170
        }
 
171
    }
 
172
    
 
173
}