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: package org.netbeans.modules.xslt.project.wizard.element;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.OutputStreamWriter;
046:
047: import java.util.ArrayList;
048: import java.util.Enumeration;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.StringTokenizer;
052:
053: import org.openide.cookies.SaveCookie;
054: import org.openide.filesystems.FileObject;
055: import org.openide.loaders.DataObject;
056:
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectUtils;
059: import org.netbeans.api.project.SourceGroup;
060: import org.netbeans.api.project.Sources;
061:
062: import org.netbeans.modules.xml.schema.model.SchemaModel;
063: import org.netbeans.modules.xml.xam.ModelSource;
064:
065: import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
066: import org.netbeans.modules.xml.catalogsupport.ProjectConstants;
067:
068: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
069: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
070: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
071: import org.openide.ErrorManager;
072: import org.openide.filesystems.FileUtil;
073: import org.openide.filesystems.Repository;
074:
075: import org.netbeans.modules.xml.catalogsupport.util.ProjectVisitor;
076: import org.netbeans.modules.xml.catalogsupport.util.ProjectUtilities;
077:
078: /**
079: * @author Vladimir Yaroslavskiy
080: * @version 2007.01.16
081: */
082: final class PanelUtil {
083:
084: private PanelUtil() {
085: }
086:
087: public static FileObject copyFile(FileObject destination,
088: String path, String file, String name, String ext) {
089: if (name == null || destination == null) {
090: return null;
091: }
092: try {
093: return FileUtil.copyFile(Repository.getDefault()
094: .getDefaultFileSystem().findResource(path + file),
095: destination, name, ext);
096: } catch (IOException ex) {
097: ErrorManager.getDefault().notify(ex);
098: }
099: return null;
100: }
101:
102: public static boolean isValidFileName(String fileName) {
103: boolean isAllowSlash = false;
104: boolean isAllowBackslash = false;
105:
106: if (File.separatorChar == '\\') {
107: isAllowBackslash = true;
108: fileName = fileName.replace('/', File.separatorChar);
109: } else {
110: isAllowSlash = true;
111: }
112: StringTokenizer dirTokens = new StringTokenizer(fileName,
113: File.separator);
114: int numDirs = dirTokens.countTokens();
115: String[] dirs = new String[numDirs];
116: int i = 0;
117:
118: while (dirTokens.hasMoreTokens()) {
119: dirs[i] = dirTokens.nextToken();
120: i++;
121: }
122: return !(fileName == null || fileName.length() == 0
123: || fileName.indexOf("\\") == 0
124: || fileName.indexOf("/") == 0
125: || (!isAllowBackslash && fileName.indexOf("\\") >= 0)
126: || (!isAllowSlash && fileName.indexOf("/") >= 0)
127: || fileName.indexOf(':') >= 0 || !isValidName(dirs));
128: }
129:
130: public static boolean isValidName(String[] dirs) {
131: if (dirs == null || dirs.length == 0) {
132: return false;
133: }
134: boolean isValid = true;
135: for (String dir : dirs) {
136: isValid = isValidName(dir);
137: if (!isValid) {
138: break;
139: }
140: }
141: return isValid;
142: }
143:
144: public static boolean isValidName(String fileName) {
145: try {
146: boolean bValid = true;
147: File tempFile = new File(fileName);
148: String tempFileName = "00" + fileName;
149: File actualTempFile = File.createTempFile(tempFileName,
150: null);
151:
152: if (!FileUtil.normalizeFile(tempFile).equals(
153: tempFile.getCanonicalFile())) {
154: bValid = false;
155: }
156: actualTempFile.delete();
157: actualTempFile = null;
158: tempFile = null;
159: return bValid;
160: } catch (Exception e) {
161: return false;
162: }
163: }
164: }
|