use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class TaglibHelper method getModelQuery.
/**
* @return Returns the fModelQuery.
*/
public ModelQuery getModelQuery(IDocument doc) {
IStructuredModel model = null;
ModelQuery mq = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
mq = ModelQueryUtil.getModelQuery(model);
} finally {
if (model != null)
model.releaseFromRead();
}
return mq;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class TaglibHyperlinkDetector method detectHyperlinks.
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
IHyperlink hyperlink = null;
if (textViewer != null && region != null) {
IDocument doc = textViewer.getDocument();
if (doc != null) {
try {
// check if jsp tag/directive first
ITypedRegion partition = TextUtilities.getPartition(doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, region.getOffset(), false);
if (partition != null && partition.getType() == IJSPPartitions.JSP_DIRECTIVE) {
IStructuredModel sModel = null;
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
// check if jsp taglib directive
Node currentNode = getCurrentNode(sModel, region.getOffset());
if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
String baseLocationForTaglib = getBaseLocationForTaglib(doc);
if (baseLocationForTaglib != null && JSP11Namespace.ElementName.DIRECTIVE_TAGLIB.equalsIgnoreCase(currentNode.getNodeName())) {
/**
* The taglib directive itself
*/
// get the uri attribute
Attr taglibURINode = ((Element) currentNode).getAttributeNode(JSP11Namespace.ATTR_NAME_URI);
if (taglibURINode != null) {
ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, taglibURINode.getValue(), false);
// there's nothing to link to
if (reference != null) {
// handle taglibs
switch(reference.getRecordType()) {
case (ITaglibRecord.TLD):
{
ITLDRecord record = (ITLDRecord) reference;
String uriString = record.getPath().toString();
IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
if (hyperlinkRegion != null) {
hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
}
}
break;
case (ITaglibRecord.JAR):
case (ITaglibRecord.URL):
{
IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
if (hyperlinkRegion != null) {
hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
}
}
}
}
}
} else if (baseLocationForTaglib != null && JSP12Namespace.ElementName.ROOT.equalsIgnoreCase(currentNode.getNodeName())) {
/**
* The jsp:root element
*/
NamedNodeMap attrs = currentNode.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = (Attr) attrs.item(i);
if (attr.getNodeName().startsWith(XMLNS)) {
String uri = StringUtils.strip(attr.getNodeValue());
if (uri.startsWith(URN_TLD)) {
uri = uri.substring(URN_TLD.length());
}
ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, uri, false);
// there's nothing to link to
if (reference != null) {
// handle taglibs
switch(reference.getRecordType()) {
case (ITaglibRecord.TLD):
{
ITLDRecord record = (ITLDRecord) reference;
String uriString = record.getPath().toString();
IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
if (hyperlinkRegion != null) {
hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
}
}
break;
case (ITaglibRecord.JAR):
case (ITaglibRecord.URL):
{
IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
if (hyperlinkRegion != null) {
hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
}
}
}
}
}
}
} else {
/**
* Hyperlink custom tag to its TLD or tag file
*/
TLDCMDocumentManager documentManager = TaglibController.getTLDCMDocumentManager(doc);
if (documentManager != null) {
List documentTrackers = documentManager.getCMDocumentTrackers(currentNode.getPrefix(), region.getOffset());
for (int i = 0; i < documentTrackers.size(); i++) {
TaglibTracker tracker = (TaglibTracker) documentTrackers.get(i);
CMElementDeclaration decl = (CMElementDeclaration) tracker.getElements().getNamedItem(currentNode.getNodeName());
if (decl != null) {
decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
if (decl instanceof CMElementDeclarationImpl) {
String base = ((CMElementDeclarationImpl) decl).getLocationString();
IRegion hyperlinkRegion = getHyperlinkRegion(currentNode, region);
if (hyperlinkRegion != null) {
hyperlink = createHyperlink(base, hyperlinkRegion, doc, currentNode);
}
}
}
}
}
}
}
} finally {
if (sModel != null)
sModel.releaseFromRead();
}
}
} catch (BadLocationException e) {
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
}
}
}
if (hyperlink != null)
return new IHyperlink[] { hyperlink };
return null;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class StructuredAutoEditStrategyJSPJava method customizeDocumentCommand.
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
if (!supportsSmartInsert(document)) {
return;
}
IStructuredModel model = null;
try {
// Auto edit for JSP Comments
if ("-".equals(command.text) && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_COMMENTS)) {
// $NON-NLS-1$
if (prefixedWith(document, command.offset, "<%-")) {
// $NON-NLS-1$
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
IDOMNode parent = (node != null) ? (IDOMNode) node.getParentNode() : null;
// Parent is the scriptlet tag
if (parent != null && JSP11Namespace.ElementName.SCRIPTLET.equals(parent.getNodeName()) && !parent.getSource().endsWith("--%>")) {
// $NON-NLS-1$
IStructuredDocumentRegion end = parent.getEndStructuredDocumentRegion();
if (end != null) {
try {
// $NON-NLS-1$
document.replace(end.getStartOffset(), 0, "--");
} catch (BadLocationException e) {
Logger.logException(e);
}
}
}
}
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class JSPSearchDocument method getJSPTranslation.
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the
* JSPSearchDocument, which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an
* unsupported file.
*/
public final JSPTranslationExtension getJSPTranslation() {
JSPTranslationExtension translation = null;
IFile jspFile = getFile();
if (!JSPSearchSupport.isJsp(jspFile))
return translation;
IStructuredModel model = null;
try {
// get existing model for read, then get document from it
IModelManager modelManager = getModelManager();
if (modelManager != null) {
jspFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
model = modelManager.getModelForRead(jspFile);
}
// handle unsupported
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
setupAdapterFactory(xmlModel);
IDOMDocument doc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
translation = adapter.getJSPTranslation();
}
} catch (IOException e) {
Logger.logException(e);
} catch (CoreException e) {
Logger.logException(e);
} catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
// Logger.logException(e);
} finally {
if (model != null)
model.releaseFromRead();
}
return translation;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class JSDTHyperlinkDetector method createHyperlink.
private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
IHyperlink link = null;
if (region != null) {
// open local variable in the JSP file...
if (element instanceof ISourceReference) {
IFile file = null;
IPath outsidePath = null;
int jspOffset = 0;
IStructuredModel sModel = null;
// try to locate the file in the workspace
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (sModel != null) {
// URIResolver resolver = sModel.getResolver();
// if (resolver != null) {
// String uriString = resolver.getFileBaseLocation();
String uriString = sModel.getBaseLocation();
file = getFile(uriString);
// }
}
} finally {
if (sModel != null) {
sModel.releaseFromRead();
}
}
// get Java range, translate coordinate to JSP
try {
ISourceRange range = null;
if (jsTranslation != null) {
// link to local variable definitions
if (element instanceof ILocalVariable) {
range = ((ILocalVariable) element).getNameRange();
IJavaScriptElement unit = ((ILocalVariable) element).getParent();
IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
unit = ((JavaElement) unit).getParent();
}
if (unit instanceof IJavaScriptUnit) {
IJavaScriptUnit cu = (IJavaScriptUnit) unit;
if (cu != myUnit) {
file = getFile(cu.getPath().toString());
if (file == null) {
outsidePath = cu.getPath();
}
}
} else if (unit instanceof IClassFile) {
IClassFile cu = (IClassFile) unit;
if (cu != myUnit) {
file = getFile(cu.getPath().toString());
if (file == null) {
outsidePath = cu.getPath();
}
}
}
} else // linking to fields of the same compilation unit
if (element.getElementType() == IJavaScriptElement.FIELD) {
Object cu = ((IField) element).getJavaScriptUnit();
if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
}
} else // linking to methods of the same compilation unit
if (element.getElementType() == IJavaScriptElement.METHOD) {
Object cu = ((IFunction) element).getJavaScriptUnit();
if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
}
}
}
if (range != null && file != null) {
jspOffset = range.getOffset();
if (jspOffset >= 0) {
link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
}
} else if (range != null && outsidePath != null) {
jspOffset = range.getOffset();
if (jspOffset >= 0) {
link = new ExternalFileHyperlink(region, outsidePath.toFile());
}
}
} catch (JavaScriptModelException jme) {
Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
}
}
if (link == null) {
link = new JSDTHyperlink(region, element);
}
}
return link;
}
Aggregations