Search in sources :

Example 1 with TypesHelper

use of org.eclipse.wst.xsd.ui.internal.util.TypesHelper in project webtools.sourceediting by eclipse.

the class UpdateNamespaceInformationCommand method execute.

public void execute() {
    ensureSchemaElement(xsdSchema);
    Element element = xsdSchema.getElement();
    try {
        // DocumentImpl doc = (DocumentImpl) element.getOwnerDocument();
        String modelTargetNamespace = xsdSchema.getTargetNamespace();
        String oldNamespace = xsdSchema.getTargetNamespace();
        TypesHelper helper = new TypesHelper(xsdSchema);
        String oldPrefix = helper.getPrefix(element.getAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE), false);
        if (modelTargetNamespace == null) {
            // $NON-NLS-1$
            modelTargetNamespace = "";
        }
        String targetNamespace = newTargetNamespace.trim();
        String prefix = newPrefix.trim();
        if (!validatePrefix(prefix) || !validateTargetNamespace(targetNamespace)) {
            return;
        }
        if (prefix.length() > 0 && targetNamespace.length() == 0) {
            // can't have blank targetnamespace and yet specify a prefix
            return;
        }
        // doc.getModel().beginRecording(this, XSDEditorPlugin.getXSDString("_UI_TARGETNAMESPACE_CHANGE")); //$NON-NLS-1$
        beginRecording(element);
        String xsdForXSDPrefix = xsdSchema.getSchemaForSchemaQNamePrefix();
        Map map = xsdSchema.getQNamePrefixToNamespaceMap();
        // for schema for schema
        if (prefix.length() == 0) {
            // and update ALL nodes with this prefix
            if (xsdForXSDPrefix == null || (xsdForXSDPrefix != null && xsdForXSDPrefix.trim().length() == 0)) {
                // get preference prefix
                xsdForXSDPrefix = XSDEditorPlugin.getPlugin().getXMLSchemaPrefix();
                // get a unique prefix by checking what's in the map
                xsdForXSDPrefix = getUniqueSchemaForSchemaPrefix(xsdForXSDPrefix, map);
                // $NON-NLS-1$
                element.setAttribute("xmlns:" + xsdForXSDPrefix, XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
                updateAllNodes(element, xsdForXSDPrefix);
                // remove the old xmlns attribute for the schema for schema
                if (// $NON-NLS-1$
                element.hasAttribute("xmlns") && // $NON-NLS-1$
                element.getAttribute("xmlns").equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001)) {
                    // $NON-NLS-1$
                    element.removeAttribute("xmlns");
                }
            }
        }
        if (targetNamespace.length() > 0 || (targetNamespace.length() == 0 && prefix.length() == 0)) {
            // clean up the old prefix for this schema
            if (oldPrefix != null && oldPrefix.length() > 0) {
                // $NON-NLS-1$
                element.removeAttribute("xmlns:" + oldPrefix);
            // element.setAttribute("xmlns:" + prefix, targetNamespace);
            // java.util.Map prefixToNameSpaceMap =
            // xsdSchema.getQNamePrefixToNamespaceMap();
            // prefixToNameSpaceMap.remove(oldPrefix);
            } else // if no prefix
            {
                if (// $NON-NLS-1$
                element.hasAttribute("xmlns")) {
                    if (// $NON-NLS-1$
                    !element.getAttribute("xmlns").equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001)) {
                        // $NON-NLS-1$
                        element.removeAttribute("xmlns");
                    }
                }
            }
        }
        if (targetNamespace.length() > 0) {
            if (!modelTargetNamespace.equals(targetNamespace)) {
                element.setAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE, targetNamespace);
            }
            // now set the new xmlns:prefix attribute
            if (prefix.length() > 0) {
                // $NON-NLS-1$
                element.setAttribute("xmlns:" + prefix, targetNamespace);
            } else {
                // $NON-NLS-1$
                element.setAttribute("xmlns", targetNamespace);
            }
        // set the targetNamespace attribute
        } else // else targetNamespace is blank
        {
            if (prefix.length() == 0) {
                element.removeAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE);
            }
        }
        // do our own referential integrity
        TargetNamespaceChangeHandler targetNamespaceChangeHandler = new TargetNamespaceChangeHandler(xsdSchema, oldNamespace, targetNamespace);
        targetNamespaceChangeHandler.resolve();
        updateElement(xsdSchema);
    // doc.getModel().endRecording(this);
    } finally {
        endRecording();
    }
}
Also used : TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) Element(org.w3c.dom.Element) TargetNamespaceChangeHandler(org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler) Map(java.util.Map)

