Skip to content

Commit

Permalink
Replace use-cases of internal PDE build Utils by standard Java methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 10, 2023
1 parent c8754be commit 8d76b3b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -176,7 +177,7 @@ public boolean generateP2Info() throws CoreException {
if (initialInf.exists()) {
//copy over initial contents
try {
StringBuffer inf = Utils.readFile(initialInf);
String inf = Files.readString(initialInf.toPath(), Charset.defaultCharset());
buffer.append(inf);
buffer.append('\n');
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -825,52 +826,6 @@ static public boolean matchVersions(String version1, String version2) {
return false;
}

/**
*
* @param buf
* @param start
* @param target
* @return int
*/
static public int scan(StringBuffer buf, int start, String target) {
return scan(buf, start, new String[] {target});
}

/**
*
* @param buf
* @param start
* @param targets
* @return int
*/
static public int scan(StringBuffer buf, int start, String[] targets) {
for (int i = start; i < buf.length(); i++) {
for (String target : targets) {
if (i < buf.length() - target.length()) {
String match = buf.substring(i, i + target.length());
if (target.equals(match))
return i;
}
}
}
return -1;
}

/**
* Return a buffer containing the contents of the file at the specified location.
*
* @param target the file
* @return StringBuffer
* @throws IOException
*/
static public StringBuffer readFile(File target) throws IOException {
return new StringBuffer(new String(Files.readAllBytes(target.toPath())));
}

static public StringBuffer readFile(InputStream stream) throws IOException {
return new StringBuffer(new String(stream.readAllBytes()));
}

/**
* Custom build scripts should have their version number matching the
* version number defined by the feature/plugin/fragment descriptor.
Expand All @@ -884,18 +839,18 @@ static public StringBuffer readFile(InputStream stream) throws IOException {
*
*/
public static void updateVersion(File buildFile, String propertyName, String version) throws IOException {
StringBuffer buffer = readFile(buildFile);
int pos = scan(buffer, 0, propertyName);
StringBuilder buffer = new StringBuilder(Files.readString(buildFile.toPath(), Charset.defaultCharset()));
int pos = buffer.indexOf(propertyName);
if (pos == -1)
return;
pos = scan(buffer, pos, "value"); //$NON-NLS-1$
pos = buffer.indexOf("value", pos); //$NON-NLS-1$
if (pos == -1)
return;
int begin = scan(buffer, pos, "\""); //$NON-NLS-1$
int begin = buffer.indexOf("\"", pos); //$NON-NLS-1$
if (begin == -1)
return;
begin++;
int end = scan(buffer, begin, "\""); //$NON-NLS-1$
int end = buffer.indexOf("\"", begin); //$NON-NLS-1$
if (end == -1)
return;
String currentVersion = buffer.substring(begin, end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -323,30 +324,33 @@ private void create30SourceFragment(FeatureEntry fragment, FeatureEntry plugin)
}

//Copy the fragment.xml
Path dest = new File(sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toOSString()).toPath();
Path dest = sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toPath();
try (InputStream fragmentXML = BundleHelper.getDefault().getBundle().getEntry(TEMPLATE + "/30/fragment/fragment.xml").openStream()) { //$NON-NLS-1$
Files.copy(fragmentXML, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_readingFile, TEMPLATE + "/30/fragment/fragment.xml"); //$NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e1));
}

StringBuffer buffer = Utils.readFile(templateLocation.openStream());
StringBuilder buffer;
try (InputStream locationContent = templateLocation.openStream()) {
buffer = new StringBuilder(new String(locationContent.readAllBytes()));
}
//Set the Id of the fragment
int beginId = Utils.scan(buffer, 0, REPLACED_FRAGMENT_ID);
int beginId = buffer.indexOf(REPLACED_FRAGMENT_ID, 0);
buffer.replace(beginId, beginId + REPLACED_FRAGMENT_ID.length(), fragment.getId());

Check warning on line 341 in build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/builder/SourceGenerator.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
// set the version number
beginId = Utils.scan(buffer, beginId, REPLACED_FRAGMENT_VERSION);
beginId = buffer.indexOf(REPLACED_FRAGMENT_VERSION, beginId);
buffer.replace(beginId, beginId + REPLACED_FRAGMENT_VERSION.length(), fragment.getVersion());

Check warning on line 344 in build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/builder/SourceGenerator.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
// Set the Id of the plugin for the fragment
beginId = Utils.scan(buffer, beginId, REPLACED_PLUGIN_ID);
beginId = buffer.indexOf(REPLACED_PLUGIN_ID, beginId);
buffer.replace(beginId, beginId + REPLACED_PLUGIN_ID.length(), plugin.getId());

Check warning on line 347 in build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/builder/SourceGenerator.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
// set the version number of the plugin to which the fragment is attached to
BundleDescription effectivePlugin = getSite().getRegistry().getResolvedBundle(plugin.getId(), plugin.getVersion());

Check warning on line 349 in build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/builder/SourceGenerator.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
beginId = Utils.scan(buffer, beginId, REPLACED_PLUGIN_VERSION);
beginId = buffer.indexOf(REPLACED_PLUGIN_VERSION, beginId);
buffer.replace(beginId, beginId + REPLACED_PLUGIN_VERSION.length(), effectivePlugin.getVersion().toString());
// Set the platform filter of the fragment
beginId = Utils.scan(buffer, beginId, REPLACED_PLATFORM_FILTER);
beginId = buffer.indexOf(REPLACED_PLATFORM_FILTER, beginId);
buffer.replace(beginId, beginId + REPLACED_PLATFORM_FILTER.length(), "(& (osgi.ws=" + fragment.getWS() + ") (osgi.os=" + fragment.getOS() + ") (osgi.arch=" + fragment.getArch() + "))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

Check warning on line 354 in build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/builder/SourceGenerator.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
Path destManifest = sourceFragmentDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toPath();
Files.writeString(destManifest, buffer.toString());
Expand Down Expand Up @@ -422,27 +426,27 @@ private void replaceXMLAttribute(String location, String tag, String attr, Strin
if (!featureFile.exists())
return;

StringBuffer buffer = null;
StringBuilder buffer = null;
try {
buffer = Utils.readFile(featureFile);
buffer = new StringBuilder(Files.readString(featureFile.toPath(), Charset.defaultCharset()));
} catch (IOException e) {
return;
}

int startComment = Utils.scan(buffer, 0, COMMENT_START_TAG);
int endComment = startComment > -1 ? Utils.scan(buffer, startComment, COMMENT_END_TAG) : -1;
int startTag = Utils.scan(buffer, 0, tag);
int startComment = buffer.indexOf(COMMENT_START_TAG, 0);
int endComment = startComment > -1 ? buffer.indexOf(COMMENT_END_TAG, startComment) : -1;
int startTag = buffer.indexOf(tag, 0);
while (startComment != -1 && startTag > startComment && startTag < endComment) {
startTag = Utils.scan(buffer, endComment, tag);
startComment = Utils.scan(buffer, endComment, COMMENT_START_TAG);
endComment = startComment > -1 ? Utils.scan(buffer, startComment, COMMENT_END_TAG) : -1;
startTag = buffer.indexOf(tag, endComment);
startComment = buffer.indexOf(COMMENT_START_TAG, endComment);
endComment = startComment > -1 ? buffer.indexOf(COMMENT_END_TAG, startComment) : -1;
}
if (startTag == -1)
return;
int endTag = Utils.scan(buffer, startTag, ">"); //$NON-NLS-1$
int endTag = buffer.indexOf(">", startTag); //$NON-NLS-1$
boolean attrFound = false;
while (!attrFound) {
int startAttributeWord = Utils.scan(buffer, startTag, attr);
int startAttributeWord = buffer.indexOf(attr, startTag);
if (startAttributeWord == -1 || startAttributeWord > endTag)
return;
if (!Character.isWhitespace(buffer.charAt(startAttributeWord - 1))) {
Expand All @@ -463,8 +467,8 @@ private void replaceXMLAttribute(String location, String tag, String attr, Strin
continue;
}

int startVersionId = Utils.scan(buffer, startAttributeWord + 1, "\""); //$NON-NLS-1$
int endVersionId = Utils.scan(buffer, startVersionId + 1, "\""); //$NON-NLS-1$
int startVersionId = buffer.indexOf("\"", startAttributeWord + 1); //$NON-NLS-1$
int endVersionId = buffer.indexOf("\"", startVersionId + 1); //$NON-NLS-1$
buffer.replace(startVersionId + 1, endVersionId, newValue);
attrFound = true;
}
Expand Down Expand Up @@ -672,28 +676,28 @@ private FeatureEntry create30SourcePlugin(BuildTimeFeature sourceFeature) throws
new File(sourcePluginDir, "META-INF").mkdirs(); //$NON-NLS-1$

// Create the MANIFEST.MF
StringBuffer buffer;
StringBuilder buffer;
IPath templateManifest = IPath.fromOSString(TEMPLATE + "/30/plugin/" + Constants.BUNDLE_FILENAME_DESCRIPTOR); //$NON-NLS-1$
URL templateManifestURL = BundleHelper.getDefault().find(templateManifest);
if (templateManifestURL == null) {
IStatus status = new Status(IStatus.WARNING, PI_PDEBUILD, IPDEBuildConstants.EXCEPTION_READING_FILE, NLS.bind(Messages.error_readingDirectory, templateManifest), null);
BundleHelper.getDefault().getLog().log(status);
return null;
}
try {
buffer = Utils.readFile(templateManifestURL.openStream());
try (InputStream manifestStream = templateManifestURL.openStream()) {
buffer = new StringBuilder(new String(manifestStream.readAllBytes()));
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_readingFile, templateManifestURL.toExternalForm());
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e1));
}
int beginId = Utils.scan(buffer, 0, REPLACED_PLUGIN_ID);
int beginId = buffer.indexOf(REPLACED_PLUGIN_ID, 0);
buffer.replace(beginId, beginId + REPLACED_PLUGIN_ID.length(), result.getId());
//set the version number
beginId = Utils.scan(buffer, beginId, REPLACED_PLUGIN_VERSION);
beginId = buffer.indexOf(REPLACED_PLUGIN_VERSION, beginId);
buffer.replace(beginId, beginId + REPLACED_PLUGIN_VERSION.length(), result.getVersion());
String destName = sourcePluginDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toOSString();
Path destName = sourcePluginDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toPath();
try {
Files.writeString(new File(destName).toPath(), buffer.toString());
Files.writeString(destName, buffer.toString());
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_writingFile, templateManifestURL.toExternalForm());
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e1));
Expand Down

0 comments on commit 8d76b3b

Please sign in to comment.