Search in sources :

Example 26 with NamespaceInfo

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

the class XSDEditNamespacesAction method run.

public void run() {
    if (element != null) {
        Shell shell = XMLCommonResources.getInstance().getWorkbench().getActiveWorkbenchWindow().getShell();
        String targetNamespace = null;
        if (xsdSchema != null) {
            targetNamespace = xsdSchema.getTargetNamespace();
        }
        XSDEditSchemaInfoDialog dialog = new XSDEditSchemaInfoDialog(shell, new Path(resourceLocation), targetNamespace);
        List namespaceInfoList = namespaceInfoManager.getNamespaceInfoList(element);
        List oldNamespaceInfoList = NamespaceInfo.cloneNamespaceInfoList(namespaceInfoList);
        // update the document in response to these changes
        for (Iterator i = namespaceInfoList.iterator(); i.hasNext(); ) {
            NamespaceInfo info = (NamespaceInfo) i.next();
            NamespaceInfo oldCopy = new NamespaceInfo(info);
            // $NON-NLS-1$
            info.setProperty("oldCopy", oldCopy);
        }
        dialog.setNamespaceInfoList(namespaceInfoList);
        dialog.create();
        dialog.getShell().setSize(500, 400);
        // $NON-NLS-1$
        dialog.getShell().setText(XMLCommonResources.getInstance().getString("_UI_MENU_EDIT_SCHEMA_INFORMATION_TITLE"));
        dialog.setBlockOnOpen(true);
        dialog.open();
        // $NON-NLS-1$
        String xsdPrefix = null;
        String origXSDPrefix = xsdSchema.getSchemaForSchemaQNamePrefix();
        String newTNSPrefix = "";
        if (dialog.getReturnCode() == Window.OK) {
            Element xsdSchemaElement = xsdSchema.getElement();
            DocumentImpl doc = (DocumentImpl) xsdSchemaElement.getOwnerDocument();
            List newInfoList = dialog.getNamespaceInfoList();
            // see if we need to rename any prefixes
            Map prefixMapping = createPrefixMapping(oldNamespaceInfoList, namespaceInfoList);
            String origTNSPrefix = null;
            Map origPrefixMap = xsdSchema.getQNamePrefixToNamespaceMap();
            for (Iterator iter = origPrefixMap.keySet().iterator(); iter.hasNext(); ) {
                String key = (String) iter.next();
                String ns = (String) origPrefixMap.get(key);
                if ((targetNamespace == null && ns == null) || targetNamespace != null && targetNamespace.equals(ns)) {
                    origTNSPrefix = key;
                    break;
                }
            }
            Map map2 = new Hashtable();
            for (Iterator iter = newInfoList.iterator(); iter.hasNext(); ) {
                NamespaceInfo ni = (NamespaceInfo) iter.next();
                String pref = ni.prefix;
                String uri = ni.uri;
                if (pref == null)
                    // $NON-NLS-1$
                    pref = "";
                if (uri == null)
                    // $NON-NLS-1$
                    uri = "";
                if (XSDConstants.isSchemaForSchemaNamespace(uri)) {
                    xsdPrefix = pref;
                }
                if (uri.equals(dialog.getTargetNamespace())) {
                    newTNSPrefix = pref;
                }
                map2.put(pref, uri);
            }
            if (map2.size() > 0) {
                try {
                    doc.getModel().beginRecording(this, XSDEditorPlugin.getXSDString("_UI_NAMESPACE_CHANGE"));
                    boolean targetNamespaceChanged = (targetNamespace != null && !targetNamespace.equals(dialog.getTargetNamespace()) || targetNamespace == null && dialog.getTargetNamespace() != null);
                    boolean tnsPrefixChanged = !newTNSPrefix.equals(origTNSPrefix);
                    boolean xsdPrefixChanged = (!(origXSDPrefix == null && xsdPrefix.equals("")) || (origXSDPrefix != null && !origXSDPrefix.equals(xsdPrefix)));
                    xsdSchema.setIncrementalUpdate(false);
                    // First handle the prefix change for the target namespace
                    if (tnsPrefixChanged) {
                        prefixMapping.remove(origTNSPrefix);
                        UpdateNamespaceInformationCommand command = new UpdateNamespaceInformationCommand(Messages._UI_ACTION_NAMESPACE_INFORMATION_CHANGE, xsdSchema, newTNSPrefix, targetNamespace);
                        command.execute();
                        xsdSchema.update();
                    }
                    // Second, handle the target namespace change
                    if (targetNamespaceChanged) {
                        // set the targetNamespace attribute
                        xsdSchema.setTargetNamespace(dialog.getTargetNamespace());
                        TargetNamespaceChangeHandler targetNamespaceChangeHandler = new TargetNamespaceChangeHandler(xsdSchema, targetNamespace, dialog.getTargetNamespace());
                        targetNamespaceChangeHandler.resolve();
                    }
                    if (prefixMapping.size() > 0) {
                        for (Iterator iter = prefixMapping.keySet().iterator(); iter.hasNext(); ) {
                            String oldPrefix = (String) iter.next();
                            String newPrefix = (String) prefixMapping.get(oldPrefix);
                            // Now update any references to this old prefix in the schema
                            // with the value of the new prefix
                            String ns = (String) origPrefixMap.get(oldPrefix);
                            // $NON-NLS-1$
                            SchemaPrefixChangeHandler spch = new SchemaPrefixChangeHandler(xsdSchema, newPrefix, ns != null ? ns : "");
                            spch.resolve();
                        }
                    }
                    namespaceInfoManager.removeNamespaceInfo(element);
                    namespaceInfoManager.addNamespaceInfo(element, newInfoList, false);
                    // Third, handle the schema for schema prefix change
                    if (xsdPrefixChanged) {
                        if (xsdPrefix != null && xsdPrefix.length() == 0) {
                            xsdSchema.setSchemaForSchemaQNamePrefix(null);
                        } else {
                            xsdSchema.setSchemaForSchemaQNamePrefix(xsdPrefix);
                        }
                        namespaceInfoManager.removeNamespaceInfo(element);
                        namespaceInfoManager.addNamespaceInfo(element, newInfoList, false);
                        xsdSchema.setIncrementalUpdate(true);
                        // Now change the references to any schema types/components ie. string --> xs:string
                        SchemaPrefixChangeHandler spch = new SchemaPrefixChangeHandler(xsdSchema, xsdPrefix);
                        spch.resolve();
                        // Change the prefix for all schema components
                        updateAllNodes(element, xsdPrefix);
                        // $NON-NLS-1$
                        prefixMapping.remove(origXSDPrefix != null ? origXSDPrefix : "");
                    }
                    xsdSchema.setIncrementalUpdate(true);
                } catch (Exception e) {
                } finally {
                    xsdSchema.update();
                    doc.getModel().endRecording(this);
                }
            }
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) Hashtable(java.util.Hashtable) Element(org.w3c.dom.Element) TargetNamespaceChangeHandler(org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler) XSDEditSchemaInfoDialog(org.eclipse.wst.xsd.ui.internal.widgets.XSDEditSchemaInfoDialog) DocumentImpl(org.eclipse.wst.xml.core.internal.document.DocumentImpl) Shell(org.eclipse.swt.widgets.Shell) Iterator(java.util.Iterator) NodeList(org.w3c.dom.NodeList) List(java.util.List) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo) SchemaPrefixChangeHandler(org.eclipse.wst.xsd.ui.internal.nsedit.SchemaPrefixChangeHandler) Map(java.util.Map) UpdateNamespaceInformationCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateNamespaceInformationCommand)