Example 2 with TypesHelper

use of org.eclipse.wst.xsd.ui.internal.util.TypesHelper in project webtools.sourceediting by eclipse.

the class XSDAttributeGroupDefinitionSection method setInput.

public void setInput(IWorkbenchPart part, ISelection selection) {
    super.setInput(part, selection);
    init();
    relayout();
    if (isReference) {
        TypesHelper helper = new TypesHelper(xsdSchema);
        List items = new ArrayList();
        items = helper.getGlobalAttributeGroups();
        // $NON-NLS-1$
        items.add(0, "");
        refCombo.setItems((String[]) items.toArray(new String[0]));
    }
}
Also used : TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with TypesHelper

use of org.eclipse.wst.xsd.ui.internal.util.TypesHelper in project webtools.sourceediting by eclipse.

the class XSDImportSection method refresh.

public void refresh() {
    setListenerEnabled(false);
    errorText.setText("");
    Element element = null;
    if (input instanceof XSDImport) {
        element = ((XSDImport) input).getElement();
        // $NON-NLS-1$
        String namespace = element.getAttribute("namespace");
        // $NON-NLS-1$
        String schemaLocation = element.getAttribute("schemaLocation");
        TypesHelper helper = new TypesHelper(xsdSchema);
        String prefix = helper.getPrefix(element.getAttribute(XSDConstants.NAMESPACE_ATTRIBUTE), false);
        if (namespace == null) {
            // $NON-NLS-1$
            namespace = "";
        }
        if (prefix == null) {
            // $NON-NLS-1$
            prefix = "";
        }
        if (schemaLocation == null) {
            // $NON-NLS-1$
            schemaLocation = "";
        }
        namespaceText.setText(namespace);
        prefixText.setText(prefix);
        schemaLocationText.setText(schemaLocation);
        oldPrefixValue = prefixText.getText();
    }
    setListenerEnabled(true);
}
Also used : TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) Element(org.w3c.dom.Element) XSDImport(org.eclipse.xsd.XSDImport)

Example 4 with TypesHelper

use of org.eclipse.wst.xsd.ui.internal.util.TypesHelper in project webtools.sourceediting by eclipse.

the class XSDSchemaSection method refresh.

/*
   * @see org.eclipse.wst.common.ui.properties.internal.provisional.view.ITabbedPropertySection#refresh()
   */
public void refresh() {
    super.refresh();
    setListenerEnabled(false);
    Element element = xsdSchema.getElement();
    if (element != null) {
        // Handle prefixText
        TypesHelper helper = new TypesHelper(xsdSchema);
        String aPrefix = helper.getPrefix(element.getAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE), false);
        if (aPrefix != null && aPrefix.length() > 0) {
            prefixText.setText(aPrefix);
        } else {
            // $NON-NLS-1$
            prefixText.setText("");
        }
        if (element.hasAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE)) {
            String tns = element.getAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE);
            String processedString = TextProcessor.process(tns);
            targetNamespaceText.setText(processedString);
        } else {
            // $NON-NLS-1$
            targetNamespaceText.setText("");
        }
        // $NON-NLS-1$
        errorText.setText("");
    }
    setListenerEnabled(true);
}
Also used : TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) Element(org.w3c.dom.Element)

Example 5 with TypesHelper

use of org.eclipse.wst.xsd.ui.internal.util.TypesHelper in project webtools.sourceediting by eclipse.

the class XSDDirectivesSchemaLocationUpdater method updateSchemaLocation.

/**
 * Modifies the schema location by opening the schema location dialog and
 * processing the results. This method refactors the code in
 * XSDImportSection$widgetSelected and SchemaLocationSection$widgetSelected
 * and the processing in handleSchemaLocationChange()
 */
