示例#1
0
  public static INamespaceDefinition getDefaultNamespaceDefinition() {
    INamespaceDefinitionResolver definitionResolver =
        BeansCorePlugin.getNamespaceDefinitionResolver(null);
    org.springframework.ide.eclipse.beans.core.model.INamespaceDefinition namespaceDefinition =
        definitionResolver.resolveNamespaceDefinition(DEFAULT_NAMESPACE_URI);

    IExtensionPoint point =
        Platform.getExtensionRegistry().getExtensionPoint(NAMESPACES_EXTENSION_POINT);
    if (point != null) {
      String namespaceURI = DEFAULT_NAMESPACE_URI;
      for (IExtension extension : point.getExtensions()) {
        for (IConfigurationElement config : extension.getConfigurationElements()) {
          if (namespaceURI.equals(config.getAttribute("uri"))) {

            String prefix = config.getAttribute("prefix");
            if (!StringUtils.hasText(prefix) && namespaceDefinition != null) {
              prefix = namespaceDefinition.getPrefix();
            }
            String schemaLocation = config.getAttribute("defaultSchemaLocation");
            if (!StringUtils.hasText(schemaLocation) && namespaceDefinition != null) {
              schemaLocation = namespaceDefinition.getDefaultSchemaLocation();
            }
            String uri = config.getAttribute("uri");
            IImageAccessor image = null;
            if (config.getAttribute("icon") != null) {
              String ns = config.getDeclaringExtension().getNamespaceIdentifier();
              String icon = config.getAttribute("icon");
              image = new DefaultImageAccessor(ns, icon);
            } else if (namespaceDefinition != null) {
              image = new NamespaceDefinitionImageAccessor(namespaceDefinition);
            }
            return new DefaultNamespaceDefinition(
                prefix, uri, schemaLocation, namespaceDefinition.getUriMapping(), image);
          }
        }
      }
    }

    if (namespaceDefinition != null) {
      return new DefaultNamespaceDefinition(
          namespaceDefinition.getPrefix(),
          namespaceDefinition.getNamespaceUri(),
          namespaceDefinition.getDefaultSchemaLocation(),
          namespaceDefinition.getUriMapping(),
          new NamespaceDefinitionImageAccessor(namespaceDefinition));
    }
    return null;
  }
