org.eclipse.xtext.util.MergeableManifest2 Java Examples

The following examples show how to use org.eclipse.xtext.util.MergeableManifest2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WorkbenchTestHelper.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @return a {@link Pair}, where the <code>key</code> is the BREE that was set and the <code>value</code> indicates that the BREE was changed.
 */
public static Pair<String, Boolean> changeBree(IJavaProject javaProject, final JavaVersion javaVersion)
		throws Exception {
	final AtomicReference<String> bree = new AtomicReference<String>();
	boolean changed = changeManifest(javaProject.getProject(), new Function<MergeableManifest2, Boolean>() {
		@Override
		public Boolean apply(MergeableManifest2 mf) {
			mf.setBREE(getBree(javaVersion));
			return mf.isModified();
		}
	});
	if (changed) {
		JavaProjectSetupUtil.addJreClasspathEntry(javaProject, bree.get());
	}
	return Pair.of(bree.get(), changed);
}
 
Example #2
Source File: AbstractJunitLibClasspathAdderTestCase.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void assertRequireBundles(final String[] expectedBundleIds) {
  try {
    IProject _project = this.workbenchHelper.getProject();
    Path _path = new Path("META-INF/MANIFEST.MF");
    InputStream _contents = _project.getFile(_path).getContents();
    final MergeableManifest2 manifest = new MergeableManifest2(_contents);
    final String requireBunbles = manifest.getMainAttributes().get(MergeableManifest2.REQUIRE_BUNDLE);
    for (final String bundleId : expectedBundleIds) {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("require bundle entry ");
      _builder.append(bundleId);
      _builder.append(" is present");
      Assert.assertTrue(_builder.toString(), requireBunbles.contains(bundleId));
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #3
Source File: ManifestAccess.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void writeTo(final IFileSystemAccess2 fileSystemAccess) {
  try {
    if ((fileSystemAccess != null)) {
      CharSequence _content = this.getContent();
      StringBuffer _stringBuffer = new StringBuffer(_content);
      final String contentToWrite = MergeableManifest2.make512Safe(_stringBuffer, this.lineDelimiter);
      byte[] _bytes = contentToWrite.getBytes("UTF-8");
      ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_bytes);
      final MergeableManifest2 mergeableManifest = new MergeableManifest2(_byteArrayInputStream);
      mergeableManifest.setLineDelimiter(this.lineDelimiter);
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      mergeableManifest.write(bout);
      String _path = this.getPath();
      byte[] _byteArray = bout.toByteArray();
      ByteArrayInputStream _byteArrayInputStream_1 = new ByteArrayInputStream(_byteArray);
      fileSystemAccess.generateFile(_path, _byteArrayInputStream_1);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #4
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMergeExportedPackages() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	assertFalse(manifest.isModified());

	manifest.addExportedPackages(Collections.singleton("org.eclipse.xtext"));
	assertFalse(manifest.isModified());

	manifest.addRequiredBundles(Collections.singleton("org.eclipse.xtend"));
	assertFalse(manifest.isModified());

	manifest.addImportedPackages(Collections.singleton("org.apache.log4j"));
	assertFalse(manifest.isModified());
}
 
Example #5
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNoLongLine() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	manifest.addExportedPackages(Collections.singleton("foobar"));
	assertTrue(manifest.isModified());
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	manifest.write(out);
	String result = out.toString();
	String lookup = "Require-Bundle: org.eclipse.xtext,";
	int idx = result.indexOf(lookup);
	assertTrue(idx != -1);
	idx += lookup.length();
	String lineDelimiter = Strings.newLine();
	for (int i = 0; i < lineDelimiter.length(); i++) {
		assertEquals(result, lineDelimiter.charAt(i), result.charAt(idx + i));
	}
	assertEquals(result, ' ', result.charAt(idx + lineDelimiter.length()));
}
 
Example #6
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSplit512Length() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	char[] buff = new char[712];
	Arrays.fill(buff, 'c');
	manifest.addExportedPackages(Collections.singleton(new String(buff)));
	assertTrue(manifest.isModified());
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	manifest.write(out);
	String result = out.toString();
	try {
		new Manifest(new StringInputStream(result));
	} catch (Exception e) {
		fail("long line has not been splitted into chunks");
	}
}
 
Example #7
Source File: WorkbenchTestHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean  addExportedPackages(IProject project, final String ... exportedPackages) throws Exception{
	return changeManifest(project, new Function<MergeableManifest2,Boolean>() {
		@Override
		public Boolean apply(MergeableManifest2 mf) {
			 mf.addExportedPackages(exportedPackages);
			return mf.isModified();
		}
	});
}
 
Example #8
Source File: WorkbenchTestHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean  removeExportedPackages(IProject project, final String... exportedPackages) throws Exception {
	return changeManifest(project, new Function<MergeableManifest2, Boolean>() {
		@Override
		public Boolean apply(MergeableManifest2 mf) {
			Attributes attrs = mf.getMainAttributes();
			String expPackKey = "Export-Package";
			String oldValue = attrs.get(expPackKey);
			if (!Strings.isNullOrEmpty(oldValue)) {
				String[] existingExports = oldValue.split(",(\\s+)?");
				System.out.println(Arrays.toString(existingExports));
				List<String> exportsToKeep = new ArrayList<String>();
				for (String string : existingExports) {
					exportsToKeep.add(string.trim());
				}
				boolean changed = exportsToKeep.removeAll(Arrays.asList(exportedPackages));
				String valueToSet = Joiner.on(',').join(exportsToKeep);
				if (changed) {
					if (valueToSet.isEmpty()) {
						attrs.remove(expPackKey);
					} else {
						attrs.put(expPackKey, valueToSet);
					}
					return true;
				}
			}
			return false;
		}
	});
}
 
Example #9
Source File: AbstractLibClasspathAdder.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private MergeableManifest2 createMergableManifest(IResource manifestFile) throws IOException, CoreException {
	InputStream originalManifest = ((IFile) manifestFile).getContents();
	try {
		return new MergeableManifest2(originalManifest);
	} finally {
		originalManifest.close();
	}
}
 
Example #10
Source File: XtextGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void mergeManifest(final ManifestAccess manifest, final IXtextGeneratorFileSystemAccess metaInf) throws IOException {
  InputStream in = null;
  try {
    in = metaInf.readBinaryFile(manifest.getPath());
    String _bundleName = manifest.getBundleName();
    final MergeableManifest2 merge = new MergeableManifest2(in, _bundleName);
    merge.setLineDelimiter(this.codeConfig.getLineDelimiter());
    merge.addExportedPackages(manifest.getExportedPackages());
    merge.addRequiredBundles(manifest.getRequiredBundles());
    merge.addImportedPackages(manifest.getImportedPackages());
    if (((manifest.getActivator() != null) && StringExtensions.isNullOrEmpty(merge.getBundleActivator()))) {
      merge.setBundleActivator(manifest.getActivator().getName());
    }
    boolean _isModified = merge.isModified();
    if (_isModified) {
      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      merge.write(out);
      String _path = manifest.getPath();
      byte[] _byteArray = out.toByteArray();
      ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
      metaInf.generateFile(_path, _byteArrayInputStream);
    }
  } finally {
    if ((in != null)) {
      in.close();
    }
  }
}
 
Example #11
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeRequiredBundles() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	Attributes attrs = manifest.getMainAttributes();
	String before = attrs.get(MergeableManifest2.REQUIRE_BUNDLE).replaceAll("\\s", "");
	manifest.addRequiredBundles(Collections.singleton("foo.bar.baz"));
	String after = attrs.get(MergeableManifest2.REQUIRE_BUNDLE);
	assertEquals(before + ",foo.bar.baz", after.replaceAll("\\s", ""));
}
 
Example #12
Source File: ManifestMerger2Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoChanges() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest2 manifest = new MergeableManifest2(resourceAsStream);
	Attributes attrs = manifest.getMainAttributes();
	String before = attrs.get(MergeableManifest2.EXPORT_PACKAGE).replaceAll("\\s", "");
	manifest.addExportedPackages(Collections.singleton("foo.bar.baz"));
	String after = attrs.get(MergeableManifest2.EXPORT_PACKAGE);
	assertEquals(before + ",foo.bar.baz", after.replaceAll("\\s", ""));
}