Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #188: AddLicense.java

File AddLicense.java, 2.9 kB (added by sunbiz, 2 years ago)

The java program to add license comments

Line 
1 import javax.swing.*;
2 import java.io.*;
3 import javax.swing.filechooser.FileNameExtensionFilter;
4
5 public class AddLicense extends JFrame {
6
7     public AddLicense() {
8         super("Add License to Code");
9         FileNameExtensionFilter filter = new FileNameExtensionFilter("Java Source", "java");
10         JFileChooser chooser = new JFileChooser();
11         chooser.setCurrentDirectory(new File("C:\\openMRS\\src\\web\\org\\openmrs"));
12         chooser.setFileFilter(filter);
13         chooser.setMultiSelectionEnabled(true);
14         int result = chooser.showOpenDialog(null);
15         if (result == JFileChooser.APPROVE_OPTION) {
16             String license = "/**\n * The contents of this file are subject to the OpenMRS Public License\n * Version 1.0 (the \"License\"); you may not use this file except in\n * compliance with the License. You may obtain a copy of the License at\n * http://license.openmrs.org\n *\n * Software distributed under the License is distributed on an \"AS IS\"\n * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\n * License for the specific language governing rights and limitations\n * under the License.\n *\n * Copyright (C) OpenMRS, LLC.  All Rights Reserved.\n */";
17             File[] file = chooser.getSelectedFiles();
18             try {
19                 for (int i = 0; i < file.length; i++) {
20                     if (file[i].isDirectory()) {
21                         //file[i].listFiles(filter);
22                     } else {
23                         String str = new String();
24                         FileReader reader = new FileReader(file[i]);
25                         int c = reader.read();
26                         while (c != -1) {
27                             str = str + (char) c;
28                             c = reader.read();
29                         }
30                         reader.close();
31                         if (!str.contains("The contents of this file are subject to the OpenMRS")) {
32                             FileWriter writer = new FileWriter(file[i]);
33                             BufferedWriter bw = new BufferedWriter(writer);
34                             bw.write(license);
35                             bw.newLine();
36                             bw.write(str);
37                             bw.close();
38                             writer.close();
39                             System.out.println("Updated File " + file[i].getAbsolutePath());
40                         }
41                     }
42                 }
43             } catch (Exception e) {
44                 e.printStackTrace();
45             }
46         }
47         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48         setSize(640, 480);
49     }
50
51     public static void main(String args[]) {
52         SwingUtilities.invokeLater(new Runnable() {
53             public void run() {
54                 AddLicense window = new AddLicense();
55                 window.setVisible(true);
56             }
57         });
58     }
59 }