Search in sources :

Example 1 with CMDocumentCache

use of org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache in project webtools.sourceediting by eclipse.

the class ElementImplTests method testCMAttrWithNullImpliedValue.

public void testCMAttrWithNullImpliedValue() {
    IDOMModel model = null;
    try {
        model = (IDOMModel) getModelForRead("testfiles/time.xml");
        if (model != null) {
            IDOMDocument document = model.getDocument();
            final String ATTR_NAME = "second";
            // Setup a ModelQueryAdapter whose sole purpose it to provide a attribute declaration with a null implied value
            document.addAdapter(new ModelQueryAdapter() {

                public boolean isAdapterForType(Object type) {
                    return type.equals(ModelQueryAdapter.class);
                }

                public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                }

                public CMDocumentCache getCMDocumentCache() {
                    return null;
                }

                public org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver getIdResolver() {
                    return null;
                }

                public ModelQuery getModelQuery() {
                    return new ModelQueryImpl(null) {

                        /* (non-Javadoc)
							 * @see org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl#getCMElementDeclaration(org.w3c.dom.Element)
							 */
                        public CMElementDeclaration getCMElementDeclaration(Element element) {
                            final CMElementDeclaration decl = new CMElementDeclarationImpl(null, null);
                            CMNamedNodeMapImpl map = (CMNamedNodeMapImpl) decl.getAttributes();
                            map.put(new CMAttributeDeclarationImpl(ATTR_NAME, CMAttributeDeclaration.OPTIONAL, new CMDataTypeImpl(ATTR_NAME, (String) null)));
                            return decl;
                        }
                    };
                }

                public void release() {
                }

                public void setIdResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver newIdResolver) {
                }
            });
            Element element = document.getDocumentElement();
            assertNotNull(element);
            // Default value should be 0
            assertEquals("", element.getAttribute(ATTR_NAME));
        }
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAttributeDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) CMDataTypeImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 2 with CMDocumentCache

use of org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache in project webtools.sourceediting by eclipse.

the class ModelQueryAdapterFactoryForJSP method createAdapter.

/**
 * createAdapter method comment.
 */
protected INodeAdapter createAdapter(INodeNotifier target) {
    if (Debug.displayInfo)
        // $NON-NLS-1$
        System.out.println("-----------------------ModelQueryAdapterFactoryForJSP.createAdapter" + target);
    if (modelQueryAdapterImpl == null) {
        if (target instanceof IDOMNode) {
            IDOMNode xmlNode = (IDOMNode) target;
            IStructuredModel model = stateNotifier = xmlNode.getModel();
            if (model.getBaseLocation() != null) {
                stateNotifier.addModelStateListener(this);
            }
            org.eclipse.wst.sse.core.internal.util.URIResolver resolver = model.getResolver();
            if (Debug.displayInfo)
                // $NON-NLS-1$
                System.out.println("----------------ModelQueryAdapterFactoryForJSP... baseLocation : " + resolver.getFileBaseLocation());
            /**
             * XMLCatalogIdResolver currently requires a filesystem
             * location string. Customarily this will be what is in the
             * deprecated SSE URIResolver and required by the Common URI
             * Resolver.
             */
            URIResolver idResolver = null;
            if (resolver != null) {
                idResolver = new XMLCatalogIdResolver(resolver.getFileBaseLocation(), resolver);
            } else {
                /*
					 * 203649 - this block may be necessary due to ordering of
					 * setting the resolver into the model
					 */
                String baseLocation = null;
                String modelsBaseLocation = model.getBaseLocation();
                if (modelsBaseLocation != null) {
                    File file = new Path(modelsBaseLocation).toFile();
                    if (file.exists()) {
                        baseLocation = file.getAbsolutePath();
                    } else {
                        IPath basePath = new Path(model.getBaseLocation());
                        IResource derivedResource = null;
                        if (basePath.segmentCount() > 1)
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
                        else
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getProject(basePath.segment(0));
                        IPath derivedPath = derivedResource.getLocation();
                        if (derivedPath != null) {
                            baseLocation = derivedPath.toString();
                        } else {
                            URI uri = derivedResource.getLocationURI();
                            if (uri != null) {
                                baseLocation = uri.toString();
                            }
                        }
                    }
                    if (baseLocation == null) {
                        baseLocation = modelsBaseLocation;
                    }
                }
                idResolver = new XMLCatalogIdResolver(baseLocation, null);
            }
            ModelQuery modelQuery = createModelQuery(model, idResolver);
            modelQuery.setEditMode(ModelQuery.EDIT_MODE_UNCONSTRAINED);
            modelQueryAdapterImpl = new JSPModelQueryAdapterImpl(new CMDocumentCache(), modelQuery, idResolver);
        }
    }
    return modelQueryAdapterImpl;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) XMLCatalogIdResolver(org.eclipse.wst.xml.core.internal.modelquery.XMLCatalogIdResolver) IPath(org.eclipse.core.runtime.IPath) URIResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver) URI(java.net.URI) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource)

