use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class LibraryTagsCompletionProposalComputer method forciblyGetTagLibAndJSPElements.
/**
* <p><b>NOTE: </b>This should be removed as soon as Bug 311961 is fixed</p>
* <p>This is bad because it does not use the ModelQuery framework, it
* access the TLDCMDocumentManager directly</p>
* <p>This is essentially a combination of the {@link TaglibModelQueryExtension} and
* the {@link JSPModelQueryExtension} but it means any other extensions get left
* out when creating content assist suggestion at the document root level</p>
*
* @param elementDecls
* @param node
* @param childIndex
* @return
*/
private List forciblyGetTagLibAndJSPElements(List elementDecls, Node node, int childIndex) {
if (node instanceof IDOMNode) {
/*
* find the location of the intended insertion as it will give us
* the correct offset for checking position dependent CMDocuments
*/
int textInsertionOffset = 0;
NodeList children = node.getChildNodes();
if (children.getLength() >= childIndex && childIndex >= 0) {
Node nodeAlreadyAtIndex = children.item(childIndex);
if (nodeAlreadyAtIndex instanceof IDOMNode)
textInsertionOffset = ((IDOMNode) nodeAlreadyAtIndex).getEndOffset();
} else {
textInsertionOffset = ((IDOMNode) node).getStartOffset();
}
TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(((IDOMNode) node).getStructuredDocument());
if (mgr != null) {
List moreCMDocuments = mgr.getCMDocumentTrackers(textInsertionOffset);
if (moreCMDocuments != null) {
for (int i = 0; i < moreCMDocuments.size(); i++) {
CMDocument doc = (CMDocument) moreCMDocuments.get(i);
CMNamedNodeMap elements = doc.getElements();
if (elements != null) {
for (int j = 0; j < elements.getLength(); j++) {
CMElementDeclaration ed = (CMElementDeclaration) elements.item(j);
elementDecls.add(ed);
}
}
}
}
}
// get position dependent CMDocuments and insert their tags as
// proposals
ModelQueryAdapter mqAdapter = null;
if (node.getNodeType() == Node.DOCUMENT_NODE)
mqAdapter = (ModelQueryAdapter) ((IDOMNode) node).getAdapterFor(ModelQueryAdapter.class);
else
mqAdapter = (ModelQueryAdapter) ((IDOMNode) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
if (mqAdapter != null) {
CMDocument doc = mqAdapter.getModelQuery().getCorrespondingCMDocument(node);
if (doc != null) {
CMDocument jcmdoc = getDefaultJSPCMDocument((IDOMNode) node);
CMNamedNodeMap jspelements = jcmdoc.getElements();
/*
* For a built-in JSP action the content model is properly
* set up, so don't just blindly add the rest--unless this
* will be a direct child of the document
*/
if (jspelements != null && (!(doc instanceof JSPCMDocument) || node.getNodeType() == Node.DOCUMENT_NODE)) {
List rejectElements = new ArrayList();
// determine if the document is in XML form
Document domDoc = null;
if (node.getNodeType() == Node.DOCUMENT_NODE)
domDoc = (Document) node;
else
domDoc = node.getOwnerDocument();
// Show XML tag forms of JSP markers if jsp:root is
// the document element OR it's HTML but
// isn't really in the text.
// If the document isn't strictly XML, pull out the
// XML tag forms it is xml format
rejectElements.add(JSP12Namespace.ElementName.SCRIPTLET);
rejectElements.add(JSP12Namespace.ElementName.EXPRESSION);
rejectElements.add(JSP12Namespace.ElementName.DECLARATION);
rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_INCLUDE);
rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_PAGE);
rejectElements.add(JSP12Namespace.ElementName.TEXT);
rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_TAGLIB);
rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_TAG);
rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_ATTRIBUTE);
rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_VARIABLE);
if (isXMLFormat(domDoc)) {
// jsp actions
rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
rejectElements.add(JSP12Namespace.ElementName.USEBEAN);
rejectElements.add(JSP12Namespace.ElementName.GETPROPERTY);
rejectElements.add(JSP12Namespace.ElementName.SETPROPERTY);
rejectElements.add(JSP12Namespace.ElementName.INCLUDE);
rejectElements.add(JSP12Namespace.ElementName.FORWARD);
rejectElements.add(JSP12Namespace.ElementName.PLUGIN);
rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
rejectElements.add(JSP12Namespace.ElementName.PARAM);
rejectElements.add(JSP12Namespace.ElementName.PARAMS);
}
// don't show jsp:root if a document element already
// exists
Element docElement = domDoc.getDocumentElement();
if (// $NON-NLS-1$
docElement != null && ((docElement.getNodeName().equals("jsp:root")) || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() != null || ((IDOMNode) docElement).getEndStructuredDocumentRegion() != null))))
rejectElements.add(JSP12Namespace.ElementName.ROOT);
for (int j = 0; j < jspelements.getLength(); j++) {
CMElementDeclaration ed = (CMElementDeclaration) jspelements.item(j);
if (rejectElements.contains(ed.getNodeName()))
continue;
elementDecls.add(ed);
}
}
} else // No cm document (such as for the Document (a non-Element) node itself)
{
CMNamedNodeMap jspElements = getDefaultJSPCMDocument((IDOMNode) node).getElements();
int length = jspElements.getLength();
for (int i = 0; i < length; i++) {
elementDecls.add(jspElements.item(i));
}
}
}
}
return elementDecls;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class HTMLTagsCompletionProposalComputer method isXHTMLNode.
/**
* Determine if this Document is an XHTML Document. Operates solely off of
* the Document Type declaration
*/
private static boolean isXHTMLNode(Node node) {
if (node == null) {
return false;
}
Document doc = null;
if (node.getNodeType() != Node.DOCUMENT_NODE)
doc = node.getOwnerDocument();
else
doc = ((Document) node);
if (doc instanceof IDOMDocument) {
return ((IDOMDocument) doc).isXMLType();
}
if (doc instanceof INodeNotifier) {
ModelQueryAdapter adapter = (ModelQueryAdapter) ((INodeNotifier) doc).getAdapterFor(ModelQueryAdapter.class);
CMDocument cmdoc = null;
if (adapter != null && adapter.getModelQuery() != null)
cmdoc = adapter.getModelQuery().getCorrespondingCMDocument(doc);
if (cmdoc != null) {
// model
if (cmdoc instanceof HTMLCMDocument)
return false;
if (cmdoc.supports(HTMLCMProperties.IS_XHTML))
return Boolean.TRUE.equals(cmdoc.getProperty(HTMLCMProperties.IS_XHTML));
}
}
// this should never be reached
DocumentType docType = doc.getDoctype();
// $NON-NLS-1$
return docType != null && docType.getPublicId() != null && docType.getPublicId().indexOf("-//W3C//DTD XHTML ") == 0;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument 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();
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class TestCatalogContentModels method assertIsXHTMLContentModel.
private void assertIsXHTMLContentModel(IDOMModel htmlModel) {
Element html = htmlModel.getDocument().getDocumentElement();
CMDocument correspondingCMDocument = ModelQueryUtil.getModelQuery(htmlModel).getCorrespondingCMDocument(html);
assertNotNull("content model document not found", correspondingCMDocument);
assertTrue("document is not XHTML", correspondingCMDocument.supports(HTMLCMProperties.IS_XHTML));
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class GlobalCMDocumentCacheTest method getCMDocumentFromXMLFile.
private CMDocument getCMDocumentFromXMLFile(String documentPath) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(documentPath));
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel structuredModel = null;
CMDocument cmDocument = null;
try {
structuredModel = modelManager.getModelForRead(file);
Document document = ((IDOMModel) structuredModel).getDocument();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
CMDocumentManager cmDocumentManager = modelQuery.getCMDocumentManager();
CMDocumentLoader loader = new CMDocumentLoader(document, cmDocumentManager);
loader.loadCMDocuments();
cmDocument = modelQuery.getCorrespondingCMDocument(document.getDocumentElement());
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (structuredModel != null) {
structuredModel.releaseFromRead();
}
}
return cmDocument;
}
Aggregations