示例#2
0
  protected static List<INamespaceDefinition> getNamespaceDefinitions(IProject project) {
    List<INamespaceDefinition> namespaceDefinitions = new ArrayList<INamespaceDefinition>();

    INamespaceDefinitionResolver definitionResolver =
        BeansCorePlugin.getNamespaceDefinitionResolver(project);
    Set<org.springframework.ide.eclipse.beans.core.model.INamespaceDefinition>
        detectedNamespaceDefinitions = definitionResolver.getNamespaceDefinitions();

    IExtensionPoint point =
        Platform.getExtensionRegistry().getExtensionPoint(NAMESPACES_EXTENSION_POINT);
    if (point != null
        && !org.springframework.ide.eclipse.beans.core.namespaces.NamespaceUtils
            .useNamespacesFromClasspath(project)) {
      for (IExtension extension : point.getExtensions()) {
        for (IConfigurationElement config : extension.getConfigurationElements()) {
          String uri = config.getAttribute("uri");
          org.springframework.ide.eclipse.beans.core.model.INamespaceDefinition
              namespaceDefinition = definitionResolver.resolveNamespaceDefinition(uri);
          detectedNamespaceDefinitions.remove(namespaceDefinition);

          String prefix = config.getAttribute("prefix");
          if (!StringUtils.hasText(prefix) && namespaceDefinition != null) {
            prefix = namespaceDefinition.getPrefix();
          }
          String schemaLocation = config.getAttribute("defaultSchemaLocation");
          if (!StringUtils.hasText(schemaLocation) && namespaceDefinition != null) {
            schemaLocation = namespaceDefinition.getDefaultSchemaLocation();
          }
          IImageAccessor image = null;
          if (config.getAttribute("icon") != null) {
            String ns = config.getDeclaringExtension().getNamespaceIdentifier();
            String icon = config.getAttribute("icon");
            image = new DefaultImageAccessor(ns, icon);
          } else if (namespaceDefinition != null) {
            image = new NamespaceDefinitionImageAccessor(namespaceDefinition);
          }

          DefaultNamespaceDefinition def = null;
          if (namespaceDefinition != null) {
            def =
                new DefaultNamespaceDefinition(
                    prefix, uri, schemaLocation, namespaceDefinition.getUriMapping(), image);
          } else {
            def =
                new DefaultNamespaceDefinition(
                    prefix, uri, schemaLocation, new Properties(), image);
          }

          // get schema locations from nested child elements
          IConfigurationElement[] schemaLocationConfigElements =
              config.getChildren("schemaLocation");
          for (IConfigurationElement schemaLocationConfigElement : schemaLocationConfigElements) {
            def.addSchemaLocation(schemaLocationConfigElement.getAttribute("url"));
          }
          if (def.getSchemaLocations().size() == 0 && namespaceDefinition != null) {
            def.getSchemaLocations().addAll(namespaceDefinition.getSchemaLocations());
          }

          namespaceDefinitions.add(def);
        }
      }
    }

    for (org.springframework.ide.eclipse.beans.core.model.INamespaceDefinition namespaceDefinition :
        detectedNamespaceDefinitions) {
      DefaultNamespaceDefinition def =
          new DefaultNamespaceDefinition(
              namespaceDefinition.getPrefix(),
              namespaceDefinition.getNamespaceUri(),
              namespaceDefinition.getDefaultSchemaLocation(),
              namespaceDefinition.getUriMapping(),
              new NamespaceDefinitionImageAccessor(namespaceDefinition));
      def.getSchemaLocations().addAll(namespaceDefinition.getSchemaLocations());
      namespaceDefinitions.add(def);
    }

    boolean foundPNamespace = false;
    boolean foundCNamespace = false;
    INamespaceDefinition defaultNamespace = null;

    // Remove the tool namespace as we don't want to surface on the UI
    for (INamespaceDefinition definition :
        new ArrayList<INamespaceDefinition>(namespaceDefinitions)) {
      if (TOOLS_NAMESPACE_URI.equals(definition.getNamespaceURI())) {
        namespaceDefinitions.remove(definition);
      } else if (DEFAULT_NAMESPACE_URI.equals(definition.getNamespaceURI())) {
        defaultNamespace = definition;
      } else if (P_NAMESPACE_URI.equals(definition.getNamespaceURI())) {
        foundPNamespace = true;
      } else if (C_NAMESPACE_URI.equals(definition.getNamespaceURI())) {
        foundCNamespace = true;
      }
    }

    if (!foundPNamespace && defaultNamespace != null) {
      // Add in p-Namespace if we found the default namespace
      namespaceDefinitions.add(P_NAMESPACE_DEFINITION);
    }
    if (!foundCNamespace && defaultNamespace != null) {
      // Add in c-Namespace if we found the default namespace
      // && is Spring 3.1 or greater
      Set<String> locations = defaultNamespace.getSchemaLocations();
      for (String locationUri : locations) {
        if (isSpring31(locationUri)) {
          namespaceDefinitions.add(C_NAMESPACE_DEFINITION);
          break;
        }
      }
    }

    Collections.sort(
        namespaceDefinitions,
        new Comparator<INamespaceDefinition>() {
          public int compare(INamespaceDefinition o1, INamespaceDefinition o2) {
            if (o1 != null
                && o1.getDefaultNamespacePrefix() != null
                && o2 != null
                && o2.getDefaultNamespacePrefix() != null) {
              return o1.getDefaultNamespacePrefix().compareTo(o2.getDefaultNamespacePrefix());
            }
            return 0;
          }
        });

    return namespaceDefinitions;
  }