KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > wsdl > util > FileWriter


1 package org.apache.axis2.wsdl.util;
2
3 import org.apache.axis2.wsdl.codegen.XSLTConstants;
4
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 /*
9 * Copyright 2004,2005 The Apache Software Foundation.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * File writer utility for writing out class files
24 */

25 public class FileWriter {
26
27     /**
28      * Creates/ returns a file object
29      * @param rootLocation - Location to be written
30      * @param packageName - package, can be '.' seperated
31      * @param fileName name of the file
32      * @param extension type of the file, java, csharp, cpp etc
33      * @return the File that was created
34      * @throws IOException
35      * @throws Exception
36      */

37     public static File JavaDoc createClassFile(File JavaDoc rootLocation,String JavaDoc packageName,String JavaDoc fileName,String JavaDoc extension) throws IOException JavaDoc,Exception JavaDoc{
38         File JavaDoc returnFile = null;
39         File JavaDoc root = rootLocation;
40
41         if (packageName!=null){
42             String JavaDoc directoryNames[] = packageName.split("\\.");
43             File JavaDoc tempFile = null;
44             int length = directoryNames.length;
45             for (int i = 0; i < length; i++) {
46                 tempFile = new File JavaDoc(root,directoryNames[i]);
47                 root = tempFile;
48                 if (!tempFile.exists()){
49                     tempFile.mkdir();
50                 }
51             }
52         }
53
54         if (!fileName.endsWith(extension)){
55             fileName = fileName + extension;
56         }
57
58         returnFile = new File JavaDoc(root,fileName);
59
60         if (!returnFile.exists()){
61             returnFile.createNewFile();
62         }
63         return returnFile;
64     }
65
66     /**
67      * Creates/ returns a file object
68      * @param rootLocation - Location to be written
69      * @param packageName - package, can be '.' seperated
70      * @param fileName name of the file
71      * @param fileType type of the file, java, csharp, cpp etc. Guesses the extension with the
72      * file type
73      * @return the File that was created
74      * @throws IOException
75      * @throws Exception
76      */

77     public static File JavaDoc createClassFile(File JavaDoc rootLocation,String JavaDoc packageName,String JavaDoc fileName,int fileType) throws IOException JavaDoc,Exception JavaDoc{
78         return createClassFile(rootLocation,packageName,fileName,getExtension(fileType));
79
80     }
81
82     /**
83      * Find the extension for a given file type
84      * @param fileType
85      * @return
86      */

87     private static String JavaDoc getExtension(int fileType) {
88         String JavaDoc extension = "";
89         switch (fileType){
90             case XSLTConstants.LanguageTypes.JAVA: extension=".java";break;
91             case XSLTConstants.LanguageTypes.C_SHARP: extension=".cs";break;
92             case XSLTConstants.LanguageTypes.C_PLUS_PLUS: extension=".cpp";break;
93             case XSLTConstants.LanguageTypes.VB_DOT_NET: extension=".vb";break;
94             default: extension=".xml";
95         }
96         return extension;
97     }
98
99 }
100
Popular Tags