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.cnd.modelimpl.options;
043:
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046: import org.netbeans.api.project.Project;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.spi.project.AuxiliaryConfiguration;
049: import org.netbeans.spi.project.support.ant.AntBasedProjectType;
050: import org.openide.util.Mutex;
051: import org.w3c.dom.DOMException;
052: import org.w3c.dom.Document;
053: import org.w3c.dom.Element;
054: import org.w3c.dom.NodeList;
055:
056: /**
057: *
058: * @author Sergey Grinev
059: */
060: public class CodeAssistanceOptions {
061:
062: private AuxiliaryConfiguration aux;
063:
064: private static final String CodeAssistanceData = "code-assistance-data"; //NOI18N
065: private static final String CodeModelEnabled = "code-model-enabled"; //NOI18N
066: private static final String ParseOrphanEnabled = "parse-orphan-enabled"; //NOI18N
067:
068: private final String namespace;
069: private final boolean shared;
070:
071: // constructors
072:
073: public CodeAssistanceOptions(Project project, boolean shared) {
074: this .shared = shared;
075: aux = ((AuxiliaryConfiguration) project.getLookup().lookup(
076: AuxiliaryConfiguration.class));
077:
078: AntBasedProjectType antPrj = ((AntBasedProjectType) project
079: .getLookup().lookup(AntBasedProjectType.class));
080: namespace = antPrj
081: .getPrimaryConfigurationDataElementNamespace(shared);
082: }
083:
084: public CodeAssistanceOptions(Project project) {
085: this (project, false);
086: }
087:
088: // options
089:
090: public Boolean getCodeAssistanceEnabled() {
091: String value = doLoad(CodeModelEnabled);
092: return !value.equals("") && value != null ? new Boolean(value)
093: : Boolean.TRUE;
094: }
095:
096: public void setCodeAssistanceEnabled(Boolean enabled) {
097: doSave(CodeModelEnabled, enabled.toString());
098: }
099:
100: public Boolean getParseOrphanEnabled() {
101: String value = doLoad(ParseOrphanEnabled);
102: return !value.equals("") && value != null ? new Boolean(value)
103: : Boolean.TRUE;
104: }
105:
106: public void setParseOrphanEnabled(Boolean enabled) {
107: doSave(ParseOrphanEnabled, enabled.toString());
108: }
109:
110: // private methods
111:
112: private Element getConfigurationFragment() {
113: Element data = aux.getConfigurationFragment(CodeAssistanceData,
114: namespace, shared);
115: if (data == null) {
116: data = createDocument(namespace,
117: shared ? "project" : "project-private")
118: .createElementNS(namespace, CodeAssistanceData); //NOI18N
119: }
120: if (data == null) {
121: System.err
122: .println("CodeAssistanceOptions: Failed to load and create configuration fragment ("
123: + CodeAssistanceData
124: + " : "
125: + namespace
126: + ")"); //NOI18N
127: }
128: return data;
129: }
130:
131: private Element getNode(Element configurationFragment, String name) {
132: NodeList nodes = configurationFragment.getElementsByTagNameNS(
133: namespace, name);
134: Element node;
135: if (nodes.getLength() == 0) {
136: node = configurationFragment.getOwnerDocument()
137: .createElementNS(namespace, name);
138: configurationFragment.appendChild(node);
139: } else {
140: node = (Element) nodes.item(0);
141: }
142: return node;
143: }
144:
145: private String doLoad(final String name) {
146: return (String) ProjectManager.mutex().readAccess(
147: new Mutex.Action() {
148: public Object run() {
149: Element configurationFragment = getConfigurationFragment();
150: if (configurationFragment == null) {
151: return null;
152: }
153: return getNode(configurationFragment, name)
154: .getTextContent();
155: }
156: });
157: }
158:
159: private void doSave(final String name, final String value) {
160: ProjectManager.mutex().writeAccess(new Runnable() {
161: public void run() {
162: Element configurationFragment = getConfigurationFragment();
163: if (configurationFragment != null) {
164: Element el = getNode(configurationFragment, name);
165: el.setTextContent(value);
166: aux.putConfigurationFragment(configurationFragment,
167: shared);
168: }
169: }
170: });
171: }
172:
173: // utility
174:
175: private static Document createDocument(String ns, String root)
176: throws DOMException {
177: DocumentBuilderFactory factory = DocumentBuilderFactory
178: .newInstance();
179: try {
180: return factory.newDocumentBuilder().getDOMImplementation()
181: .createDocument(ns, root, null);
182: } catch (ParserConfigurationException ex) {
183: throw (DOMException) new DOMException(
184: DOMException.NOT_SUPPORTED_ERR,
185: "Cannot create parser").initCause(ex); // NOI18N
186: }
187: }
188: }
|