List of usage examples for org.eclipse.jdt.internal.core ClasspathEntry TAG_KIND
String TAG_KIND
To view the source code for org.eclipse.jdt.internal.core ClasspathEntry TAG_KIND.
Click Source Link
From source file:io.sarl.eclipse.util.JavaClasspathParser.java
License:Apache License
/** * Decodes one XML element with the XML stream. * * @param element// ww w . j a v a2 s.c om * - the considered element * @param projectName * - the name of project containing the .classpath file * @param projectRootAbsoluteFullPath * - he path to project containing the .classpath file * @param unknownElements * - map of unknown elements * @return the set of CLasspath ENtries extracted from the considered element */ @SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" }) public static IClasspathEntry elementDecode(Element element, String projectName, IPath projectRootAbsoluteFullPath, Map<IPath, UnknownXmlElements> unknownElements) { final IPath projectPath = projectRootAbsoluteFullPath; final NamedNodeMap attributes = element.getAttributes(); final NodeList children = element.getChildNodes(); final boolean[] foundChildren = new boolean[children.getLength()]; final String kindAttr = removeAttribute(ClasspathEntry.TAG_KIND, attributes); final String pathAttr = removeAttribute(ClasspathEntry.TAG_PATH, attributes); // ensure path is absolute IPath path = new Path(pathAttr); final int kind = kindFromString(kindAttr); if (kind != IClasspathEntry.CPE_VARIABLE && kind != IClasspathEntry.CPE_CONTAINER && !path.isAbsolute()) { if (!(path.segmentCount() > 0 && path.segment(0).equals(ClasspathEntry.DOT_DOT))) { path = projectPath.append(path); } } // source attachment info (optional) IPath sourceAttachmentPath = element.hasAttribute(ClasspathEntry.TAG_SOURCEPATH) ? new Path(removeAttribute(ClasspathEntry.TAG_SOURCEPATH, attributes)) : null; if (kind != IClasspathEntry.CPE_VARIABLE && sourceAttachmentPath != null && !sourceAttachmentPath.isAbsolute()) { sourceAttachmentPath = projectPath.append(sourceAttachmentPath); } final IPath sourceAttachmentRootPath = element.hasAttribute(ClasspathEntry.TAG_ROOTPATH) ? new Path(removeAttribute(ClasspathEntry.TAG_ROOTPATH, attributes)) : null; // exported flag (optional) final boolean isExported = removeAttribute(ClasspathEntry.TAG_EXPORTED, attributes).equals("true"); //$NON-NLS-1$ // inclusion patterns (optional) IPath[] inclusionPatterns = decodePatterns(attributes, ClasspathEntry.TAG_INCLUDING); if (inclusionPatterns == null) { inclusionPatterns = ClasspathEntry.INCLUDE_ALL; } // exclusion patterns (optional) IPath[] exclusionPatterns = decodePatterns(attributes, ClasspathEntry.TAG_EXCLUDING); if (exclusionPatterns == null) { exclusionPatterns = ClasspathEntry.EXCLUDE_NONE; } // access rules (optional) NodeList attributeList = getChildAttributes(ClasspathEntry.TAG_ACCESS_RULES, children, foundChildren); IAccessRule[] accessRules = decodeAccessRules(attributeList); // backward compatibility if (accessRules == null) { accessRules = getAccessRules(inclusionPatterns, exclusionPatterns); } // combine access rules (optional) final boolean combineAccessRestrictions = !removeAttribute(ClasspathEntry.TAG_COMBINE_ACCESS_RULES, attributes).equals("false"); //$NON-NLS-1$ // extra attributes (optional) attributeList = getChildAttributes(ClasspathEntry.TAG_ATTRIBUTES, children, foundChildren); final IClasspathAttribute[] extraAttributes = decodeExtraAttributes(attributeList); // custom output location final IPath outputLocation = element.hasAttribute(ClasspathEntry.TAG_OUTPUT) ? projectPath.append(removeAttribute(ClasspathEntry.TAG_OUTPUT, attributes)) : null; String[] unknownAttributes = null; ArrayList<String> unknownChildren = null; if (unknownElements != null) { // unknown attributes final int unknownAttributeLength = attributes.getLength(); if (unknownAttributeLength != 0) { unknownAttributes = new String[unknownAttributeLength * 2]; for (int i = 0; i < unknownAttributeLength; i++) { final Node attribute = attributes.item(i); unknownAttributes[i * 2] = attribute.getNodeName(); unknownAttributes[i * 2 + 1] = attribute.getNodeValue(); } } // unknown children for (int i = 0, length = foundChildren.length; i < length; i++) { if (!foundChildren[i]) { final Node node = children.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } if (unknownChildren == null) { unknownChildren = new ArrayList<>(); } final StringBuffer buffer = new StringBuffer(); decodeUnknownNode(node, buffer); unknownChildren.add(buffer.toString()); } } } // recreate the CP entry IClasspathEntry entry = null; switch (kind) { case IClasspathEntry.CPE_PROJECT: /* * IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_PROJECT, path, ClasspathEntry.INCLUDE_ALL, // inclusion patterns * ClasspathEntry.EXCLUDE_NONE, // exclusion patterns null, // source attachment null, // source attachment root null, // specific output * folder */ entry = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_PROJECT, path, ClasspathEntry.INCLUDE_ALL, ClasspathEntry.EXCLUDE_NONE, null, null, null, isExported, accessRules, combineAccessRestrictions, extraAttributes); break; case IClasspathEntry.CPE_LIBRARY: entry = JavaCore.newLibraryEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported); break; case IClasspathEntry.CPE_SOURCE: // must be an entry in this project or specify another project final String projSegment = path.segment(0); if (projSegment != null && projSegment.equals(projectName)) { // this project entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes); } else { if (path.segmentCount() == 1) { // another project entry = JavaCore.newProjectEntry(path, accessRules, combineAccessRestrictions, extraAttributes, isExported); } else { // an invalid source folder entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes); } } break; case IClasspathEntry.CPE_VARIABLE: entry = JavaCore.newVariableEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported); break; case IClasspathEntry.CPE_CONTAINER: entry = JavaCore.newContainerEntry(path, accessRules, extraAttributes, isExported); break; case ClasspathEntry.K_OUTPUT: if (!path.isAbsolute()) { return null; } /* * ClasspathEntry.EXCLUDE_NONE, null, // source attachment null, // source attachment root null, // custom output location false, null, // * no access rules false, // no accessible files to combine */ entry = new ClasspathEntry(ClasspathEntry.K_OUTPUT, IClasspathEntry.CPE_LIBRARY, path, ClasspathEntry.INCLUDE_ALL, ClasspathEntry.EXCLUDE_NONE, null, null, null, false, null, false, ClasspathEntry.NO_EXTRA_ATTRIBUTES); break; default: throw new AssertionFailedException(Messages.bind(Messages.classpath_unknownKind, kindAttr)); } if (unknownAttributes != null || unknownChildren != null) { final UnknownXmlElements unknownXmlElements = new UnknownXmlElements(); unknownXmlElements.attributes = unknownAttributes; unknownXmlElements.children = unknownChildren; if (unknownElements != null) { unknownElements.put(path, unknownXmlElements); } } return entry; }
From source file:io.sarl.eclipse.util.JavaClasspathParser.java
License:Apache License
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:innerassignment" })
private static IAccessRule[] decodeAccessRules(NodeList list) {
if (list == null) {
return null;
}//from w w w . j a va 2s. c o m
final int length = list.getLength();
if (length == 0) {
return null;
}
IAccessRule[] result = new IAccessRule[length];
int index = 0;
for (int i = 0; i < length; i++) {
final Node accessRule = list.item(i);
if (accessRule.getNodeType() == Node.ELEMENT_NODE) {
final Element elementAccessRule = (Element) accessRule;
final String pattern = elementAccessRule.getAttribute(ClasspathEntry.TAG_PATTERN);
if (pattern == null) {
continue;
}
final String tagKind = elementAccessRule.getAttribute(ClasspathEntry.TAG_KIND);
final int kind;
if (ClasspathEntry.TAG_ACCESSIBLE.equals(tagKind)) {
kind = IAccessRule.K_ACCESSIBLE;
} else if (ClasspathEntry.TAG_NON_ACCESSIBLE.equals(tagKind)) {
kind = IAccessRule.K_NON_ACCESSIBLE;
} else if (ClasspathEntry.TAG_DISCOURAGED.equals(tagKind)) {
kind = IAccessRule.K_DISCOURAGED;
} else {
continue;
}
final boolean ignoreIfBetter = "true" //$NON-NLS-1$
.equals(elementAccessRule.getAttribute(ClasspathEntry.TAG_IGNORE_IF_BETTER));
result[index++] = new ClasspathAccessRule(new Path(pattern),
ignoreIfBetter ? kind | IAccessRule.IGNORE_IF_BETTER : kind);
}
}
if (index != length) {
System.arraycopy(result, 0, result = new IAccessRule[index], 0, index);
}
return result;
}
From source file:org.eclipse.jdt.internal.core.JavaProject.java
License:Open Source License
/** * Returns the XML String encoding of the class path. *//*w w w .j a v a 2 s. c o m*/ protected String encodeClasspath(IClasspathEntry[] classpath, IClasspathEntry[] referencedEntries, IPath outputLocation, boolean indent, Map unknownElements) throws JavaModelException { try { ByteArrayOutputStream s = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$ XMLWriter xmlWriter = new XMLWriter(writer, this, true/*print XML version*/); 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/*insert new line*/); writer.flush(); writer.close(); return s.toString("UTF8");//$NON-NLS-1$ } catch (IOException e) { throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); } }