Search in sources :

Example 46 with CMDocument

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;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) DOMContentBuilderImpl(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl) DOMWriter(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMWriter) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Document(org.w3c.dom.Document) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)

Example 47 with CMDocument

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]);
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 48 with CMDocument

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);
                    }
                }
            }
        }
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) URIResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver) List(java.util.List) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo) IResource(org.eclipse.core.resources.IResource)

Example 49 with CMDocument

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;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 50 with 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;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) Path(org.eclipse.core.runtime.Path)

Aggregations

CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)83 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)33 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)26 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)20 List (java.util.List)15 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)14 Document (org.w3c.dom.Document)12 Element (org.w3c.dom.Element)11 ArrayList (java.util.ArrayList)10 Path (org.eclipse.core.runtime.Path)8 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)8 JSPCMDocument (org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument)7 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 URL (java.net.URL)6 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)6 Node (org.w3c.dom.Node)6 NodeList (org.w3c.dom.NodeList)6 Iterator (java.util.Iterator)5 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)5