I have a bunch of simple interfaces like this one (pretty enough formed, but not guarantee)
package com.example.sources;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.resources.client.ImageResource.RepeatStyle;
import com.example.sources.MainSceneResource;
import com.example.sources.Preloadable;
public interface MainSceneResources extends ClientBundle, Preloadable {
@Source("com/example/sources/css/mainscene.css")
public MainSceneResource mainSceneCss();
@Source("com/example/sources/img/panorama.jpg")
@ImageOptions(repeatStyle = RepeatStyle.None)
public ImageResource mainSceneBackground();
}
I need to:
- remove line with
package
; - change lines with
import
(delete theimport
keyword, whitespaces, semicolon at the end) and add stripped packages with theFactory
's methodaddImport(line)
; - change
Preloadable
toGenerated
- copy other lines without changes with the
SourceWriter
's methodsw.println(line)
Currently my code is like:
BufferedReader reader = new BufferedReader(new InputStreamReader(ris));
String line = null;
try {
while ((line = reader.readLine()) != null) {
if (line.contains("package") || line.isEmpty()) {
continue;
}
int index = line.indexOf("import");
if (index != -1) {
line = line.substring(index + 6, line.lastIndexOf(";")).trim();
factory.addImport(line);
continue;
}
if (line.contains("Preloadable")) {
line = line.replace("Preloadable", "Generated");
}
sw.println(line);
}
} catch (IOException e) {
throw new UnableToCompleteException();
}
The one more notice is that I can't use SourceWriter
method sw.println(line)
before Factrory
factory.addImport(line)
;
My most worries are about calling line.contains("pattern")
in while
-loop. As I know "pattern"
will be compiled to Java regex Pattern.class
. I'm aware it's not good to compile the same Pattern
over and over till the loop ends, but don't know how to do this better.
Also I doubt about this line (to strip import
lines)
line = line.substring(index + 6, line.lastIndexOf(";")).trim();
Is there a better way to do this?