use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class HTMLTagInfoTest method getCMElementDeclaration.
/**
* Retreives CMElementDeclaration for given node
*
* @return CMElementDeclaration - CMElementDeclaration of node or
* <code>null</code> if not possible
*/
private CMElementDeclaration getCMElementDeclaration(Element element) {
CMElementDeclaration result = null;
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
if (modelQuery != null)
result = modelQuery.getCMElementDeclaration(element);
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class CommentElementHandlerForSSI method createElement.
public Element createElement(Document document, String data, boolean isJSPTag) {
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
if (modelQuery == null) {
return null;
}
CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
if (cm == null) {
return null;
}
CMNamedNodeMap map = cm.getElements();
if (map == null) {
return null;
}
TagScanner scanner = new TagScanner(data, 1);
String name = scanner.nextName();
if (name == null) {
return null;
}
StringBuffer buffer = new StringBuffer(name.length() + 4);
buffer.append(SSI_PREFIX);
buffer.append(':');
buffer.append(name);
String tagName = buffer.toString();
// check if valid (defined) SSI tag or not
if (map.getNamedItem(tagName) == null) {
return null;
}
CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
Element element = factory.create(tagName, CommentElementFactory.IS_START);
// set attributes
String attrName = scanner.nextName();
while (attrName != null) {
String attrValue = scanner.nextValue();
Attr attr = document.createAttribute(attrName);
if (attr != null) {
if (attrValue != null)
attr.setValue(attrValue);
element.setAttributeNode(attr);
}
attrName = scanner.nextName();
}
return element;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method isEmptyElement.
private boolean isEmptyElement(IDOMElement element) {
Document document = element.getOwnerDocument();
if (document == null)
// undefined tag, return default
return false;
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
if (modelQuery == null)
// undefined tag, return default
return false;
CMElementDeclaration decl = modelQuery.getCMElementDeclaration(element);
if (decl == null)
// undefined tag, return default
return false;
return (decl.getContentType() == CMElementDeclaration.EMPTY);
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class LibraryTagsCompletionProposalComputer method addAttributeValueProposals.
/**
* @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
if (!this.isXHTML) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
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 (doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:")) {
// $NON-NLS-1$
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 = AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(node);
if (nameRegion != null && elementDecl != null) {
String attributeName = open.getText(nameRegion);
if (attributeName != null) {
Node parent = contentAssistRequest.getParent();
// ignore start quote in match string
String matchString = contentAssistRequest.getMatchString().trim();
if (matchString.startsWith("'") || matchString.startsWith("\"")) {
// $NON-NLS-1$ //$NON-NLS-2$
matchString = matchString.substring(1);
}
// get all the proposals
List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ALL);
Iterator nodeIterator = additionalElements.iterator();
// check each suggestion
while (nodeIterator.hasNext()) {
CMNode additionalElementDecl = (CMNode) nodeIterator.next();
if (additionalElementDecl != null && additionalElementDecl instanceof CMElementDeclaration && validModelQueryNode(additionalElementDecl)) {
CMElementDeclaration ed = (CMElementDeclaration) additionalElementDecl;
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
StringBuffer sb = new StringBuffer();
getContentGenerator().generateTag(parent, ed, sb);
String proposedText = sb.toString();
// filter out any proposals that dont match matchString
if (beginsWith(proposedText, matchString)) {
// wrap with ' because JSP attributes are warped with "
// $NON-NLS-1$
proposedText = "'" + proposedText;
// don't want to risk injecting an extra
if (!(contentAssistRequest.getRegion() instanceof ITextRegionContainer)) {
// $NON-NLS-1$
proposedText += "'";
}
// get the image
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
image = this.getGenericTagImage();
}
// create the proposal
int cursorAdjustment = getCursorPositionForProposedText(proposedText);
String proposedInfo = AbstractXMLModelQueryCompletionProposalComputer.getAdditionalInfo(AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(parent), elementDecl);
String tagname = getContentGenerator().getRequiredName(node, ed);
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class HTMLTagsCompletionProposalComputer method addHTMLTagProposal.
/**
* <p>adds HTML tag proposal for empty document</p>
*
* @param contentAssistRequest request to add proposal too
* @param context context of the completion request
*/
private void addHTMLTagProposal(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
IStructuredModel model = null;
try {
if (context.getDocument() instanceof IStructuredDocument) {
model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) context.getDocument());
}
if (model != null) {
IDOMDocument doc = ((IDOMModel) model).getDocument();
ModelQuery mq = ModelQueryUtil.getModelQuery(doc);
if (mq != null) {
// XHTML requires lowercase tagname for lookup
CMDocument correspondingCMDocument = mq.getCorrespondingCMDocument(doc);
if (correspondingCMDocument != null) {
CMElementDeclaration htmlDecl = (CMElementDeclaration) correspondingCMDocument.getElements().getNamedItem(HTML40Namespace.ElementName.HTML.toLowerCase());
if (htmlDecl != null) {
StringBuffer proposedTextBuffer = new StringBuffer();
getContentGenerator().generateTag(doc, htmlDecl, proposedTextBuffer);
String proposedText = proposedTextBuffer.toString();
String requiredName = getContentGenerator().getRequiredName(doc, htmlDecl);
IStructuredDocumentRegion region = contentAssistRequest.getDocumentRegion();
if (region != null) {
if (region.getFirstRegion() != null && region.getFirstRegion().getType().equals(DOMRegionContext.XML_TAG_OPEN)) {
// in order to differentiate between content assist on
// completely empty document and the one with xml open tag
proposedText = proposedText.substring(1);
}
}
if (!beginsWith(proposedText, contentAssistRequest.getMatchString())) {
return;
}
int cursorAdjustment = getCursorPositionForProposedText(proposedText);
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, HTMLEditorPluginImageHelper.getInstance().getImage(HTMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), requiredName, null, null, XMLRelevanceConstants.R_TAG_NAME);
contentAssistRequest.addProposal(proposal);
}
}
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
Aggregations