use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method computeEntityReferenceProposals.
/**
* return all possible EntityReferenceProposals (according to current
* position in doc)
*/
private ICompletionProposal[] computeEntityReferenceProposals(ITextRegion completionRegion, IDOMNode treeNode, CompletionProposalInvocationContext context) {
// only handle XML content for now
int documentPosition = context.getInvocationOffset();
// ICompletionProposals
Vector proposals = new Vector();
IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(context.getViewer(), context.getInvocationOffset());
if ((completionRegion != null) && (completionRegion.getType() == DOMRegionContext.XML_CONTENT)) {
int nodeOffset = documentPosition - sdRegion.getStartOffset(completionRegion);
String regionText = sdRegion.getFullText(completionRegion);
// the previous region...there might be a better way to do this
if ((regionText != null) && regionText.trim().equals("") && (documentPosition > 0)) {
// $NON-NLS-1$
IStructuredDocumentRegion prev = treeNode.getStructuredDocument().getRegionAtCharacterOffset(documentPosition - 1);
if ((prev != null) && prev.getText().equals("&")) {
// $NON-NLS-1$
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=206680
// examine previous region
sdRegion = prev;
completionRegion = prev.getLastRegion();
regionText = prev.getFullText();
nodeOffset = 1;
}
}
// string must start w/ &
if ((regionText != null) && regionText.startsWith("&")) {
// $NON-NLS-1$
// $NON-NLS-1$
String key = (nodeOffset > 0) ? regionText.substring(1, nodeOffset) : "";
// get entity proposals, passing in the appropriate start
// string
ModelQuery mq = ModelQueryUtil.getModelQuery(((Node) treeNode).getOwnerDocument());
if (mq != null) {
CMDocument xmlDoc = mq.getCorrespondingCMDocument(treeNode);
CMNamedNodeMap cmmap = null;
Properties entities = null;
if (xmlDoc != null) {
cmmap = xmlDoc.getEntities();
}
if (cmmap != null) {
entities = mapToProperties(cmmap);
} else // 224787 in absence of content model, just use
// minimal 5 entities
{
entities = new Properties();
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("quot", "\"");
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("apos", "'");
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("amp", "&");
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("lt", "<");
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("gt", ">");
// $NON-NLS-1$ //$NON-NLS-2$
entities.put("nbsp", " ");
}
addEntityProposals(proposals, entities, key, nodeOffset, sdRegion, completionRegion, context);
}
}
}
return (ICompletionProposal[]) ((proposals.size() > 0) ? proposals.toArray(new ICompletionProposal[proposals.size()]) : null);
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method getAvailableRootChildren.
/**
* returns a list of CMElementDeclarations
*
* @param document
* @param childIndex
* @return
*/
private List getAvailableRootChildren(Document document, int childIndex) {
List list = null;
// extract the valid 'root' node name from the DocumentType Node
DocumentType docType = document.getDoctype();
String rootName = null;
if (docType != null) {
rootName = docType.getNodeName();
}
if (rootName == null) {
return new ArrayList(0);
}
for (Node child = document.getFirstChild(); child != null; child = child.getNextSibling()) {
// is it required to be an Element?
if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equalsIgnoreCase(rootName)) {
// count it as present
if ((child instanceof IDOMNode) && ((((IDOMNode) child).getStartStructuredDocumentRegion() == null) || (((IDOMNode) child).getEndStructuredDocumentRegion() == null))) {
continue;
}
if (Debug.displayInfo) {
// $NON-NLS-1$
System.out.println(rootName + " already present!");
}
setErrorMessage(NLS.bind(XMLUIMessages.The_document_element__, (new Object[] { rootName })));
return new ArrayList(0);
}
}
list = new ArrayList(1);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
if (modelQuery != null) {
CMDocument cmdoc = modelQuery.getCorrespondingCMDocument(document);
if (cmdoc != null) {
if (rootName != null) {
CMElementDeclaration rootDecl = (CMElementDeclaration) cmdoc.getElements().getNamedItem(rootName);
if (rootDecl != null) {
list.add(rootDecl);
} else {
// supply the given document name anyway, even if it
// is an error
list.add(new SimpleCMElementDeclaration(rootName));
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
if (location.length() > 0) {
setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for_in, (new Object[] { rootName, location })));
} else {
setErrorMessage(NLS.bind(XMLUIMessages.No_definition_for, (new Object[] { rootName })));
}
}
}
} else {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String location = "" + (docType.getPublicId() != null ? docType.getPublicId() + "/" : "") + (docType.getSystemId() != null ? docType.getSystemId() : "");
if (location.length() > 0) {
setErrorMessage(NLS.bind(XMLUIMessages.No_content_model_for, (new Object[] { location })));
} else {
setErrorMessage(XMLUIMessages.No_content_model_found_UI_);
}
}
}
return list;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class QuickAssistProcessorXML method getRequiredAttrs.
protected List getRequiredAttrs(Node node) {
List result = new ArrayList();
ModelQuery modelQuery = getModelQuery(node);
if (modelQuery != null) {
CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element) node);
if (elementDecl != null) {
CMNamedNodeMap attrMap = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
for (int k = 0; k < nodes.size(); k++) {
CMNode cmnode = (CMNode) nodes.get(k);
if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
allAttributes.put(cmnode);
}
}
attrMap = allAttributes;
Iterator it = attrMap.iterator();
CMAttributeDeclaration attr = null;
while (it.hasNext()) {
attr = (CMAttributeDeclaration) it.next();
if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
result.add(attr);
}
}
}
}
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class XMLPropertySource method _getValidStrings.
private String[] _getValidStrings(CMAttributeDeclaration attrDecl, CMDataType valuesHelper) {
String attributeName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
List values = new ArrayList(1);
boolean currentValueKnown = false;
boolean checkIfCurrentValueIsKnown = ((fNode.getAttributes() != null) && (fNode.getAttributes().getNamedItem(attributeName) != null) && (fNode.getAttributes().getNamedItem(attributeName).getNodeValue() != null));
String currentValue = null;
if (checkIfCurrentValueIsKnown) {
currentValue = fNode.getAttributes().getNamedItem(attributeName).getNodeValue();
}
if ((valuesHelper.getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED) && (valuesHelper.getImpliedValue() != null)) {
// FIXED value
currentValueKnown = (currentValue != null) && valuesHelper.getImpliedValue().equals(currentValue);
values.add(valuesHelper.getImpliedValue());
} else {
// ENUMERATED values
String[] valueStrings = null;
// valueStrings = valuesHelper.getEnumeratedValues();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument());
if ((modelQuery != null) && (fNode.getNodeType() == Node.ELEMENT_NODE)) {
valueStrings = modelQuery.getPossibleDataTypeValues((Element) fNode, attrDecl);
} else {
valueStrings = attrDecl.getAttrType().getEnumeratedValues();
}
if (valueStrings != null) {
for (int i = 0; i < valueStrings.length; i++) {
if (checkIfCurrentValueIsKnown && valueStrings[i].equals(currentValue)) {
currentValueKnown = true;
}
values.add(valueStrings[i]);
}
}
}
if ((valuesHelper.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && (valuesHelper.getImpliedValue() != null)) {
if (!values.contains(valuesHelper.getImpliedValue())) {
values.add(valuesHelper.getImpliedValue());
}
}
if (checkIfCurrentValueIsKnown && !currentValueKnown && (currentValue != null) && (currentValue.length() > 0)) {
values.add(currentValue);
}
String[] validStrings = new String[values.size()];
validStrings = (String[]) values.toArray(validStrings);
return validStrings;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class ToggleEditModeHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ITextEditor textEditor = null;
if (editor instanceof ITextEditor)
textEditor = (ITextEditor) editor;
else {
Object o = editor.getAdapter(ITextEditor.class);
if (o != null)
textEditor = (ITextEditor) o;
}
if (textEditor != null) {
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
ModelQuery modelQuery;
try {
modelQuery = ModelQueryUtil.getModelQuery(model);
} finally {
model.releaseFromRead();
}
if (modelQuery != null) {
int newState = getNextState(modelQuery.getEditMode());
modelQuery.setEditMode(newState);
// Force a Refresh on this command so that the image can
// be
// updated.
ICommandService commandService = HandlerUtil.getActiveWorkbenchWindow(event).getService(ICommandService.class);
Map<String, IWorkbenchWindow> filter = new HashMap<>();
filter.put(IServiceScopes.WINDOW_SCOPE, HandlerUtil.getActiveWorkbenchWindow(event));
commandService.refreshElements(event.getCommand().getId(), filter);
}
}
}
return null;
}
Aggregations