use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager in project webtools.sourceediting by eclipse.
the class TaglibHelper method getTaglibVariables.
/**
* @param tagToAdd
* is the name of the tag whose variables we want
* @param structuredDoc
* is the IStructuredDocument where the tag is found
* @param customTag
* is the IStructuredDocumentRegion opening tag for the custom
* tag
* @param problems problems that are generated while creating variables are added to this collection
*/
public TaglibVariable[] getTaglibVariables(String tagToAdd, IStructuredDocument structuredDoc, ITextRegionCollection customTag, List problems) {
List results = new ArrayList();
if (problems == null)
problems = new ArrayList();
ModelQuery mq = getModelQuery(structuredDoc);
if (mq != null) {
TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(structuredDoc);
// mq).getTaglibSupport();
if (mgr == null)
return new TaglibVariable[0];
List trackers = mgr.getCMDocumentTrackers(-1);
Iterator taglibs = trackers.iterator();
// TaglibSupport support = ((TaglibModelQuery)
// mq).getTaglibSupport();
// if (support == null)
// return new TaglibVariable[0];
//
// Iterator taglibs =
// support.getCMDocuments(customTag.getStartOffset()).iterator();
CMDocument doc = null;
CMNamedNodeMap elements = null;
while (taglibs.hasNext()) {
doc = (CMDocument) taglibs.next();
CMNode node = null;
if ((elements = doc.getElements()) != null && (node = elements.getNamedItem(tagToAdd)) != null && node.getNodeType() == CMNode.ELEMENT_DECLARATION) {
if (node instanceof CMNodeWrapper) {
node = ((CMNodeWrapper) node).getOriginNode();
}
TLDElementDeclaration tldElementDecl = (TLDElementDeclaration) node;
/*
* Although clearly not the right place to add validation
* design-wise, this is the first time we have the
* necessary information to validate the tag class.
*/
boolean tagClassFound = validateTagClass(structuredDoc, customTag, tldElementDecl, problems);
// 1.2+ taglib style
addVariables(results, node, customTag);
// for 1.1 need more info from taglib tracker
if (tagClassFound && doc instanceof TaglibTracker) {
String uri = ((TaglibTracker) doc).getURI();
String prefix = ((TaglibTracker) doc).getPrefix();
// only for 1.1 taglibs
addTEIVariables(structuredDoc, customTag, results, tldElementDecl, prefix, uri, problems);
}
}
}
}
return (TaglibVariable[]) results.toArray(new TaglibVariable[results.size()]);
}
use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager 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.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager in project webtools.sourceediting by eclipse.
the class JSPContentAssistProcessor method addAttributeValueProposals.
/**
* add proposals for tags in attribute values
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
addTemplates(contentAssistRequest, TemplateContextTypeIdsJSP.ATTRIBUTE_VALUE);
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// add JSP extra proposals from JSPBeanInfoContentAssistProcessor
// JSPPropertyContentAssistProcessor
// 2.1
// get results from JSPUseBean and JSPProperty here
// (look up processor in a map based on node name)
JSPDummyContentAssistProcessor extraProcessor = (JSPDummyContentAssistProcessor) fNameToProcessorMap.get(node.getNodeName());
if (extraProcessor != null && contentAssistRequest != null) {
extraProcessor.addAttributeValueProposals(contentAssistRequest);
}
ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
if (mq != null) {
CMDocument doc = mq.getCorrespondingCMDocument(node);
// this shouldn't have to have the prefix coded in
if (// $NON-NLS-1$
doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:"))
return;
}
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0)
return;
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
break;
}
// on an empty value, add all the JSP and taglib tags
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
if (nameRegion != null && elementDecl != null) {
String attributeName = open.getText(nameRegion);
if (attributeName != null) {
String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
if (currentValue == null || currentValue.length() == 0) {
// $NON-NLS-1$
List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
for (i = 0; i < additionalElements.size(); i++) {
Object additionalElement = additionalElements.get(i);
if (additionalElement instanceof CMElementDeclaration) {
CMElementDeclaration ed = (CMElementDeclaration) additionalElement;
String tagname = getContentGenerator().getRequiredName(node, ed);
// $NON-NLS-1$
StringBuffer contents = new StringBuffer("\"");
getContentGenerator().generateTag(node, ed, contents);
// $NON-NLS-1$
contents.append('"');
CustomCompletionProposal proposal = new CustomCompletionProposal(contents.toString(), contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), contents.length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), tagname, null, null, XMLRelevanceConstants.R_JSP_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
}
} else if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
try {
// Create a new model for Content Assist to operate on. This
// will simulate
// a full Document and then adjust the offset numbers in the
// list of results.
IStructuredModel internalModel = null;
IModelManager mmanager = StructuredModelManager.getModelManager();
internalModel = mmanager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
IDOMNode xmlNode = null;
IDOMModel xmlOuterModel = null;
if (contentAssistRequest.getNode() instanceof IDOMNode) {
xmlNode = (IDOMNode) contentAssistRequest.getNode();
xmlOuterModel = xmlNode.getModel();
internalModel.setResolver(xmlOuterModel.getResolver());
internalModel.setBaseLocation(xmlOuterModel.getBaseLocation());
}
String contents = StringUtils.strip(contentAssistRequest.getText());
if (xmlNode != null && contents != null) {
int additionalShifts = 0;
// Be sure that custom tags from taglibs also show up
// by
// adding taglib declarations to the internal model.
TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(xmlOuterModel.getStructuredDocument());
if (mgr != null) {
List trackers = mgr.getCMDocumentTrackers(contentAssistRequest.getReplacementBeginPosition());
if (trackers != null) {
for (i = 0; i < trackers.size(); i++) {
CMDocumentTracker tracker = (CMDocumentTracker) trackers.get(i);
String declaration = tracker.getStructuredDocumentRegion().getText();
if (declaration != null) {
contents = declaration + contents;
additionalShifts += declaration.length();
}
}
}
}
// Also copy any jsp:useBean tags so that
// jsp:[gs]etProperty will function
Document doc = null;
if (contentAssistRequest.getNode().getNodeType() == Node.DOCUMENT_NODE)
doc = (Document) node;
else
doc = node.getOwnerDocument();
NodeList useBeans = doc.getElementsByTagName(JSP12Namespace.ElementName.USEBEAN);
for (int k = 0; k < useBeans.getLength(); k++) {
IDOMNode useBean = (IDOMNode) useBeans.item(k);
if (useBean.getStartOffset() < contentAssistRequest.getReplacementBeginPosition()) {
// $NON-NLS-1$
StringBuffer useBeanText = new StringBuffer("<jsp:useBean");
for (int j = 0; j < useBean.getAttributes().getLength(); j++) {
Attr attr = (Attr) useBean.getAttributes().item(j);
useBeanText.append(' ');
useBeanText.append(attr.getName());
// $NON-NLS-1$
useBeanText.append("=\"");
useBeanText.append(attr.getValue());
useBeanText.append('"');
}
// $NON-NLS-1$
useBeanText.append("/>");
additionalShifts += useBeanText.length();
contents = useBeanText.toString() + contents;
}
}
internalModel.getStructuredDocument().set(contents);
int internalOffset = 0;
boolean quoted = false;
// if quoted, use position inside and shift by one
if (contentAssistRequest.getMatchString().length() > 0 && (contentAssistRequest.getMatchString().charAt(0) == '\'' || contentAssistRequest.getMatchString().charAt(0) == '"')) {
internalOffset = contentAssistRequest.getMatchString().length() - 1 + additionalShifts;
quoted = true;
} else // if unquoted, use position inside
if (contentAssistRequest.getMatchString().length() > 0 && contentAssistRequest.getMatchString().charAt(0) == '<')
internalOffset = contentAssistRequest.getMatchString().length() + additionalShifts;
else
internalOffset = contentAssistRequest.getReplacementBeginPosition() - contentAssistRequest.getStartOffset() + additionalShifts;
depthCount++;
IndexedRegion internalNode = null;
int tmpOffset = internalOffset;
while (internalNode == null && tmpOffset >= 0) internalNode = internalModel.getIndexedRegion(tmpOffset--);
if (internalModel.getFactoryRegistry() != null) {
// set up the internal model
if (internalModel.getFactoryRegistry().getFactoryFor(PageDirectiveAdapter.class) == null) {
internalModel.getFactoryRegistry().addFactory(new PageDirectiveAdapterFactory());
}
PageDirectiveAdapter outerEmbeddedTypeAdapter = (PageDirectiveAdapter) xmlOuterModel.getDocument().getAdapterFor(PageDirectiveAdapter.class);
PageDirectiveAdapter internalEmbeddedTypeAdapter = (PageDirectiveAdapter) ((INodeNotifier) ((Node) internalNode).getOwnerDocument()).getAdapterFor(PageDirectiveAdapter.class);
internalEmbeddedTypeAdapter.setEmbeddedType(outerEmbeddedTypeAdapter.getEmbeddedType());
}
AdapterFactoryRegistry adapterRegistry = JSPUIPlugin.getDefault().getAdapterFactoryRegistry();
Iterator adapterList = adapterRegistry.getAdapterFactories();
// of content
while (adapterList.hasNext()) {
try {
AdapterFactoryProvider provider = (AdapterFactoryProvider) adapterList.next();
if (provider.isFor(internalModel.getModelHandler())) {
provider.addAdapterFactories(internalModel);
}
} catch (Exception e) {
Logger.logException(e);
}
}
/**
* the internal adapter does all the real work of using
* the JSP content model to form proposals
*/
ICompletionProposal[] results = null;
depthCount--;
if (results != null) {
for (i = 0; i < results.length; i++) {
contentAssistRequest.addProposal(new CustomCompletionProposal(((CustomCompletionProposal) results[i]).getReplacementString(), ((CustomCompletionProposal) results[i]).getReplacementOffset() - additionalShifts + contentAssistRequest.getStartOffset() + (quoted ? 1 : 0), ((CustomCompletionProposal) results[i]).getReplacementLength(), ((CustomCompletionProposal) results[i]).getCursorPosition(), results[i].getImage(), results[i].getDisplayString(), ((CustomCompletionProposal) results[i]).getContextInformation(), ((CustomCompletionProposal) results[i]).getAdditionalProposalInfo(), (results[i] instanceof IRelevanceCompletionProposal) ? ((IRelevanceCompletionProposal) results[i]).getRelevance() : IRelevanceConstants.R_NONE));
}
}
}
} catch (Exception e) {
// $NON-NLS-1$
Logger.logException("Error in embedded JSP Content Assist", e);
}
}
}
use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager in project webtools.sourceediting by eclipse.
the class JSPModelQueryCMProvider method getCorrespondingCMDocument.
/**
* Returns the CMDocument that corresponds to the DOM Node. or null if no
* CMDocument is appropriate for the DOM Node.
*/
public CMDocument getCorrespondingCMDocument(Node node) {
CMDocument jcmdoc = null;
if (node instanceof IDOMNode) {
IDOMModel model = ((IDOMNode) node).getModel();
String modelPath = model.getBaseLocation();
if (modelPath != null && !IModelManager.UNMANAGED_MODEL.equals(modelPath)) {
float version = DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(new Path(modelPath));
jcmdoc = JSPCMDocumentFactory.getCMDocument(version);
}
}
if (jcmdoc == null) {
jcmdoc = JSPCMDocumentFactory.getCMDocument();
}
CMDocument result = null;
try {
if (node.getNodeType() == Node.ELEMENT_NODE) {
String elementName = node.getNodeName();
// test to see if this node belongs to JSP's CMDocument (case
// sensitive)
CMElementDeclaration dec = (CMElementDeclaration) jcmdoc.getElements().getNamedItem(elementName);
if (dec != null) {
result = jcmdoc;
}
}
String prefix = node.getPrefix();
if (result == null && prefix != null && prefix.length() > 0 && node instanceof IDOMNode) {
// check position dependent
IDOMNode xmlNode = (IDOMNode) node;
TLDCMDocumentManager tldmgr = TaglibController.getTLDCMDocumentManager(xmlNode.getStructuredDocument());
if (tldmgr != null) {
List documents = tldmgr.getCMDocumentTrackers(node.getPrefix(), xmlNode.getStartOffset());
// there shouldn't be more than one cmdocument returned
if (documents != null && documents.size() > 0)
result = (CMDocument) documents.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager in project webtools.sourceediting by eclipse.
the class TagModelQueryCMProvider method getCorrespondingCMDocument.
/**
* Returns the CMDocument that corresponds to the DOM Node. or null if no
* CMDocument is appropriate for the DOM Node.
*/
public CMDocument getCorrespondingCMDocument(Node node) {
CMDocument tagdoc = null;
if (node instanceof IDOMNode) {
IDOMModel model = ((IDOMNode) node).getModel();
String modelPath = model.getBaseLocation();
if (modelPath != null && !IModelManager.UNMANAGED_MODEL.equals(modelPath)) {
float version = DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(new Path(modelPath));
tagdoc = TAGCMDocumentFactory.getCMDocument(version);
}
}
CMDocument result = null;
try {
if (node.getNodeType() == Node.ELEMENT_NODE && tagdoc != null) {
String elementName = node.getNodeName();
// test to see if this node belongs to JSP's CMDocument (case
// sensitive)
CMElementDeclaration dec = (CMElementDeclaration) tagdoc.getElements().getNamedItem(elementName);
if (dec != null) {
result = tagdoc;
}
}
String prefix = node.getPrefix();
if (result == null && prefix != null && prefix.length() > 0 && node instanceof IDOMNode) {
// check position dependent
IDOMNode xmlNode = (IDOMNode) node;
TLDCMDocumentManager tldmgr = TaglibController.getTLDCMDocumentManager(xmlNode.getStructuredDocument());
if (tldmgr != null) {
List documents = tldmgr.getCMDocumentTrackers(node.getPrefix(), xmlNode.getStartOffset());
// there shouldn't be more than one cmdocument returned
if (documents != null && !documents.isEmpty())
result = (CMDocument) documents.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// }
return result;
}
Aggregations