use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class InferredGrammarFactory method debugPrint.
public void debugPrint(Collection collection) {
for (Iterator iter = collection.iterator(); iter.hasNext(); ) {
CMDocument cmDocument = (CMDocument) iter.next();
// $NON-NLS-1$
System.out.println("-----------------------------------------------");
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("cmDocument (" + cmDocument.getProperty("http://org.eclipse.wst/cm/properties/targetNamespaceURI") + ")");
CMNamedNodeMapImpl elementMap = (CMNamedNodeMapImpl) cmDocument.getElements();
int size = elementMap.getLength();
for (int i = 0; i < size; i++) {
CMElementDeclaration ed = (CMElementDeclaration) elementMap.item(i);
CMDescriptionBuilder builder = new CMDescriptionBuilder();
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println(" ELEMENT " + ed.getNodeName() + " = " + builder.buildDescription(ed));
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class InferredGrammarFactory method createCMElementDeclaration.
public CMElementDeclaration createCMElementDeclaration(CMDocument cmDocument, Element element, boolean isLocal) {
String localName = element.getLocalName();
CMDocumentImpl cmDocumentImpl = (CMDocumentImpl) cmDocument;
CMNamedNodeMapImpl elementMap = isLocal ? (CMNamedNodeMapImpl) cmDocumentImpl.getLocalElementPool() : (CMNamedNodeMapImpl) cmDocumentImpl.getElements();
CMElementDeclarationImpl ed = (CMElementDeclarationImpl) elementMap.getNamedItem(localName);
if (ed == null) {
// System.out.println("create ed " + localName);
ed = new CMElementDeclarationImpl(cmDocument, localName);
ed.setInferred(true);
ed.setLocal(isLocal);
ed.setMaxOccur(1);
CMGroupImpl group = new CMGroupImpl(new CMNodeListImpl(), CMGroup.CHOICE);
group.setInferred(true);
group.setMinOccur(0);
group.setMaxOccur(-1);
ed.setContent(group);
elementMap.put(ed);
}
// lookup or create the attributes
//
NamedNodeMap attributeMap = element.getAttributes();
int attributeMapLength = attributeMap.getLength();
for (int i = 0; i < attributeMapLength; i++) {
Attr attr = (Attr) attributeMap.item(i);
CMAttributeDeclarationImpl ad = (CMAttributeDeclarationImpl) ed.getAttributeMap().getNamedItem(attr.getNodeName());
if (ad == null) {
// todo... use an attribute pool to be more efficient?
ad = new CMAttributeDeclarationImpl(attr.getNodeName(), CMAttributeDeclaration.OPTIONAL);
ad.setInferred(true);
ed.getAttributeMap().put(ad);
}
}
return ed;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class JSPActionValidator method processDirective.
private void processDirective(IReporter reporter, IFile file, IStructuredModel model, IStructuredDocumentRegion documentRegion) {
IndexedRegion ir = model.getIndexedRegion(documentRegion.getStartOffset());
if (ir instanceof IDOMElement) {
IDOMElement element = (IDOMElement) ir;
ModelQuery query = ModelQueryUtil.getModelQuery(model);
if (query != null) {
CMElementDeclaration cmElement = query.getCMElementDeclaration(element);
if (cmElement != null) {
CMNamedNodeMap cmAttributes = null;
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl();
List nodes = query.getAvailableContent(element, cmElement, 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);
}
}
cmAttributes = allAttributes;
boolean foundjspattribute = checkUnknownAttributes(element, cmElement, cmAttributes, reporter, file, model.getStructuredDocument(), documentRegion);
// missing required attributes
if (!foundjspattribute && fSeverityMissingRequiredAttribute != ValidationMessage.IGNORE)
checkRequiredAttributes(element, cmAttributes, reporter, file, model.getStructuredDocument(), documentRegion);
if (fSeverityNonEmptyInlineTag != ValidationMessage.IGNORE)
checkNonEmptyInlineTag(element, cmElement, reporter, file, model.getStructuredDocument());
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addAttributeValueProposals.
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// Find the attribute region and name for which this position should
// have a value proposed
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;
}
}
// the name region is REQUIRED to do anything useful
if (nameRegion != null) {
// Retrieve the declaration
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
// String attributeName = nameRegion.getText();
String attributeName = open.getText(nameRegion);
CMAttributeDeclaration attrDecl = null;
// declaration for the attribute otherwise
if (elementDecl != null) {
CMNamedNodeMap attributes = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes) {
private Map caseInsensitive;
private Map getCaseInsensitiveMap() {
if (caseInsensitive == null)
caseInsensitive = new HashMap();
return caseInsensitive;
}
public CMNode getNamedItem(String name) {
CMNode node = super.getNamedItem(name);
if (node == null) {
node = (CMNode) getCaseInsensitiveMap().get(name.toLowerCase(Locale.US));
}
return node;
}
public void put(CMNode cmNode) {
super.put(cmNode);
getCaseInsensitiveMap().put(cmNode.getNodeName().toLowerCase(Locale.US), cmNode);
}
};
if (node.getNodeType() == Node.ELEMENT_NODE) {
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);
}
}
}
attributes = allAttributes;
String noprefixName = DOMNamespaceHelper.getUnprefixedName(attributeName);
if (attributes != null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
if (attrDecl == null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attributeName);
}
}
if (attrDecl == null) {
setErrorMessage(UNKNOWN_ATTR, attributeName);
}
}
String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
String proposedInfo = null;
Image image = CMImageUtil.getImage(attrDecl);
if (image == null) {
if ((attrDecl != null) && (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED)) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
} else {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
}
if ((attrDecl != null) && (attrDecl.getAttrType() != null)) {
// attribute is known, prompt with values from the declaration
proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
List possibleValues = getPossibleDataTypeValues(node, attrDecl);
String defaultValue = attrDecl.getAttrType().getImpliedValue();
if (possibleValues.size() > 0 || defaultValue != null) {
// ENUMERATED VALUES
String matchString = contentAssistRequest.getMatchString();
if (matchString == null) {
// $NON-NLS-1$
matchString = "";
}
if ((matchString.length() > 0) && (matchString.startsWith("\"") || matchString.startsWith("'"))) {
matchString = matchString.substring(1);
}
boolean currentValid = false;
// d210858, if the region's a container, don't suggest the
// enumerated values as they probably won't help
boolean existingComplicatedValue = (contentAssistRequest.getRegion() != null) && (contentAssistRequest.getRegion() instanceof ITextRegionContainer);
if (!existingComplicatedValue) {
int rOffset = contentAssistRequest.getReplacementBeginPosition();
int rLength = contentAssistRequest.getReplacementLength();
for (Iterator j = possibleValues.iterator(); j.hasNext(); ) {
String possibleValue = (String) j.next();
if (!possibleValue.equals(defaultValue)) {
currentValid = currentValid || possibleValue.equals(currentValue);
if ((matchString.length() == 0) || possibleValue.startsWith(matchString)) {
// $NON-NLS-2$//$NON-NLS-1$
String rString = "\"" + possibleValue + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, possibleValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
if (defaultValue != null && ((matchString.length() == 0) || defaultValue.startsWith(matchString))) {
// $NON-NLS-2$//$NON-NLS-1$
String rString = "\"" + defaultValue + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, defaultValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
} else if (((attrDecl.getUsage() == CMAttributeDeclaration.FIXED) || (attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)) && (attrDecl.getAttrType().getImpliedValue() != null)) {
// FIXED values
String value = attrDecl.getAttrType().getImpliedValue();
if ((value != null) && (value.length() > 0)) {
// $NON-NLS-2$//$NON-NLS-1$
String rValue = "\"" + value + "\"";
CustomCompletionProposal proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
if ((currentValue.length() > 0) && !value.equals(currentValue)) {
// $NON-NLS-2$//$NON-NLS-1$
rValue = "\"" + currentValue + "\"";
proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
} else {
// unknown attribute, so supply nice empty values
proposedInfo = getAdditionalInfo(null, elementDecl);
CustomCompletionProposal proposal = null;
if ((currentValue != null) && (currentValue.length() > 0)) {
// $NON-NLS-2$//$NON-NLS-1$
String rValue = "\"" + currentValue + "\"";
proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
} else {
setErrorMessage(UNKNOWN_CONTEXT);
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl in project webtools.sourceediting by eclipse.
the class XMLQuickAssistProcessor method getRequiredAttrs.
private 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;
}
Aggregations