Example 3 with CMDocumentCache

use of org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache in project webtools.sourceediting by eclipse.

the class ModelQueryAdapterFactoryForXML method createAdapter.

/**
 * createAdapter method comment.
 */
protected INodeAdapter createAdapter(INodeNotifier target) {
    if (org.eclipse.wst.sse.core.internal.util.Debug.displayInfo)
        // $NON-NLS-1$
        System.out.println("-----------------------ModelQueryAdapterFactoryForXML.createAdapter" + target);
    if (modelQueryAdapterImpl == null) {
        if (target instanceof IDOMNode) {
            IDOMNode xmlNode = (IDOMNode) target;
            IStructuredModel model = xmlNode.getModel();
            stateNotifier = xmlNode.getModel();
            stateNotifier.addModelStateListener(getInternalModelStateListener());
            org.eclipse.wst.sse.core.internal.util.URIResolver resolver = model.getResolver();
            if (Debug.displayInfo)
                // $NON-NLS-1$
                System.out.println("----------------ModelQueryAdapterFactoryForXML... baseLocation : " + resolver.getFileBaseLocation());
            /**
             * XMLCatalogIdResolver currently requires a filesystem
             * location string. Customarily this will be what is in the
             * deprecated SSE URIResolver and required by the Common URI
             * Resolver.
             */
            URIResolver idResolver = null;
            if (resolver != null) {
                idResolver = new XMLCatalogIdResolver(resolver.getFileBaseLocation(), resolver);
            } else {
                /*
					 * 203649 - this block may be necessary due to ordering of
					 * setting the resolver into the model
					 */
                String baseLocation = null;
                String modelsBaseLocation = model.getBaseLocation();
                if (modelsBaseLocation != null) {
                    File file = new Path(modelsBaseLocation).toFile();
                    if (file.exists()) {
                        baseLocation = file.getAbsolutePath();
                    } else {
                        IPath basePath = new Path(model.getBaseLocation());
                        IResource derivedResource = null;
                        if (basePath.segmentCount() > 1)
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
                        else
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getProject(basePath.segment(0));
                        IPath derivedPath = derivedResource.getLocation();
                        if (derivedPath != null) {
                            baseLocation = derivedPath.toString();
                        } else {
                            URI uri = derivedResource.getLocationURI();
                            if (uri != null) {
                                baseLocation = uri.toString();
                            }
                        }
                    }
                    if (baseLocation == null) {
                        baseLocation = modelsBaseLocation;
                    }
                }
                idResolver = new XMLCatalogIdResolver(baseLocation, null);
            }
            CMDocumentCache cmDocumentCache = new CMDocumentCache();
            ModelQuery modelQuery = new XMLModelQueryImpl(cmDocumentCache, idResolver);
            // cs todo...
            // for now we create a CMDocumentCache on a 'per editor' basis
            // in the future we need to support a CMDocumentCache that is
            // shared between editors
            // nsd comment: may not be appropriate depending on
            CMDocumentManager documentManager = modelQuery.getCMDocumentManager();
            if (documentManager != null) {
                configureDocumentManager(documentManager);
            }
            modelQueryAdapterImpl = new ModelQueryAdapterImpl(cmDocumentCache, modelQuery, idResolver);
        }
    }
    return modelQueryAdapterImpl;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) CMDocumentManager(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager) URIResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver) ModelQueryAdapterImpl(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapterImpl) URI(java.net.URI) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource)

Example 4 with CMDocumentCache

use of org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache in project webtools.sourceediting by eclipse.

the class CMVisitorTest method test.

protected void test(String fileName) {
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        XMLAssociationProvider provider = new XMLAssociationProvider(new CMDocumentCache()) {
        };
        ModelQuery mq = new ModelQueryImpl(provider);
        Thread.currentThread().setContextClassLoader(CMVisitorTest.class.getClassLoader());
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(fileName);
        /*
			 * ClassLoader prevClassLoader =
			 * Thread.currentThread().getContextClassLoader();
			 * Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
			 * Class theClass =
			 * Class.forName("org.apache.xerces.parsers.DOMParser"); DOMParser
			 * parser = (DOMParser)theClass.newInstance();
			 * Thread.currentThread().setContextClassLoader(prevClassLoader);
			 * parser.parse(new InputSource(fileName)); Document document =
			 * parser.getDocument();
			 */
        visitNode(document, mq, 0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) XMLAssociationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.XMLAssociationProvider) Document(org.w3c.dom.Document) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache)