Example 27 with NamespaceInfo

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

the class XPathUIPlugin method getNamespaceInfo.

public List<NamespaceInfo> getNamespaceInfo(IDOMDocument document) {
    XPathUIPlugin plugin = XPathUIPlugin.getDefault();
    String modelID = document.getModel().getId();
    Map<String, List<NamespaceInfo>> namespaceInfo = plugin.getNamespaceInfo();
    List<NamespaceInfo> info = namespaceInfo.get(modelID);
    if (info == null) {
        if (document.getDocumentElement() != null) {
            info = new ArrayList<NamespaceInfo>();
            NamespaceTable namespaceTable = new NamespaceTable(document);
            namespaceTable.visitElement(document.getDocumentElement());
            Collection<?> namespaces = namespaceTable.getNamespaceInfoCollection();
            info.addAll((Collection<NamespaceInfo>) namespaces);
            namespaceInfo.put(modelID, info);
            plugin.setNamespaceInfo(namespaceInfo);
            ensureDefault(namespaceTable, "xs", "http://www.w3.org/2001/XMLSchema");
        }
    }
    return info;
}
Also used : NamespaceTable(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable) ArrayList(java.util.ArrayList) List(java.util.List) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Example 28 with NamespaceInfo

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

the class XMLAssociationProvider method getCMDocumentReferences.

/**
 * This method returns a list of CMDocumentReferences associated with a particular node or subtree
 */
public List getCMDocumentReferences(Node node, boolean deep) {
    List result = new ArrayList();
    Document document = (node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument();
    DocumentType doctype = document.getDoctype();
    // so that the implict DTD (if any) can be used
    if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null)) {
        String uri = resolveGrammarURI(document, doctype.getPublicId(), doctype.getSystemId());
        result.add(new CMDocumentReferenceImpl(doctype.getPublicId(), uri));
    } else if (getImplictDoctype(document) != null) {
        String[] implicitDoctype = getImplictDoctype(document);
        String uri = resolveGrammarURI(document, implicitDoctype[0], implicitDoctype[1]);
        result.add(new CMDocumentReferenceImpl(implicitDoctype[0], uri));
    } else {
        NamespaceTable namespaceTable = new NamespaceTable(document);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            namespaceTable.addElement((Element) node);
        }
        if (deep) {
            addChildElementsToNamespaceTable(node, namespaceTable);
        }
        List list = namespaceTable.getNamespaceInfoList();
        for (Iterator i = list.iterator(); i.hasNext(); ) {
            NamespaceInfo info = (NamespaceInfo) i.next();
            String uri = resolveGrammarURI(document, info.uri, info.locationHint);
            result.add(new CMDocumentReferenceImpl(info.uri, uri));
        }
    }
    return result;
}
Also used : NamespaceTable(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) DocumentType(org.w3c.dom.DocumentType) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Example 29 with NamespaceInfo

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

