use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class BugFixesTest method testBase64BinaryDefaultValue.
/**
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=322841
*/
public void testBase64BinaryDefaultValue() {
Bundle bundle = Platform.getBundle(XSDCoreTestsPlugin.PLUGIN_ID);
// $NON-NLS-1$
URL url = bundle.getEntry("/testresources/samples/base64Binary/Test.xsd");
CMDocument cmDocument = XSDImpl.buildCMDocument(url.toExternalForm());
assertNotNull(cmDocument);
CMNamedNodeMap elements = cmDocument.getElements();
// $NON-NLS-1$
CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) elements.getNamedItem("Test");
assertNotNull(cmElementDeclaration);
CMDataType dataType = cmElementDeclaration.getDataType();
assertNotNull(dataType);
String impliedValue = dataType.generateInstanceValue();
// $NON-NLS-1$
assertEquals("MA==", impliedValue);
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class TestNewXMLGenerator method runOneTest.
/**
* Runs a generic generate + comparison test.
* @param buildPolicy Integer representing the build policy for this generation.
* @param rootElement Desired root element.
* @param xsdUri URI of the desired XML schema.
*/
private void runOneTest(int buildPolicy, String rootElement, String xsdUriFile) {
try {
// generic setup
PLUGIN_ABSOLUTE_PATH = XMLUITestsPlugin.getInstallURL();
// $NON-NLS-3$
String uri = "file:///" + PLUGIN_ABSOLUTE_PATH + "testresources/" + xsdUriFile + ".xsd";
String[] errorInfo = new String[2];
CMDocument cmd = NewXMLGenerator.createCMDocument(uri, errorInfo);
NewXMLGenerator generator = new NewXMLGenerator(uri, cmd);
String id = xsdUriFile + "-" + rootElement + "-" + buildPolicy;
// $NON-NLS-2$
String fileNameResult = PLUGIN_ABSOLUTE_PATH + "testresources/Tested-" + id + ".xml";
File nFile = new File(fileNameResult);
if (!nFile.exists()) {
nFile.createNewFile();
}
// $NON-NLS-2$
String fileNameCompare = PLUGIN_ABSOLUTE_PATH + "testresources/Compare-" + id + ".xml";
generator.setBuildPolicy(buildPolicy);
generator.setRootElementName(rootElement);
// $NON-NLS-1$
generator.setDefaultSystemId(xsdUriFile + ".xsd");
generator.createNamespaceInfoList();
generator.createXMLDocument(fileNameResult);
XMLDiff differ = new XMLDiff();
// $NON-NLS-2$
assertTrue("The XML files are not identical.", differ.diff(fileNameCompare, fileNameResult, "XML"));
// if we've made it this far, delete the output file
remove(nFile);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class CMDocumentFactoryTLD method createCMDocument.
/**
* Builds a CMDocument from a taglib descriptor
*
* @param uri -
* the location of a valid taglib descriptor
*/
public CMDocument createCMDocument(String uri) {
CMDocument result = null;
URL url = null;
try {
url = new URL(uri);
} catch (MalformedURLException e) {
result = createCMDocumentFromFile(uri);
}
if (result == null && url != null) {
if (url.getProtocol().equals("file")) {
// $NON-NLS-1$
result = createCMDocumentFromFile(url.getFile());
} else {
/**
* Skip anything else since trying to load a TLD from a remote
* location has unpredictable performance impact.
*/
}
}
if (result == null)
result = new CMDocumentImpl();
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument 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.CMDocument 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);
}
}
}
}
}
}
}
Aggregations