Example 5 with CMDocumentCache

use of org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache in project webtools.sourceediting by eclipse.

the class ModelQueryAdapterFactoryForHTML method createAdapter.

/**
 * createAdapter method comment.
 *
 * XXX: we must make this method more independent of 'location' (at least
 * provide some fall-back method).
 */
protected INodeAdapter createAdapter(INodeNotifier target) {
    if (Debug.displayInfo) {
        // $NON-NLS-1$
        Logger.log(Logger.INFO_DEBUG, "-----------------------ModelQueryAdapterFactoryForHTML.createAdapter" + target);
    }
    if (modelQueryAdapter == null) {
        if (target instanceof IDOMNode) {
            IDOMNode xmlNode = (IDOMNode) target;
            modelStateNotifier = xmlNode.getModel();
            modelStateNotifier.addModelStateListener(getInternalModelStateListener());
            IStructuredModel model = xmlNode.getModel();
            org.eclipse.wst.sse.core.internal.util.URIResolver resolver = model.getResolver();
            if (Debug.displayInfo)
                // $NON-NLS-1$
                System.out.println("----------------ModelQueryAdapterFactoryForHTML... baseLocation : " + resolver.getFileBaseLocation());
            /**
             * XMLCatalogIdResolver currently requires a filesystem
             * location string. Customarily this will be what is in the
             * deprecated SSE URIResolver and required by the Common URI
             * Resolver.
             */
            URIResolver idResolver = null;
            if (resolver != null) {
                idResolver = new XMLCatalogIdResolver(resolver.getFileBaseLocation(), resolver);
            } else {
                /*
					 * 203649 - this block may be necessary due to ordering of
					 * setting the resolver into the model
					 */
                String baseLocation = null;
                String modelsBaseLocation = model.getBaseLocation();
                if (modelsBaseLocation != null) {
                    File file = new Path(modelsBaseLocation).toFile();
                    if (file.exists()) {
                        baseLocation = file.getAbsolutePath();
                    } else {
                        IPath basePath = new Path(model.getBaseLocation());
                        IResource derivedResource = null;
                        if (basePath.segmentCount() > 1)
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
                        else
                            derivedResource = ResourcesPlugin.getWorkspace().getRoot().getProject(basePath.segment(0));
                        IPath derivedPath = derivedResource.getLocation();
                        if (derivedPath != null) {
                            baseLocation = derivedPath.toString();
                        } else {
                            URI uri = derivedResource.getLocationURI();
                            if (uri != null) {
                                baseLocation = uri.toString();
                            }
                        }
                    }
                    if (baseLocation == null) {
                        baseLocation = modelsBaseLocation;
                    }
                }
                idResolver = new XMLCatalogIdResolver(baseLocation, null);
            }
            CMDocumentCache documentCache = new CMDocumentCache();
            ModelQuery modelQuery = new HTMLModelQueryImpl(documentCache, idResolver);
            modelQuery.setEditMode(ModelQuery.EDIT_MODE_UNCONSTRAINED);
            modelQueryAdapter = new ModelQueryAdapterImpl(documentCache, modelQuery, idResolver);
        }
    }
    return modelQueryAdapter;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) XMLCatalogIdResolver(org.eclipse.wst.xml.core.internal.modelquery.XMLCatalogIdResolver) IPath(org.eclipse.core.runtime.IPath) URIResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver) ModelQueryAdapterImpl(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapterImpl) URI(java.net.URI) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource)

Aggregations

ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)5 CMDocumentCache (org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache)5 File (java.io.File)3 URI (java.net.URI)3 IFile (org.eclipse.core.resources.IFile)3 IResource (org.eclipse.core.resources.IResource)3 IPath (org.eclipse.core.runtime.IPath)3 Path (org.eclipse.core.runtime.Path)3 URIResolver (org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)3 ModelQueryImpl (org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl)2 XMLCatalogIdResolver (org.eclipse.wst.xml.core.internal.modelquery.XMLCatalogIdResolver)2 ModelQueryAdapterImpl (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapterImpl)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)1 URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)1 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)1 CMAttributeDeclarationImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl)1 CMDataTypeImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl)1