use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection in project webtools.sourceediting by eclipse.
the class StructuredTextPartitioner method getDeepRegionAtCharacterOffset.
/**
* <p>Unlike {@link IStructuredDocumentRegion#getRegionAtCharacterOffset(int)} this will dig
* into <code>ITextRegionCollection</code> to find the region containing the given offset</p>
*
* @param region the containing region of the given <code>offset</code>
* @param offset to the overall offset in the document.
* @return the <code>ITextRegion</code> containing the given <code>offset</code>, will never be
* a <code>ITextRegionCollextion</code>
*/
private DeepRegion getDeepRegionAtCharacterOffset(IStructuredDocumentRegion region, int offset) {
ITextRegion text = region.getRegionAtCharacterOffset(offset);
int end = region.getStartOffset();
if (text != null)
end += text.getStart();
while (text instanceof ITextRegionCollection) {
text = ((ITextRegionCollection) text).getRegionAtCharacterOffset(offset);
end += text.getStart();
}
if (text != null)
end += text.getLength();
return new DeepRegion(text, end);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection in project webtools.sourceediting by eclipse.
the class JSPTranslator method translate.
/**
* the main control loop for translating the document, driven by the
* structuredDocument nodes
*/
public void translate() {
if (fTagToVariableMap == null) {
fTagToVariableMap = new StackMap();
}
if (fAtBeginVariableMap == null) {
fAtBeginVariableMap = new HashMap();
}
fAtBeginScopeStack.clear();
// need to existing scope for top level customtags
fAtBeginScopeStack.push("__root__");
fTranslationProblems.clear();
setCurrentNode(new ZeroStructuredDocumentRegion(fStructuredDocument, 0));
translatePreludes();
setCurrentNode(fStructuredDocument.getFirstStructuredDocumentRegion());
while (getCurrentNode() != null && !isCanceled()) {
// no code has been translated for this region yet
fCodeTranslated = false;
// basically this part will handle any "embedded" JSP containers
if (getCurrentNode().getType() == DOMRegionContext.XML_COMMENT_TEXT || getCurrentNode().getType() == DOMRegionContext.XML_CDATA_TEXT || getCurrentNode().getType() == DOMRegionContext.UNDEFINED) {
translateXMLCommentNode(getCurrentNode());
} else {
// iterate through each region in the flat node
translateRegionContainer(getCurrentNode(), STANDARD_JSP);
}
// if no code was translated for this region then found "non translated code"
if (!fCodeTranslated) {
fFoundNonTranslatedCode = true;
}
if (getCurrentNode() != null)
advanceNextNode();
}
writePlaceHolderForNonTranslatedCode();
setCurrentNode(new ZeroStructuredDocumentRegion(fStructuredDocument, fStructuredDocument.getLength()));
translateCodas();
/*
* Any contents left in the map indicate start tags that never had end
* tags. While the '{' that is present without the matching '}' should
* cause a Java translation fault, that's not particularly helpful to
* a user who may only know how to use custom tags as tags. Ultimately
* unbalanced custom tags should just be reported as unbalanced tags,
* and unbalanced '{'/'}' only reported when the user actually
* unbalanced them with scriptlets.
*/
Iterator regionAndTaglibVariables = fTagToVariableMap.values().iterator();
while (regionAndTaglibVariables.hasNext()) {
RegionTags regionTag = (RegionTags) regionAndTaglibVariables.next();
ITextRegionCollection extraStartRegion = regionTag.region;
IJSPProblem missingEndTag = createJSPProblem(IJSPProblem.EndCustomTagMissing, IJSPProblem.F_PROBLEM_ID_LITERAL, NLS.bind(JSPCoreMessages.JSPTranslator_5, regionTag.tag.getTagName()), extraStartRegion.getStartOffset(), extraStartRegion.getEndOffset());
fTranslationProblems.add(missingEndTag);
StringBuffer text = new StringBuffer();
// Account for iteration tags that have a missing end tag
if (regionTag.tag.isIterationTag())
doAfterBody(text, regionTag);
// $NON-NLS-1$
text.append("} // [</");
text.append(regionTag.tag.getTagName());
// $NON-NLS-1$
text.append(">]");
appendToBuffer(text.toString(), fUserCode, false, fStructuredDocument.getLastStructuredDocumentRegion());
}
fTagToVariableMap.clear();
fAtBeginVariableMap.clear();
/*
* Now do the same for jsp:useBean tags, whose contents get their own
* { & }
*/
while (!fUseBeansStack.isEmpty()) {
// $NON-NLS-1$
appendToBuffer("}", fUserCode, false, fStructuredDocument.getLastStructuredDocumentRegion());
ITextRegionCollection extraStartRegion = (ITextRegionCollection) fUseBeansStack.pop();
IJSPProblem missingEndTag = createJSPProblem(IJSPProblem.UseBeanEndTagMissing, IJSPProblem.F_PROBLEM_ID_LITERAL, NLS.bind(JSPCoreMessages.JSPTranslator_5, JSP11Namespace.ElementName.USEBEAN), extraStartRegion.getStartOffset(), extraStartRegion.getEndOffset());
fTranslationProblems.add(missingEndTag);
}
buildResult(true);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection in project webtools.sourceediting by eclipse.
the class JSPTranslationExtension method checkForELRegion.
private boolean checkForELRegion(IStructuredDocumentRegion container) {
Iterator regions = container.getRegions().iterator();
ITextRegion region = null;
while (regions.hasNext()) {
region = (ITextRegion) regions.next();
if (region instanceof ITextRegionCollection) {
ITextRegionCollection parentRegion = ((ITextRegionCollection) region);
Iterator childRegions = parentRegion.getRegions().iterator();
while (childRegions.hasNext()) {
ITextRegion childRegion = (ITextRegion) childRegions.next();
if (childRegion.getType() == DOMJSPRegionContexts.JSP_EL_OPEN)
return true;
}
}
}
return false;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection in project webtools.sourceediting by eclipse.
the class JSPTranslator method translateUseBean.
//
// <jsp:useBean>
protected void translateUseBean(ITextRegionCollection container) {
ITextRegion r = null;
String attrName = null;
String attrValue = null;
String id = null;
ITextRegion idRegion = null;
String type = null;
ITextRegion typeRegion = null;
String className = null;
ITextRegion classnameRegion = null;
String beanName = null;
ITextRegion beanNameRegion = null;
if (DOMRegionContext.XML_END_TAG_OPEN.equals(container.getFirstRegion().getType())) {
if (!fUseBeansStack.isEmpty()) {
fUseBeansStack.pop();
// $NON-NLS-1$
appendToBuffer("}", fUserCode, false, fCurrentNode);
} else {
// no useBean start tag being remembered
ITextRegionCollection extraEndRegion = container;
IJSPProblem missingStartTag = createJSPProblem(IJSPProblem.UseBeanStartTagMissing, IJSPProblem.F_PROBLEM_ID_LITERAL, NLS.bind(JSPCoreMessages.JSPTranslator_4, JSP11Namespace.ElementName.USEBEAN), extraEndRegion.getStartOffset(), extraEndRegion.getEndOffset());
fTranslationProblems.add(missingStartTag);
}
return;
}
Iterator regions = container.getRegions().iterator();
while (regions.hasNext() && (r = (ITextRegion) regions.next()) != null && (r.getType() != DOMRegionContext.XML_TAG_CLOSE || r.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE)) {
attrName = attrValue = null;
if (r.getType().equals(DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)) {
attrName = container.getText(r).trim();
if (regions.hasNext() && (r = (ITextRegion) regions.next()) != null && r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
if (regions.hasNext() && (r = (ITextRegion) regions.next()) != null && r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
attrValue = StringUtils.stripQuotes(container.getText(r));
}
// has equals, but no value?
}
// an attribute with no equals?
}
// (pa) might need different logic here if we wanna support more
if (attrName != null && attrValue != null) {
if (attrName.equals("id")) {
// $NON-NLS-1$
id = attrValue;
idRegion = r;
} else if (attrName.equals("class")) {
// $NON-NLS-1$
className = attrValue;
classnameRegion = r;
} else if (attrName.equals("type")) {
// $NON-NLS-1$
type = attrValue;
typeRegion = r;
} else if (attrName.equals("beanName")) {
// $NON-NLS-1$
beanName = attrValue;
beanNameRegion = r;
}
}
}
if (id != null) {
// The id is not a valid Java identifier
if (!isValidJavaIdentifier(id) && idRegion != null) {
Object problem = createJSPProblem(IJSPProblem.UseBeanInvalidID, IProblem.ParsingErrorInvalidToken, MessageFormat.format(JSPCoreMessages.JSPTranslator_0, new String[] { id }), container.getStartOffset(idRegion), container.getTextEndOffset(idRegion) - 1);
fTranslationProblems.add(problem);
}
// No Type information is provided
if (((type == null && className == null) || (type == null && beanName != null)) && idRegion != null) {
Object problem = createJSPProblem(IJSPProblem.UseBeanMissingTypeInfo, IProblem.UndefinedType, NLS.bind(JSPCoreMessages.JSPTranslator_3, new String[] { id }), container.getStartOffset(idRegion), container.getTextEndOffset(idRegion) - 1);
fTranslationProblems.add(problem);
}
// Cannot specify both a class and a beanName
if (className != null && beanName != null && beanNameRegion != null) {
ITextRegion nameRegion = container.getRegions().get(1);
Object problem = createJSPProblem(IJSPProblem.UseBeanAmbiguousType, IProblem.AmbiguousType, JSPCoreMessages.JSPTranslator_2, container.getStartOffset(nameRegion), container.getTextEndOffset(nameRegion) - 1);
fTranslationProblems.add(problem);
}
// Type id = null; // if there is no classname or beanname
if ((type != null || className != null)) {
if (className != null)
className = decodeType(className);
if (type == null) {
type = className;
typeRegion = classnameRegion;
} else
type = decodeType(type);
/* Now check the types (multiple of generics may be involved) */
List errorTypeNames = new ArrayList(2);
if (!isTypeFound(type, errorTypeNames)) {
for (int i = 0; i < errorTypeNames.size(); i++) {
Object problem = createJSPProblem(IJSPProblem.F_PROBLEM_ID_LITERAL, IProblem.UndefinedType, MessageFormat.format(JSPCoreMessages.JSPTranslator_1, new String[] { errorTypeNames.get(i).toString() }), container.getStartOffset(typeRegion), container.getTextEndOffset(typeRegion) - 1);
fTranslationProblems.add(problem);
}
} else {
// $NON-NLS-1$ //$NON-NLS-2$
String prefix = type + " " + id + " = ";
// $NON-NLS-1$
String suffix = "null;" + ENDL;
if (className != null)
// $NON-NLS-1$ //$NON-NLS-2$
suffix = "new " + className + "();" + ENDL;
else if (beanName != null)
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
suffix = "(" + type + ") java.beans.Beans.instantiate(getClass().getClassLoader(), \"" + beanName + "\");" + ENDL;
appendToBuffer(prefix + suffix, fUserCode, true, fCurrentNode);
}
}
}
/*
* Add a brace and remember the start tag regardless of whether a
* variable was correctly created
*/
if (!DOMRegionContext.XML_EMPTY_TAG_CLOSE.equals(container.getLastRegion().getType())) {
fUseBeansStack.push(container);
// $NON-NLS-1$
appendToBuffer("{", fUserCode, false, fCurrentNode);
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection in project webtools.sourceediting by eclipse.
the class LineStyleProviderForXSL method prepareTextRegions.
protected boolean prepareTextRegions(IStructuredDocumentRegion structuredDocumentRegion, int partitionStartOffset, int partitionLength, Collection holdResults) {
boolean handled = false;
final int partitionEndOffset = partitionStartOffset + partitionLength - 1;
while (structuredDocumentRegion != null && structuredDocumentRegion.getStartOffset() <= partitionEndOffset) {
ITextRegion region = null;
ITextRegionList regions = structuredDocumentRegion.getRegions();
StyleRange styleRange = null;
for (int i = 0; i < regions.size(); i++) {
region = regions.get(i);
TextAttribute attr = null;
TextAttribute previousAttr = null;
if (structuredDocumentRegion.getStartOffset(region) > partitionEndOffset)
break;
if (structuredDocumentRegion.getEndOffset(region) <= partitionStartOffset)
continue;
if (region instanceof ITextRegionCollection) {
boolean handledCollection = (prepareTextRegion((ITextRegionCollection) region, partitionStartOffset, partitionLength, holdResults));
handled = (!handled) ? handledCollection : handled;
} else {
attr = getAttributeFor(structuredDocumentRegion, region);
if (attr == null) {
previousAttr = null;
} else {
handled = true;
styleRange = applyStyleRange(structuredDocumentRegion, partitionStartOffset, partitionLength, holdResults, region, styleRange, attr, previousAttr);
}
}
}
structuredDocumentRegion = structuredDocumentRegion.getNext();
}
return handled;
}
Aggregations