use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method computeEntityReferenceProposals.
/**
* return all possible EntityReferenceProposals (according to current
* position in doc)
*/
protected ICompletionProposal[] computeEntityReferenceProposals(int documentPosition, ITextRegion completionRegion, IDOMNode treeNode) {
// only handle XML content for now
// ICompletionProposals
Vector proposals = new Vector();
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
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);
}
}
}
return (ICompletionProposal[]) ((proposals.size() > 0) ? proposals.toArray(new ICompletionProposal[proposals.size()]) : null);
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class NewXMLGenerator method createXMLDocument.
public ByteArrayOutputStream createXMLDocument(String xmlFileName, String charset) throws Exception {
if (charset == null) {
charset = getUserPreferredCharset();
if (charset == null) {
// $NON-NLS-1$
charset = "UTF-8";
}
}
CMDocument cmDocument = getCMDocument();
Assert.isNotNull(cmDocument);
Assert.isNotNull(getRootElementName());
// create the xml model
CMNamedNodeMap nameNodeMap = cmDocument.getElements();
CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) nameNodeMap.getNamedItem(getRootElementName());
Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
DOMContentBuilderImpl contentBuilder = new DOMContentBuilderImpl(xmlDocument);
// this 'uglyTempHack' flag is required in order to supress the
// creation a default encoding
// we'll handle this later in the domWriter.print() method used below
//
contentBuilder.supressCreationOfDoctypeAndXMLDeclaration = true;
contentBuilder.setBuildPolicy(buildPolicy);
contentBuilder.setOptionalElementDepthLimit(optionalElementDepthLimit);
contentBuilder.setExternalCMDocumentSupport(new MyExternalCMDocumentSupport(namespaceInfoList, xmlFileName));
contentBuilder.createDefaultRootContent(cmDocument, cmElementDeclaration, namespaceInfoList);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset);
DOMWriter domWriter = new DOMWriter(outputStreamWriter);
// TODO... instead of relying on file extensions, we need to keep
// track of the grammar type
// better yet we should reate an SSE document so that we can format it
// nicely before saving
// then we won't need the DOMWriter at all
//
domWriter.print(xmlDocument, charset, cmDocument.getNodeName(), getNonWhitespaceString(getPublicId()), getNonWhitespaceString(getSystemId()));
outputStream.flush();
outputStream.close();
return outputStream;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class AttributeContextInformationProvider method getInfoForElement.
/**
* @param node
*/
private IContextInformation[] getInfoForElement(IDOMNode node) {
IContextInformation[] results = EMPTY_CONTEXT_INFO;
CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) node);
if (decl != null) {
CMNamedNodeMap attributes = decl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, decl, 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 attrContextString = node.getNodeName();
// $NON-NLS-1$
StringBuffer attrInfo = new StringBuffer(" ");
// $NON-NLS-1$
String name = "";
HashMap attrPosMap = new HashMap();
int pos = 0;
int length = 0;
int numPerLine = 8;
CMAttributeDeclaration[] sortedAttrs = getSortedAttributes(attributes);
for (int i = 0; i < sortedAttrs.length; i++) {
name = sortedAttrs[i].getAttrName();
length = name.length();
pos = attrInfo.length();
attrInfo.append(name);
if (sortedAttrs[i].getUsage() == CMAttributeDeclaration.REQUIRED) {
// $NON-NLS-1$
attrInfo.append("*");
length++;
}
if (i < attributes.getLength() - 1) {
// $NON-NLS-1$
attrInfo.append(" ");
if ((i != 0) && (i % numPerLine == 0)) {
// $NON-NLS-1$
attrInfo.append("\n ");
}
}
attrPosMap.put(name, new Position(pos, length));
}
if (!attrInfo.toString().trim().equals("")) {
return new IContextInformation[] { new AttributeContextInformation(attrContextString, attrInfo.toString(), attrPosMap) };
}
}
return results;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class HTML5ContentModelTest method checkAttrValues.
private void checkAttrValues(String documentKey, String elementName, String attrName, String[] attrValues) {
CMDocument document = HTMLCMDocumentFactory.getCMDocument(documentKey);
CMNode elementDeclaration = document.getElements().getNamedItem(elementName);
assertEquals("not an element declaration:" + elementDeclaration, CMNode.ELEMENT_DECLARATION, elementDeclaration.getNodeType());
assertNotNull("missing element declaration:" + elementName, elementDeclaration);
CMNamedNodeMap attributes = ((CMElementDeclaration) elementDeclaration).getAttributes();
final CMNode node = attributes.getNamedItem(attrName);
assertNotNull("No attribute [" + attrName + "]", node);
final String[] actualValues = ((HTMLAttributeDeclaration) node).getAttrType().getEnumeratedValues();
assertEquals(attrValues.length, actualValues.length);
Set valueSet = new HashSet(actualValues.length);
for (int i = 0; i < actualValues.length; i++) {
valueSet.add(actualValues[i]);
}
for (int i = 0; i < attrValues.length; i++) {
if (!valueSet.remove(attrValues[i]))
fail("Type did not contain attribute value [" + attrValues[i] + "]");
}
assertTrue("Type had unexpected attribute values", valueSet.isEmpty());
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class HTML5ContentModelTest method checkAttrNames.
/**
* @param cm_doc_type
* @param elementName
* @param attrNameImport
*/
private void checkAttrNames(String documentKey, String elementName, String[] attrNames) {
CMDocument document = HTMLCMDocumentFactory.getCMDocument(documentKey);
CMNode elementDeclaration = document.getElements().getNamedItem(elementName);
assertEquals("not an element declaration:" + elementDeclaration, CMNode.ELEMENT_DECLARATION, elementDeclaration.getNodeType());
assertNotNull("missing element declaration:" + elementName, elementDeclaration);
CMNamedNodeMap attributes = ((CMElementDeclaration) elementDeclaration).getAttributes();
for (int i = 0; i < attrNames.length; i++) {
assertNotNull("missing attribute declaration:" + attrNames[i] + " for element: " + elementName, attributes.getNamedItem(attrNames[i]));
}
assertEquals("Attributes defined in content model that are not expected by the test for element: " + elementName, attributes.getLength(), attrNames.length);
}
Aggregations