use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProject method encodeClasspathEntry.
public String encodeClasspathEntry(IClasspathEntry classpathEntry) {
try {
ByteArrayOutputStream s = new ByteArrayOutputStream();
//$NON-NLS-1$
OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8");
XMLWriter xmlWriter = new XMLWriter(writer, this, false);
((ClasspathEntry) classpathEntry).elementEncode(xmlWriter, this.project.getFullPath(), true, /*indent*/
true, /*insert new line*/
null, /*not interested in unknown elements*/
(classpathEntry.getReferencingEntry() != null));
writer.flush();
writer.close();
//$NON-NLS-1$
return s.toString("UTF8");
} catch (IOException e) {
// never happens since all is done in memory
return null;
}
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProject method readFileEntriesWithException.
/**
* Reads the classpath file entries of this project's .classpath file.
* Returns a two-dimensional array, where the number of elements in the row is fixed to 2.
* The first element is an array of raw classpath entries, which includes the output entry,
* and the second element is an array of referenced entries that may have been stored
* by the client earlier.
* See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
* As a side effect, unknown elements are stored in the given map (if not null)
* Throws exceptions if the file cannot be accessed or is malformed.
*/
public IClasspathEntry[][] readFileEntriesWithException(Map unknownElements) throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
IFile rscFile = this.project.getFile(org.eclipse.jdt.internal.core.JavaProject.CLASSPATH_FILENAME);
byte[] bytes;
if (rscFile.exists()) {
bytes = Util.getResourceContentsAsByteArray(rscFile);
} else {
// if (!file.exists())
return new IClasspathEntry[][] { defaultClasspath(), ClasspathEntry.NO_ENTRIES };
// throw e;
// }
}
if (hasUTF8BOM(bytes)) {
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
int length = bytes.length - IContentDescription.BOM_UTF_8.length;
System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
}
String xmlClasspath;
try {
// .classpath always encoded with UTF-8
xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
} catch (UnsupportedEncodingException e) {
//$NON-NLS-1$
Util.log(e, "Could not read .classpath with UTF-8 encoding");
// fallback to default
xmlClasspath = new String(bytes);
}
return decodeClasspath(xmlClasspath, unknownElements);
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProject method decodeClasspath.
/**
* Reads and decode an XML classpath string. Returns a two-dimensional array, where the number of elements in the row is fixed to 2.
* The first element is an array of raw classpath entries and the second element is an array of referenced entries that may have been stored
* by the client earlier. See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
*
*/
public IClasspathEntry[][] decodeClasspath(String xmlClasspath, Map unknownElements) throws IOException, ClasspathEntry.AssertionFailedException {
ArrayList paths = new ArrayList();
IClasspathEntry defaultOutput = null;
StringReader reader = new StringReader(xmlClasspath);
Element cpElement;
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
cpElement = parser.parse(new InputSource(reader)).getDocumentElement();
} catch (SAXException e) {
throw new IOException(Messages.file_badFormat);
} catch (ParserConfigurationException e) {
throw new IOException(Messages.file_badFormat);
} finally {
reader.close();
}
if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) {
//$NON-NLS-1$
throw new IOException(Messages.file_badFormat);
}
NodeList list = cpElement.getElementsByTagName(ClasspathEntry.TAG_CLASSPATHENTRY);
int length = list.getLength();
for (int i = 0; i < length; ++i) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
IClasspathEntry entry = ClasspathEntry.elementDecode((Element) node, this, unknownElements);
if (entry != null) {
if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
// separate output
defaultOutput = entry;
} else {
paths.add(entry);
}
}
}
}
int pathSize = paths.size();
IClasspathEntry[][] entries = new IClasspathEntry[2][];
entries[0] = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
paths.toArray(entries[0]);
// ensure output is last item
if (defaultOutput != null)
entries[0][pathSize] = defaultOutput;
paths.clear();
list = cpElement.getElementsByTagName(ClasspathEntry.TAG_REFERENCED_ENTRY);
length = list.getLength();
for (int i = 0; i < length; ++i) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
IClasspathEntry entry = ClasspathEntry.elementDecode((Element) node, this, unknownElements);
if (entry != null) {
paths.add(entry);
}
}
}
entries[1] = new IClasspathEntry[paths.size()];
paths.toArray(entries[1]);
return entries;
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProject method encodeClasspath.
/**
* Returns the XML String encoding of the class path.
*/
protected String encodeClasspath(IClasspathEntry[] classpath, IClasspathEntry[] referencedEntries, IPath outputLocation, boolean indent, Map unknownElements) throws JavaModelException {
try {
ByteArrayOutputStream s = new ByteArrayOutputStream();
//$NON-NLS-1$
OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8");
XMLWriter xmlWriter = new XMLWriter(writer, this, true);
xmlWriter.startTag(ClasspathEntry.TAG_CLASSPATH, indent);
for (int i = 0; i < classpath.length; ++i) {
((ClasspathEntry) classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true, unknownElements, false);
}
if (outputLocation != null) {
outputLocation = outputLocation.removeFirstSegments(1);
outputLocation = outputLocation.makeRelative();
HashMap parameters = new HashMap();
parameters.put(ClasspathEntry.TAG_KIND, ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));
parameters.put(ClasspathEntry.TAG_PATH, String.valueOf(outputLocation));
xmlWriter.printTag(ClasspathEntry.TAG_CLASSPATHENTRY, parameters, indent, true, true);
}
if (referencedEntries != null) {
for (int i = 0; i < referencedEntries.length; ++i) {
((ClasspathEntry) referencedEntries[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true, unknownElements, true);
}
}
xmlWriter.endTag(ClasspathEntry.TAG_CLASSPATH, indent, true);
writer.flush();
writer.close();
//$NON-NLS-1$
return s.toString("UTF8");
} catch (IOException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProject method projectPrerequisites.
public String[] projectPrerequisites(IClasspathEntry[] resolvedClasspath) throws JavaModelException {
ArrayList prerequisites = new ArrayList();
for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
IClasspathEntry entry = resolvedClasspath[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
prerequisites.add(entry.getPath().lastSegment());
}
}
int size = prerequisites.size();
if (size == 0) {
return NO_PREREQUISITES;
} else {
String[] result = new String[size];
prerequisites.toArray(result);
return result;
}
}
Aggregations