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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.cnd.loaders;
040:
041: import java.io.BufferedReader;
042: import java.io.BufferedWriter;
043: import java.io.IOException;
044: import java.io.InputStreamReader;
045: import java.io.OutputStreamWriter;
046: import org.netbeans.modules.cnd.editor.filecreation.CndExtensionList;
047: import org.netbeans.modules.cnd.editor.filecreation.CndHandlableExtensions;
048: import org.netbeans.modules.cnd.editor.filecreation.ExtensionsSettings;
049: import org.openide.filesystems.FileLock;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.loaders.DataObject;
053: import org.openide.loaders.ExtensionList;
054: import org.openide.loaders.MultiDataObject;
055:
056: /**
057: *
058: * @author Sergey Grinev
059: */
060: public abstract class CndAbstractDataLoaderExt extends
061: CndAbstractDataLoader implements CndHandlableExtensions {
062:
063: protected CndAbstractDataLoaderExt(String representationClassName) {
064: super (representationClassName);
065: }
066:
067: @Override
068: protected void createExtentions(String[] extensions) {
069: setExtensions(new CndExtensionList(extensions));
070: }
071:
072: @Override
073: public ExtensionList getExtensions() {
074: return ExtensionsSettings.getInstance(this ).getExtensionList();
075: }
076:
077: @Override
078: public void setExtensions(ExtensionList ext) {
079: ExtensionsSettings.getInstance(this ).setExtensionList(
080: (CndExtensionList) ext);
081: }
082:
083: protected static CndExtensionList arrayToExtensionList(String[] ar) {
084: return new CndExtensionList(ar);
085: }
086:
087: public String getSettingsName() {
088: return getRepresentationClassName();
089: }
090:
091: @Override
092: protected MultiDataObject.Entry createPrimaryEntry(
093: MultiDataObject obj, FileObject primaryFile) {
094: return new CndFormatExt(obj, primaryFile);
095: }
096:
097: private static class CndFormatExt extends CndFormat {
098:
099: public CndFormatExt(MultiDataObject obj, FileObject primaryFile) {
100: super (obj, primaryFile);
101: }
102:
103: @Override
104: public FileObject createFromTemplate(FileObject f, String name)
105: throws IOException {
106: // we don't want extension to be taken from template filename
107: String ext = FileUtil.getExtension(name);
108: assert ext.length() > 0;
109: name = name.substring(0, name.length() - ext.length() - 1);
110:
111: FileObject fo = f.createData(name, ext);
112:
113: java.text.Format frm = createFormat(f, name, ext);
114:
115: BufferedReader r = new BufferedReader(
116: new InputStreamReader(getFile().getInputStream()));
117: try {
118: FileLock lock = fo.lock();
119: try {
120: BufferedWriter w = new BufferedWriter(
121: new OutputStreamWriter(fo
122: .getOutputStream(lock)));
123:
124: try {
125: String current;
126: while ((current = r.readLine()) != null) {
127: w.write(frm.format(current));
128: // Cf. #7061.
129: w.newLine();
130: }
131: } finally {
132: w.close();
133: }
134: } finally {
135: lock.releaseLock();
136: }
137: } finally {
138: r.close();
139: }
140:
141: // copy attributes
142: FileUtil.copyAttributes(getFile(), fo);
143:
144: // unmark template state
145: setTemplate(fo, false);
146:
147: return fo;
148: }
149:
150: private static boolean setTemplate(FileObject fo,
151: boolean newTempl) throws IOException {
152: boolean oldTempl = false;
153:
154: Object o = fo.getAttribute(DataObject.PROP_TEMPLATE);
155: if ((o instanceof Boolean) && ((Boolean) o).booleanValue()) {
156: oldTempl = true;
157: }
158: if (oldTempl == newTempl) {
159: return false;
160: }
161:
162: fo.setAttribute(DataObject.PROP_TEMPLATE,
163: (newTempl ? Boolean.TRUE : null));
164:
165: return true;
166: }
167: }
168: }
|