public static void updateSchemaLocation(XSDSchema xsdSchema, Object selection, boolean isInclude) {
    Shell shell = Display.getCurrent().getActiveShell();
    IFile currentIFile = null;
    IEditorInput editorInput = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput();
    ViewerFilter filter;
    if (editorInput instanceof IFileEditorInput) {
        currentIFile = ((IFileEditorInput) editorInput).getFile();
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, new IFile[] { currentIFile }, null);
    } else {
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, null, null);
    }
    XSDSelectIncludeFileWizard fileSelectWizard = new // $NON-NLS-1$
    XSDSelectIncludeFileWizard(// $NON-NLS-1$
    xsdSchema, // $NON-NLS-1$
    isInclude, // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_SCHEMA"), // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_DESC"), filter, new StructuredSelection(selection));
    WizardDialog wizardDialog = new WizardDialog(shell, fileSelectWizard);
    wizardDialog.create();
    wizardDialog.setBlockOnOpen(true);
    int result = wizardDialog.open();
    if (result == Window.OK) {
        IFile selectedIFile = fileSelectWizard.getResultFile();
        String schemaFileString;
        if (selectedIFile != null && currentIFile != null) {
            schemaFileString = URIHelper.getRelativeURI(selectedIFile.getLocation(), currentIFile.getLocation());
        } else if (selectedIFile != null && currentIFile == null) {
            schemaFileString = selectedIFile.getLocationURI().toString();
        } else {
            schemaFileString = fileSelectWizard.getURL();
        }
        // $NON-NLS-1$
        String attributeSchemaLocation = "schemaLocation";
        if (selection instanceof XSDImport) {
            XSDImport xsdImport = (XSDImport) selection;
            // $NON-NLS-1$
            xsdImport.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
            String namespace = fileSelectWizard.getNamespace();
            if (namespace == null)
                // $NON-NLS-1$
                namespace = "";
            XSDSchema externalSchema = fileSelectWizard.getExternalSchema();
            java.util.Map map = xsdSchema.getQNamePrefixToNamespaceMap();
            Element schemaElement = xsdSchema.getElement();
            // update the xmlns in the schema element first, and then update the
            // import element next so that the last change will be in the import element. This keeps the
            // selection on the import element
            TypesHelper helper = new TypesHelper(externalSchema);
            String prefix = helper.getPrefix(namespace, false);
            if (map.containsKey(prefix)) {
                prefix = null;
            }
            if (prefix == null || (prefix != null && prefix.length() == 0)) {
                // $NON-NLS-1$
                StringBuffer newPrefix = new StringBuffer("pref");
                int prefixExtension = 1;
                while (map.containsKey(newPrefix.toString()) && prefixExtension < 100) {
                    // $NON-NLS-1$
                    newPrefix = new StringBuffer("pref" + String.valueOf(prefixExtension));
                    prefixExtension++;
                }
                prefix = newPrefix.toString();
            }
            // $NON-NLS-1$
            String attributeNamespace = "namespace";
            if (namespace.length() > 0) {
                // if ns already in map, use its corresponding prefix
                if (map.containsValue(namespace)) {
                    TypesHelper typesHelper = new TypesHelper(xsdSchema);
                    prefix = typesHelper.getPrefix(namespace, false);
                } else // otherwise add to the map
                {
                    // $NON-NLS-1$
                    schemaElement.setAttribute("xmlns:" + prefix, namespace);
                }
                // prefixText.setText(prefix);
                xsdImport.getElement().setAttribute(attributeNamespace, namespace);
            }
            // into the resource set
            if (// redundant
            selection instanceof XSDImportImpl) {
                XSDImportImpl xsdImportImpl = (XSDImportImpl) selection;
                xsdImportImpl.importSchema();
            }
        } else if (selection instanceof XSDInclude) {
            XSDInclude xsdInclude = (XSDInclude) selection;
            xsdInclude.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        } else if (selection instanceof XSDRedefine) {
            XSDRedefine xsdRedefine = (XSDRedefine) selection;
            xsdRedefine.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Element(org.w3c.dom.Element) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Shell(org.eclipse.swt.widgets.Shell) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSelectIncludeFileWizard(org.eclipse.wst.xsd.ui.internal.wizards.XSDSelectIncludeFileWizard) TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XSDImport(org.eclipse.xsd.XSDImport) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Aggregations

TypesHelper (org.eclipse.wst.xsd.ui.internal.util.TypesHelper)8 Element (org.w3c.dom.Element)5 List (java.util.List)3 ArrayList (java.util.ArrayList)2 TargetNamespaceChangeHandler (org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler)2 XSDImport (org.eclipse.xsd.XSDImport)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IFile (org.eclipse.core.resources.IFile)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1 Shell (org.eclipse.swt.widgets.Shell)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IFileEditorInput (org.eclipse.ui.IFileEditorInput)1 DOMNamespaceInfoManager (org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceInfoManager)1 NamespaceInfo (org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)1 DocumentImpl (org.eclipse.wst.xml.core.internal.document.DocumentImpl)1