Child: [r23] (diff)

Download this file

ExtensionCreate.java    203 lines (176 with data), 7.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-16T00:00:00Z
* LicenseName: NOASSERTION
* FileName: ExtensionCreate.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Automatically create a class with relevant information
* about a given extension. For this to happen, information is collected across
* the Internet.</text>
*/
package FileExtension;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import spdxlib.DocumentCreate;
import main.core;
/**
*
* @author Nuno Brito, 16th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class ExtensionCreate {
public String
sourceURL = "http://www.filesuffix.com/en/extension/",
descriptionText,
descriptionURL,
descriptionCopyright = "FileSuffix.com (c)"
+ utils.time.getCurrentYear();
/**
* An automated way of getting information for new file names.
* It is not an easy task to catalog file names, some web sites
* indicate automatically much automatically >5000 file extensions
* and deviations so we will try to get some info from them and give
* credits where credits are due.
* @param extension the unrecognized extension
* @param file file pointer that we use to gather data
*/
public void automatically(String extension, boolean overWrite) {
File template = new File(core.getExtensionsFolder(), "template.java");
// preflight check
String safeExt =
utils.text.findRegEx( extension,"[a-zA-Z]+$", 0);
// if the extension was just numbers, we need to make this compatible
if(safeExt.isEmpty()){
safeExt = "ext"
+ utils.text.findRegEx( extension,"[a-zA-Z0-9_]+$", 0);
}
File newExtension = new File(core.getExtensionsUnknown(),
safeExt + ".java");
// don't overwrite already existent unknown files
if(overWrite == false)
if(newExtension.exists()){
return;
}
// we can't proceed without a template
if(template.exists() == false){
System.err.println("FI005 - Cannot find template.java to cre"
+ "ate a new file extension");
return;
}
// all done, let's create a new template
String text = utils.files.readAsString(template);
// edit that are necessary
text = "package unknown;\n\n" + text;
text = text.replace("class template", "class " + safeExt);
text = text.replace("#EXT#", extension);
// add the person name
String person = System.getProperty("user.name");
text = text.replace("#PERSON#", person);
text = text.replace("LicenseName: NOASSERTION",
"LicenseName: CC-BY-3.0");
text = text.replace("#DATE#", DocumentCreate.getDateSPDX());
// get the year value for the copyright value
Date date = new Date();
SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy");
String year = simpleDateformat.format(date);
text = text.replace("#COPYRIGHT#", "Copyright �� "
+ year
+ ", "
+ person);
// // add the description text from the Internet
// // retrieves information on the web about this file extension
// getWebInformation(extension);
//
// String tagText = "return null; // file type description";
// String newText = "return " + descriptionText
// + doPrettyJavaText(60,
// descriptionText
// )
// + ";";
// text = text.replace(tagText, newText);
//
// //System.err.println(doPrettyJavaText(60, descriptionText));
//
// // add the description copyright from the Internet
// tagText = "return null; //author of description";
// newText = "return \"" + descriptionCopyright + "\";";
// text = text.replace(tagText, newText);
//
// // add the description page from the Internet
// tagText = "return null; // where the original page can be reached";
// newText = "return \"" + descriptionURL + "\";";
// text = text.replace(tagText, newText);
// now save it automatically new file
utils.files.SaveStringToFile(newExtension, text);
}
/**
* Get information automatically from the web in regards to this file
* extension
* @param extension the extension to find info about
*/
private void getWebInformation(String extension) {
String www = sourceURL + extension;
String webContent = utils.internet.getTextFile(www);
if(webContent.isEmpty()){
// nothing much to do, we just got an empty result
}
// processing the page contents
extractWebDescription(webContent);
descriptionURL = www;
}
/**
* Gets the short info about the file description
* @param webContent
*/
private void extractWebDescription(String webContent) {
String id1 = "<span>Description:</span><br/>";
String id2 = "</div>";
int lineStart = webContent.indexOf(id1) + id1.length();
descriptionText = webContent.substring(lineStart);
int lineEnd = descriptionText.indexOf(id2);
descriptionText = descriptionText.substring(0, lineEnd);
// do the regEx filtering to get what we want
descriptionText = descriptionText.replaceAll("[a-zA-Z0-9-_@\\.]+$", "");
descriptionText = descriptionText.replaceAll("<[^>]*>", "");
descriptionText = descriptionText.replaceAll(" ", "");
descriptionText = descriptionText.replaceAll("[\\n\\t]", "");
System.out.println(webContent);
}
/**
* Breaks long lines of text after a given number of chars
*/
private String doPrettyJavaText(int desiredLength, String text){
String temp = text;
String result = "";
int jump = desiredLength;
while(temp.length() > jump){
// // reset the counter
// jump = desiredLength;
// // get the upcoming line to be found
// String findWord = temp.substring(0, desiredLength);
// // if not empty, get the last space
// if(findWord.length() > 0){
// int lastSpace = findWord.lastIndexOf(" ");
// // shall we shorten our jump?
// if(lastSpace > 0){
// jump = lastSpace;
// }
// }
//
//
result += "\n\t\t+ \"" + temp.substring(0, jump) + "\"";
temp = temp.substring(jump);
}
// add the ending part
if(temp.length() > 0){
result += "\n\t\t+ \"" + temp + "\"";
}
// clean up the beginning
result = "\t " + result.substring(4);
return result;
}
}