Package de.innovationgate.eclipse.editors.config

Source Code of de.innovationgate.eclipse.editors.config.TMLTagDefinitions

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.config;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.wga.common.beans.csconfig.v1.Version;
import de.innovationgate.wga.model.VersionCompliance;
import de.innovationgate.wga.model.WGADesignConfigurationModel;

public class TMLTagDefinitions {

  private Set<TMLAttributeGroup> _attributeGroups = new HashSet<TMLAttributeGroup>();
  private Set<TMLTag> _tags = new HashSet<TMLTag>();

  // attribute groups mapped by name
  private transient Map<String, TMLAttributeGroup> _attributeGroupsByName = new HashMap<String, TMLAttributeGroup>();

  // tags mapped by name
  private transient Map<String, TMLTag> _tagsByName = new HashMap<String, TMLTag>();

  private static Map<String, TMLTagDefinitions> _instances = new HashMap<String, TMLTagDefinitions>();
 
  private Version _wgaVersion = null;

  TMLTagDefinitions() {

  }

  public static TMLTagDefinitions getInstance(VersionCompliance versionCompliance) {
    if (_instances.containsKey(versionCompliance.getKey())) {
      return _instances.get(versionCompliance.getKey());
    } else {
      Version wgaVersion = (Version) WGADesignConfigurationModel.VERSIONCOMPLIANCE_TO_WGA_VERSION.get(versionCompliance.getKey());
      String tmlTagDefinitionsFileName = "tmlTagDefinitions_50";

      if (wgaVersion != null) {   
        tmlTagDefinitionsFileName = "tmlTagDefinitions_" + Integer.toString(wgaVersion.getMajorVersion()) + Integer.toString(wgaVersion.getMinorVersion());
      }

      TMLTagDefinitions instance;
      InputStreamReader reader = null;
      try {
        reader = new InputStreamReader(Plugin.getDefault().getResourceAsStream("resources/wga/" + tmlTagDefinitionsFileName + ".xml"));
        instance = TMLTagDefinitions.fromXML(reader);
        instance.setWgaVersion(wgaVersion);
      } catch (Exception e) {
        Plugin.getDefault().logError("Unable to deserialize tml tag definitions", e);
        instance = new TMLTagDefinitions();
      } finally {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
     
      Iterator<TMLTag> tags = instance.getTags().iterator();
      while (tags.hasNext()) {
        tags.next().setTagDefinitions(instance);
      }
     
      _instances.put(versionCompliance.getKey(), instance);
     
      return instance;
    }
  }

  void addTag(TMLTag tag) {
    _tags.add(tag);
  }

  void addAttributeGroup(TMLAttributeGroup group) {
    _attributeGroups.add(group);
  }

  public static Map<String, String> getXStreamAliasFields() {
    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put("_attributeGroups", "attributeGroups");
    aliases.put("_tags", "tags");
    return aliases;
  }

  public static void toXML(TMLTagDefinitions definitions, Writer out) {
    createXStream().toXML(definitions, out);
  }

  public static TMLTagDefinitions fromXML(Reader in) {
    return (TMLTagDefinitions) createXStream().fromXML(in);
  }

  private static XStream createXStream() {
    XStream xstream = new XStream(new DomDriver());
    xstream.setClassLoader(TMLTagDefinitions.class.getClassLoader());
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.alias("tagDefinitions", TMLTagDefinitions.class);
    xstream.alias("tag", TMLTag.class);
    xstream.alias("attribute", TMLTagAttribute.class);
    xstream.alias("attributeGroup", TMLAttributeGroup.class);

    addXStreamAliasFields(xstream, getXStreamAliasFields(), TMLTagDefinitions.class);
    addXStreamAliasFields(xstream, TMLAttributeGroup.getXStreamAliasFields(), TMLAttributeGroup.class);
    addXStreamAliasFields(xstream, TMLTag.getXStreamAliasFields(), TMLTag.class);
    addXStreamAliasFields(xstream, TMLTagAttribute.getXStreamAliasFields(), TMLTagAttribute.class);

    return xstream;
  }

  @SuppressWarnings("unchecked")
  private static void addXStreamAliasFields(XStream xstream, Map<String, String> aliases, Class type) {
    Iterator<String> fields = aliases.keySet().iterator();
    while (fields.hasNext()) {
      String field = fields.next();
      String alias = aliases.get(field);
      xstream.aliasField(alias, type, field);
    }
  }

  public Set<TMLTag> getTagsByPrefix(String prefix) {
    Set<TMLTag> filtered = new HashSet<TMLTag>();
    Iterator<TMLTag> tags = _tags.iterator();
    while (tags.hasNext()) {
      TMLTag tag = tags.next();
      if (tag.getName().startsWith(prefix)) {
        filtered.add(tag);
      } else {
        Iterator<String> aliases = tag.getAliases().iterator();
        while (aliases.hasNext()) {
          String alias = aliases.next();
          if (alias.startsWith(prefix)) {
            filtered.add(tag);
          }
        }

      }
    }
    return filtered;
  }

  public Set<TMLTag> getTags() {
    Set<TMLTag> copy = new HashSet<TMLTag>();
    copy.addAll(_tags);
    return copy;
  }

  public TMLTag getTagByName(String name) {
        Version wgaVersion = getWgaVersion();
    if (wgaVersion != null && wgaVersion.isAtLeast(4, 1)) {
      if (name.matches("\\[.*\\]")) {
        name = "include";
      }
    }
    Iterator<TMLTag> tags = _tags.iterator();
    while (tags.hasNext()) {
      TMLTag tag = tags.next();
      if (tag.getName().equals(name) || tag.getAliases().contains(name)) {
        return tag;
      }
    }
     
    return null;
  }

  public TMLAttributeGroup getAttributeGroupByName(String name) {
    Iterator<TMLAttributeGroup> groups = _attributeGroups.iterator();
    while (groups.hasNext()) {
      TMLAttributeGroup group = groups.next();
      if (group.getName().equals(name)) {
        return group;
      }
    }
    return null;
  }

  private Version getWgaVersion() {
    return _wgaVersion;
  }

  private void setWgaVersion(Version wgaVersion) {
    _wgaVersion = wgaVersion;
  }

}
TOP

Related Classes of de.innovationgate.eclipse.editors.config.TMLTagDefinitions

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.