use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument 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.CMDocument in project webtools.sourceediting by eclipse.
the class NewXMLWizard method showDialog.
public static void showDialog(Shell shell, IFile file, IStructuredSelection structuredSelection) {
String[] errorInfo = new String[2];
// (cs) the URI argument to createCMDocument needs to be a fully
// qualified URI
//
CMDocument cmDocument = NewXMLGenerator.createCMDocument(URIHelper.getPlatformURI(file), errorInfo);
if (errorInfo[0] == null) {
NewXMLWizard wizard = new NewXMLWizard(file, cmDocument);
wizard.init(PlatformUI.getWorkbench(), structuredSelection);
wizard.setNeedsProgressMonitor(true);
WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.create();
dialog.getShell().setText(XMLWizardsMessages._UI_DIALOG_NEW_TITLE);
dialog.setBlockOnOpen(true);
dialog.open();
} else {
MessageDialog.openInformation(shell, errorInfo[0], errorInfo[1]);
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class EditNamespaceInfoDialog method performBrowse.
protected void performBrowse() {
// $NON-NLS-1$
String[] extensions = { ".xsd" };
SelectFileOrXMLCatalogIdDialog dialog = new SelectFileOrXMLCatalogIdDialog(getShell(), extensions);
dialog.create();
dialog.getShell().setText(XMLUIMessages._UI_LABEL_SELECT_FILE);
dialog.setBlockOnOpen(true);
dialog.open();
if (dialog.getReturnCode() == Window.OK) {
String grammarURI = null;
IFile file = dialog.getFile();
String id = dialog.getId();
if (file != null) {
String uri = null;
if (resourceLocation != null) {
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(resourceLocation);
if (resource != null) {
IPath location = resource.getLocation();
if (location != null) {
uri = URIHelper.getRelativeURI(file.getLocation(), location);
}
} else {
uri = URIHelper.getRelativeURI(file.getLocation(), resourceLocation);
}
grammarURI = file.getLocation().toOSString();
} else {
uri = file.getLocation().toOSString();
grammarURI = uri;
}
locationHintField.setText(uri);
} else if (id != null) {
locationHintField.setText(id);
URIResolver resolver = URIResolverPlugin.createResolver();
grammarURI = resolver.resolve(null, id, id);
}
// $NON-NLS-1$
CMDocument document = ContentModelManager.getInstance().createCMDocument(URIHelper.getURIForFilePath(grammarURI), "xsd");
if (document != null) {
// $NON-NLS-1$
List namespaceInfoList = (List) document.getProperty("http://org.eclipse.wst/cm/properties/namespaceInfo");
if (namespaceInfoList != null) {
NamespaceInfo info = (NamespaceInfo) namespaceInfoList.get(0);
if (info != null) {
if ((uriField.getText().trim().length() == 0) && (info.uri != null)) {
uriField.setText(info.uri);
}
if ((prefixField.getText().trim().length() == 0) && (info.prefix != null)) {
prefixField.setText(info.prefix);
}
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class XSDImpl method buildCMDocument.
/**
* Given uri for an XML Schema document, parse the document and build
* corresponding CMDocument node.
*
* @param uri -
* the uri for an XML Schema document
* @return the corresponding CMDocument node.
*/
public static CMDocument buildCMDocument(String uri) {
CMDocument cmDocument = null;
XSDSchema xsdSchema = buildXSDModel(uri);
if (xsdSchema != null) {
cmDocument = (CMDocument) getAdapter(xsdSchema);
}
return cmDocument;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMDocument in project webtools.sourceediting by eclipse.
the class JSPModelQueryExtension method getDefaultJSPCMDocument.
/**
* <p>For JSP files and segments, this is just the JSP
* document, but when editing tag files and their fragments, it
* should be the tag document.</p>
*
* <p>It may also vary based on the model being edited in the future.</p>
*
* <p><b>NOTE:</b>Copied from JSPContentAssistProcessor</p>
*
* @return the default non-embedded CMDocument for the document being
* edited.
*/
private CMDocument getDefaultJSPCMDocument(IDOMNode node) {
CMDocument jcmdoc = null;
// handle tag files here
String contentType = node.getModel().getContentTypeIdentifier();
if (ContentTypeIdForJSP.ContentTypeID_JSPTAG.equals(contentType)) {
jcmdoc = JSPCMDocumentFactory.getCMDocument(CMDocType.TAG20_DOC_TYPE);
} else {
String modelPath = node.getModel().getBaseLocation();
if (modelPath != null && !IModelManager.UNMANAGED_MODEL.equals(modelPath)) {
float version = DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(new Path(modelPath));
jcmdoc = JSPCMDocumentFactory.getCMDocument(version);
}
if (jcmdoc == null) {
jcmdoc = JSPCMDocumentFactory.getCMDocument();
}
}
return jcmdoc;
}
Aggregations