the class XSDSchemaSection method doHandleEvent.

public void doHandleEvent(Event event) {
    // $NON-NLS-1$
    errorText.setText("");
    String prefixValue = prefixText.getText();
    String tnsValue = targetNamespaceText.getText();
    Element element = xsdSchema.getElement();
    TypesHelper helper = new TypesHelper(xsdSchema);
    String currentPrefix = helper.getPrefix(element.getAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE), false);
    String currentNamespace = xsdSchema.getTargetNamespace();
    if (tnsValue.trim().length() == 0) {
        if (prefixValue.trim().length() > 0) {
            // $NON-NLS-1$
            errorText.setText(XSDEditorPlugin.getXSDString("_ERROR_TARGET_NAMESPACE_AND_PREFIX"));
            int length = errorText.getText().length();
            red = new Color(null, 255, 0, 0);
            StyleRange style = new StyleRange(0, length, red, targetNamespaceText.getBackground());
            errorText.setStyleRange(style);
            return;
        }
    }
    if (event.widget == prefixText) {
        // widget loses focus.
        if (prefixValue.equals(currentPrefix))
            return;
        updateNamespaceInfo(prefixValue, tnsValue);
    } else if (event.widget == targetNamespaceText) {
        // widget loses focus.
        if (tnsValue.equals(currentNamespace))
            return;
        DOMNamespaceInfoManager namespaceInfoManager = new DOMNamespaceInfoManager();
        List namespaceInfoList = namespaceInfoManager.getNamespaceInfoList(xsdSchema.getElement());
        Element xsdSchemaElement = xsdSchema.getElement();
        DocumentImpl doc = (DocumentImpl) xsdSchemaElement.getOwnerDocument();
        try {
            doc.getModel().beginRecording(this, XSDEditorPlugin.getXSDString("_UI_NAMESPACE_CHANGE"));
            if (// remove the targetNamespace attribute
            tnsValue.trim().length() == 0) {
                xsdSchemaElement.removeAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE);
                return;
            }
            // Now replace the namespace for the xmlns entry
            for (Iterator i = namespaceInfoList.iterator(); i.hasNext(); ) {
                NamespaceInfo info = (NamespaceInfo) i.next();
                if (info.uri.equals(currentNamespace)) {
                    info.uri = tnsValue;
                }
            }
            xsdSchema.setIncrementalUpdate(false);
            // set the new xmlns entries
            namespaceInfoManager.removeNamespaceInfo(element);
            namespaceInfoManager.addNamespaceInfo(element, namespaceInfoList, false);
            xsdSchema.setIncrementalUpdate(true);
            // set the targetNamespace attribute
            xsdSchema.setTargetNamespace(tnsValue);
            TargetNamespaceChangeHandler targetNamespaceChangeHandler = new TargetNamespaceChangeHandler(xsdSchema, currentNamespace, tnsValue);
            targetNamespaceChangeHandler.resolve();
        } catch (Exception e) {
        } finally {
            try {
                xsdSchema.update();
            } finally {
                doc.getModel().endRecording(this);
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) TargetNamespaceChangeHandler(org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler) DocumentImpl(org.eclipse.wst.xml.core.internal.document.DocumentImpl) TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) DOMNamespaceInfoManager(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceInfoManager) Iterator(java.util.Iterator) List(java.util.List) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Example 30 with NamespaceInfo

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

the class AbstractXSLContentAssistRequest method getNamespaces.

protected Collection<NamespaceInfo> getNamespaces(IDOMElement element) {
    NamespaceTable table = new NamespaceTable(element.getOwnerDocument());
    table.visitElement(element);
    Collection<NamespaceInfo> namespaceInfoList = table.getNamespaceInfoCollection();
    return namespaceInfoList;
}
Also used : NamespaceTable(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Aggregations

NamespaceInfo (org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)30 List (java.util.List)9 Iterator (java.util.Iterator)8 Element (org.w3c.dom.Element)6 NamespaceTable (org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable)5 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4 Map (java.util.Map)4 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)4 IFile (org.eclipse.core.resources.IFile)3 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)3 NodeList (org.w3c.dom.NodeList)3 Path (org.eclipse.core.runtime.Path)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 Shell (org.eclipse.swt.widgets.Shell)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IPathEditorInput (org.eclipse.ui.IPathEditorInput)2 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)2 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)2