use of com.ibm.icu.util.StringTokenizer in project webtools.sourceediting by eclipse.
the class JSPTranslator method isTypeFound.
/**
* @param type
* @return
*/
private boolean isTypeFound(String rawTypeValue, List errorTypeNames) {
// If the translation is being loaded from disk, the model and structured document may not have been intiailized yet
IFile file = getStructuredDocument() != null ? getFile() : (fSavedModelPath != null ? ResourcesPlugin.getWorkspace().getRoot().getFile(fSavedModelPath) : null);
if (file == null)
return true;
IProject project = file.getProject();
IJavaProject p = JavaCore.create(project);
if (p.exists()) {
List types = new ArrayList();
if (rawTypeValue.indexOf('<') > 0) {
// JSR 14 : Generics are being used, parse them out
try {
StringTokenizer toker = new StringTokenizer(rawTypeValue);
// $NON-NLS-1$
String token = toker.nextToken("<,>/\"");
while (token != null) {
types.add(token);
// $NON-NLS-1$
token = toker.nextToken("<,>/\"");
}
} catch (NoSuchElementException e) {
// StringTokenizer failure with unsupported syntax
return true;
}
} else {
types.add(rawTypeValue);
}
for (int i = 0; i < types.size(); i++) {
String typeName = types.get(i).toString();
// remove any array suffixes
if (typeName.indexOf('[') > 0 && typeName.indexOf(']') > typeName.indexOf('[')) {
typeName = typeName.substring(0, typeName.indexOf('['));
}
// remove any "extends" prefixes (JSR 14)
if (typeName.indexOf("extends") > 0) {
// $NON-NLS-1$
// $NON-NLS-1$
typeName = StringUtils.strip(typeName.substring(typeName.indexOf("extends")));
}
addNameToListIfTypeNotFound(p, typeName, errorTypeNames);
}
}
return errorTypeNames.isEmpty();
}
use of com.ibm.icu.util.StringTokenizer in project webtools.sourceediting by eclipse.
the class JSPTranslator method appendUseBeanToBuffer.
/**
* temp fix for 282295 until better mapping is in place
*
* @param newText
* @param jspReferenceRegion
*/
private void appendUseBeanToBuffer(String newText, ITextRegionCollection jspReferenceRegion, boolean isIndirect) throws Exception {
// java string looks like this (tokenized)
// Type id = new Classname();\n
// 0 1 2 3 4
// or
// Type id = null;\n // if there is no classname
// 0 1 2 3
// ----------------------
// calculate java ranges
// ----------------------
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(newText, " ", false);
int i = 0;
String[] parsedJava = new String[st.countTokens()];
while (st.hasMoreTokens()) parsedJava[i++] = st.nextToken();
// $NON-NLS-1$
String type = parsedJava[0] != null ? parsedJava[0] : "";
// $NON-NLS-1$
String id = parsedJava[1] != null ? parsedJava[1] : "";
// $NON-NLS-1$
String className = parsedJava.length > 4 ? parsedJava[4] : "";
Position javaTypeRange = new Position(fOffsetInUserCode, type.length());
Position javaIdRange = new Position(fOffsetInUserCode + type.length() + 1, id.length());
Position javaClassRange = new Position(fOffsetInUserCode + type.length() + 1 + id.length() + 7, 0);
/*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=212242 - Check for
* the existence of '(' first.
*/
int parenPos = -1;
if (className.length() >= 4 && (parenPos = className.indexOf('(')) >= 0) {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=86132
int classNameLength = className.substring(0, parenPos).length();
javaClassRange = new Position(fOffsetInUserCode + type.length() + 1 + id.length() + 7, classNameLength);
}
// ---------------------
// calculate jsp ranges
// ---------------------
ITextRegionList regions = jspReferenceRegion.getRegions();
ITextRegion r = null;
// $NON-NLS-1$ //$NON-NLS-2$
String attrName = "", attrValue = "";
int quoteOffset = 0;
Position jspTypeRange = null;
Position jspIdRange = null;
Position jspClassRange = null;
for (int j = 0; j < regions.size(); j++) {
r = regions.get(j);
if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
attrName = jspReferenceRegion.getText(r);
if (regions.size() > j + 2 && regions.get(j + 2).getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
// get attr value
r = regions.get(j + 2);
attrValue = jspReferenceRegion.getText(r);
// may have quotes
// $NON-NLS-1$ //$NON-NLS-2$
quoteOffset = (attrValue.startsWith("\"") || attrValue.startsWith("'")) ? 1 : 0;
if (// $NON-NLS-1$
attrName.equals("type"))
jspTypeRange = new Position(jspReferenceRegion.getStartOffset(r) + quoteOffset, StringUtils.stripQuotesLeaveInsideSpace(attrValue).length());
else if (// $NON-NLS-1$
attrName.equals("id"))
jspIdRange = new Position(jspReferenceRegion.getStartOffset(r) + quoteOffset, StringUtils.stripQuotesLeaveInsideSpace(attrValue).length());
else if (// $NON-NLS-1$
attrName.equals("class"))
jspClassRange = new Position(jspReferenceRegion.getStartOffset(r) + quoteOffset, StringUtils.stripQuotesLeaveInsideSpace(attrValue).length());
}
}
}
// put ranges in java -> jsp range map
if (!type.equals("") && jspTypeRange != null) {
// $NON-NLS-1$
fCodeRanges.put(javaTypeRange, jspTypeRange);
// note: don't update offsets for this map when result is built
// they'll be updated when code ranges offsets are updated
fUseBeanRanges.put(javaTypeRange, jspTypeRange);
if (isIndirect)
fIndirectRanges.put(javaTypeRange, jspTypeRange);
}
if (!id.equals("") && jspIdRange != null) {
// $NON-NLS-1$
fCodeRanges.put(javaIdRange, jspIdRange);
// note: don't update offsets for this map when result is built
// they'll be updated when code ranges offsets are updated
fUseBeanRanges.put(javaIdRange, jspTypeRange);
if (isIndirect)
fIndirectRanges.put(javaIdRange, jspTypeRange);
}
if (!className.equals("") && jspClassRange != null) {
// $NON-NLS-1$
fCodeRanges.put(javaClassRange, jspClassRange);
// note: don't update offsets for this map when result is built
// they'll be updated when code ranges offsets are updated
fUseBeanRanges.put(javaClassRange, jspTypeRange);
if (isIndirect)
fIndirectRanges.put(javaClassRange, jspTypeRange);
}
}
use of com.ibm.icu.util.StringTokenizer in project webtools.sourceediting by eclipse.
the class JSPTranslator method translateXMLNode.
/**
* translates the various XMLJSP type nodes
*
* @param regions
* the regions of the XMLNode
*/
protected void translateXMLNode(ITextRegionCollection container, Iterator regions) {
// contents must be valid XHTML, translate escaped CDATA into what it
// really is...
ITextRegion r = null;
if (regions.hasNext()) {
r = (ITextRegion) regions.next();
// <jsp:directive.xxx > comes in as this
if (r.getType() == DOMRegionContext.XML_TAG_NAME || r.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) {
String fullTagName = container.getText(r);
if (fullTagName.indexOf(':') > -1 && !fullTagName.startsWith(JSP_PREFIX)) {
// it
addTaglibVariables(fullTagName, container, -1);
// may
// be a
// custom
// tag
}
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(fullTagName, ":.", false);
if (// $NON-NLS-1$
st.hasMoreTokens() && st.nextToken().equals("jsp")) {
if (st.hasMoreTokens()) {
String jspTagName = st.nextToken();
if (// $NON-NLS-1$
jspTagName.equals("scriptlet")) {
translateXMLJSPContent(SCRIPTLET);
} else if (// $NON-NLS-1$
jspTagName.equals("expression")) {
translateXMLJSPContent(EXPRESSION);
} else if (// $NON-NLS-1$
jspTagName.equals("declaration")) {
translateXMLJSPContent(DECLARATION);
} else if (// $NON-NLS-1$
jspTagName.equals("directive")) {
if (st.hasMoreTokens()) {
String directiveName = st.nextToken();
if (directiveName.equals("taglib")) {
// $NON-NLS-1$
while (r != null && regions.hasNext() && !r.getType().equals(DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)) {
r = (ITextRegion) regions.next();
if (container.getText(r).equals(JSP11Namespace.ATTR_NAME_PREFIX)) {
String prefix = getAttributeValue(r, regions);
if (prefix != null) {
handleTaglib(prefix);
}
}
}
return;
} else if (directiveName.equals("include")) {
// $NON-NLS-1$
// $NON-NLS-1$
String fileLocation = "";
// include directive
while (r != null && regions.hasNext() && !r.getType().equals(DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)) {
r = (ITextRegion) regions.next();
}
fileLocation = getAttributeValue(r, regions);
if (fileLocation != null)
handleIncludeFile(fileLocation);
} else if (directiveName.equals("page")) {
// setCurrentNode(getCurrentNode().getNext());
if (getCurrentNode() != null) {
// 'regions' contain the attrs
translatePageDirectiveAttributes(regions, getCurrentNode());
}
} else if (directiveName.equals("tag")) {
// $NON-NLS-1$
translatePageDirectiveAttributes(regions, getCurrentNode());
} else if (directiveName.equals("variable")) {
// $NON-NLS-1$
translateVariableDirectiveAttributes(regions);
}
}
} else if (jspTagName.equals("include")) {
// $NON-NLS-1$
// <jsp:include page="filename") />
// $NON-NLS-1$
checkAttributeValueContainer(regions, "page");
} else if (jspTagName.equals("forward")) {
// $NON-NLS-1$
// $NON-NLS-1$
checkAttributeValueContainer(regions, "page");
} else if (jspTagName.equals("param")) {
// $NON-NLS-1$
// $NON-NLS-1$
checkAttributeValueContainer(regions, "value");
} else if (jspTagName.equals("setProperty")) {
// $NON-NLS-1$
// $NON-NLS-1$
checkAttributeValueContainer(regions, "value");
} else if (// $NON-NLS-1$
jspTagName.equals("useBean")) {
// $NON-NLS-1$
checkAttributeValueContainer(regions, "name");
// advanceNextNode(); // get the content
if (getCurrentNode() != null) {
// 'regions'
translateUseBean(container);
}
}
}
} else {
checkAllAttributeValueContainers(container, regions);
}
}
}
}
use of com.ibm.icu.util.StringTokenizer in project webtools.sourceediting by eclipse.
the class PathHelper method convertToRelative.
/**
* Convert to relative url based on base
*/
public static String convertToRelative(String input, String base) {
// tokenize the strings
StringTokenizer inputTokenizer = new StringTokenizer(input, FORWARD_SLASH);
StringTokenizer baseTokenizer = new StringTokenizer(base, FORWARD_SLASH);
// $NON-NLS-2$//$NON-NLS-1$
String token1 = "", token2 = "";
// Go through until equls
while (true) {
if (!inputTokenizer.hasMoreTokens() || !baseTokenizer.hasMoreTokens())
break;
token1 = baseTokenizer.nextToken();
token2 = inputTokenizer.nextToken();
if (!token1.equals(token2))
break;
}
// now generate the backs
// $NON-NLS-1$
String output = "";
StringBuffer sb = new StringBuffer(output);
while (baseTokenizer.hasMoreTokens()) {
baseTokenizer.nextToken();
// $NON-NLS-1$
sb.append("../");
}
sb.append(token2);
// generate the rest
while (inputTokenizer.hasMoreTokens()) {
sb.append(FORWARD_SLASH);
sb.append(inputTokenizer.nextToken());
}
output = sb.toString();
return output;
}
use of com.ibm.icu.util.StringTokenizer in project webtools.sourceediting by eclipse.
the class ProjectDescription method restoreReferences.
/**
* Restores any saved reference tables
*/
private void restoreReferences() {
final boolean notifyOnRestoration = true;
if (TaglibIndex.ENABLED) {
// resources first
index();
// now build path
// ================ test reload time ========================
boolean restored = false;
File savedState = new File(fSaveStateFilename);
if (savedState.exists() && !requestedRefresh()) {
Reader reader = null;
try {
time0 = System.currentTimeMillis();
// $NON-NLS-1$
reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(savedState)), "UTF-16");
// use a string buffer temporarily to reduce string
// creation
StringBuffer buffer = new StringBuffer();
char[] array = new char[2048];
int charsRead = 0;
while ((charsRead = reader.read(array)) != -1) {
if (charsRead > 0) {
buffer.append(array, 0, charsRead);
}
}
IDocument doc = new org.eclipse.jface.text.Document();
doc.set(buffer.toString());
int lines = doc.getNumberOfLines();
if (lines > 0) {
IRegion line = doc.getLineInformation(0);
String lineText = doc.get(line.getOffset(), line.getLength());
JarRecord libraryRecord = null;
if (SAVE_FORMAT_VERSION.equals(lineText.trim())) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
for (int i = 1; i < lines && !fBuildPathIsDirty; i++) {
line = doc.getLineInformation(i);
lineText = doc.get(line.getOffset(), line.getLength());
// $NON-NLS-1$
StringTokenizer toker = new StringTokenizer(lineText, "|");
if (toker.hasMoreTokens()) {
String tokenType = toker.nextToken();
if ("JAR".equalsIgnoreCase(tokenType)) {
// $NON-NLS-1$ //$NON-NLS-2$
boolean has11TLD = Boolean.valueOf(toker.nextToken()).booleanValue();
boolean exported = Boolean.valueOf(toker.nextToken()).booleanValue();
// make the rest the libraryLocation
String libraryLocation = toker.nextToken();
while (toker.hasMoreTokens()) {
// $NON-NLS-1$ //$NON-NLS-2$
libraryLocation = libraryLocation + "|" + toker.nextToken();
}
libraryLocation = libraryLocation.trim();
if (libraryRecord != null && notifyOnRestoration) {
TaglibIndex.getInstance().addDelta(new TaglibIndexDelta(fProject, libraryRecord, ITaglibIndexDelta.ADDED));
}
// Create a new JarRecord
libraryRecord = createJARRecord(libraryLocation);
libraryRecord.has11TLD = has11TLD;
libraryRecord.isExported = exported;
// Add a URLRecord for the 1.1 TLD
if (has11TLD) {
InputStream contents = JarUtilities.getInputStream(libraryLocation, JarUtilities.JSP11_TAGLIB);
if (contents != null) {
TaglibInfo info = extractInfo(libraryLocation, contents);
if (info != null && info.uri != null && info.uri.length() > 0) {
URLRecord urlRecord = new URLRecord();
urlRecord.info = info;
urlRecord.isExported = exported;
urlRecord.baseLocation = libraryLocation;
try {
// $NON-NLS-1$ //$NON-NLS-2$
urlRecord.url = new URL("jar:file:" + libraryLocation + "!/" + JarUtilities.JSP11_TAGLIB);
libraryRecord.urlRecords.add(urlRecord);
fClasspathReferences.put(urlRecord.getURI(), urlRecord);
if (_debugIndexCreation)
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "created record for " + urlRecord.getURI() + "@" + urlRecord.getURL());
} catch (MalformedURLException e) {
/*
* don't record this
* URI
*/
Logger.logException(e);
}
}
try {
contents.close();
} catch (IOException e) {
Logger.log(Logger.ERROR_DEBUG, null, e);
}
}
}
fClasspathJars.put(libraryLocation, libraryRecord);
} else if ("URL".equalsIgnoreCase(tokenType) && libraryRecord != null) {
// $NON-NLS-1$
// relies on a previously declared JAR record
boolean exported = Boolean.valueOf(toker.nextToken()).booleanValue();
// make the rest the URL
String urlString = toker.nextToken();
while (toker.hasMoreTokens()) {
// $NON-NLS-1$ //$NON-NLS-2$
urlString = urlString + "|" + toker.nextToken();
}
urlString = urlString.trim();
// Append a URLrecord
URLRecord urlRecord = new URLRecord();
urlRecord.url = new URL(urlString);
urlRecord.isExported = exported;
urlRecord.baseLocation = libraryRecord.location.toString();
InputStream tldStream = JarUtilities.getInputStream(urlRecord.url);
if (tldStream != null) {
TaglibInfo info = extractInfo(urlRecord.url.toString(), tldStream);
if (info != null) {
urlRecord.info = info;
}
libraryRecord.urlRecords.add(urlRecord);
try {
tldStream.close();
} catch (IOException e) {
Logger.log(Logger.ERROR_DEBUG, null, e);
}
if (urlRecord.getURI() != null && urlRecord.getURI().length() > 0) {
fClasspathReferences.put(urlRecord.getURI(), urlRecord);
}
}
} else if (BUILDPATH_PROJECT.equalsIgnoreCase(tokenType)) {
String projectName = toker.nextToken();
if (Path.ROOT.isValidSegment(projectName)) {
IProject project = workspaceRoot.getProject(projectName);
/* do not check if "open" here */
if (project != null) {
fClasspathProjects.add(project);
}
}
} else // last since they occur once
if (BUILDPATH_DIRTY.equalsIgnoreCase(tokenType)) {
fBuildPathIsDirty = Boolean.valueOf(toker.nextToken()).booleanValue();
} else if (BUILDPATH_ENTRIES.equalsIgnoreCase(tokenType)) {
fBuildPathEntryCount = Integer.valueOf(toker.nextToken()).intValue();
}
}
if (libraryRecord != null && notifyOnRestoration) {
TaglibIndex.getInstance().addDelta(new TaglibIndexDelta(fProject, libraryRecord, ITaglibIndexDelta.ADDED));
}
}
restored = true;
} else {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Logger.log(Logger.INFO_DEBUG, "Tag Library Index: different cache format found, was \"" + lineText + "\", supports \"" + SAVE_FORMAT_VERSION + "\", reindexing build path");
}
}
if (_debugIndexTime)
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "time spent reloading " + fProject.getName() + " build path: " + (System.currentTimeMillis() - time0));
} catch (Exception e) {
restored = false;
if (_debugIndexTime)
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "failure reloading " + fProject.getName() + " build path index", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Logger.log(Logger.ERROR_DEBUG, null, e);
}
}
}
}
if (!restored) {
setBuildPathIsDirty();
}
}
}
Aggregations