Package de.innovationgate.eclipse.utils.wga

Source Code of de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper$CaseInsensitivFolderReferenceSearcher

/*******************************************************************************
* 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.utils.wga;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.collections.functors.IfClosure;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.osgi.service.prefs.BackingStoreException;

import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.eclipse.utils.FileUtils;
import de.innovationgate.eclipse.utils.ui.model.TMLFileMetadataModel;
import de.innovationgate.eclipse.utils.ui.model.WGADesignConfigurationModelWrapper;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.schemadef.WGSchemaDefinition;
import de.innovationgate.wga.common.DesignDirectory;
import de.innovationgate.wga.common.beans.csconfig.v1.Version;
import de.innovationgate.wga.model.Encoding;
import de.innovationgate.wga.model.TMLMetadataInfo;
import de.innovationgate.wga.model.VersionCompliance;
import de.innovationgate.wga.model.WGADesignConfigurationModel;

public class WGADesignStructureHelper {

  public static final String SCRIPT_TYPE_CSS = "css";
  public static final String SCRIPT_TYPE_JS = "js";
  public static final String DEFAULT_DESIGN_DEV_LANGUAGE = "de";
  public static final String SCRIPT_TYPE_TMLSCRIPT = "tmlscript"
  public static final List<String> DEFAULT_MEDIA_KEYS = Arrays.asList("html","wml","xml","pdf")

  private IFile _syncInfo;
  private IContainer _designRoot;
  private IFolder _fileContainerRoot;
  private IFolder _scriptsRoot;
  private IFolder _tmlRoot;
  private IFile _csConfig;
    private IFile _schemaDefinition;

  public WGADesignStructureHelper(IFile anyDesignResource) {
    init(determineSyncInfo(anyDesignResource));
  }

  public WGADesignStructureHelper(IContainer designRoot) {
    File designInfoFile = DesignDirectory.getDesignDefinitionFile(designRoot.getLocation().toFile());
    init(designRoot.getFile(new Path(designInfoFile.getName())));
  }

  private void init(IFile syncInfo) {
    _syncInfo = syncInfo;
    _designRoot = _syncInfo.getParent();
    _fileContainerRoot = _designRoot.getFolder(new Path("files"));
    _csConfig = _fileContainerRoot.getFolder("system").getFile("csconfig.xml");
    _scriptsRoot = _designRoot.getFolder(new Path("scripts"));
    _tmlRoot = _designRoot.getFolder(new Path("tml"));
    _schemaDefinition= _fileContainerRoot.getFolder("system").getFile("schema.xml");
  }

  /**
   * determine media key of given tmlfile
   *
   * @param tmlfile
   * @return
   */
  public static String determineMediaKey(IFile tmlFile) {
    if (tmlFile != null) {
      IResource parent = tmlFile.getParent();
      while (parent != null && parent.getParent() != null && !parent.getParent().getName().equals("tml")) {
        parent = parent.getParent();
      }
      if (parent != null) {
        return parent.getName().toLowerCase();
      }
    }
    return null;
  }

  /**
   * determine the syncInfo file based upon a given design container
   *
   * @param file
   * @return design.xml, syncinfo.xml or null
   */
 
  public static IFile determineSyncInfo(IResource resource) {
    if(resource instanceof IContainer){
      return determineSyncInfo((IContainer)resource);
    }
    if(resource instanceof IFile){
      return determineSyncInfo((IFile)resource);
    }
   
    return null;
 
 
 
 
  public static IFile determineSyncInfo(IContainer container) {   
    IContainer parent = container;
    while (parent instanceof IFolder) {
      parent = parent.getParent();
      IFile syncInfo = parent.getFile(new Path(DesignDirectory.DESIGN_DEFINITION_FILE));
      if (syncInfo.exists()) {
        return syncInfo;
      }
      syncInfo = parent.getFile(new Path(DesignDirectory.SYNCINFO_FILE));
      if (syncInfo.exists()) {
        return syncInfo;
      }
    }
    return null;
  }
 
 
  public static IFile determineSyncInfo(IFile file) {
    if (file == null) {
      return null;
    }

    if (file.getName().equals(DesignDirectory.DESIGN_DEFINITION_FILE)) {
      return file;
    } else if (file.getName().equals(DesignDirectory.SYNCINFO_FILE) && !file.getParent().getFile(new Path(DesignDirectory.DESIGN_DEFINITION_FILE)).exists()) {
      return file;
    }
    IContainer parent = file.getParent();
    return determineSyncInfo(parent);
  }
 
  /**
   *
   * @param container
   * @return true/false
   */

 
  public static boolean isDirlinkFolder(IFolder container){
    IFile dirlink = container.getFile(new Path(WGUtils.DIRLINK_FILE))
    return dirlink.exists();
  }
 
 
 
  public static IContainer dirLinkFolderToContainer(IFolder folder){
    File linkedFile = WGUtils.resolveDirLink(folder.getLocation().toFile());
    IContainer[] containers = folder.getWorkspace().getRoot().findContainersForLocationURI(linkedFile.toURI());

    if (containers != null && containers.length > 0) {
      IContainer container = containers[0];
      if (container instanceof IFolder) {
        return container;
      }
    }
   
    return null;
  }
 

  /**
   * checks if the given container is a media key container
   *
   * @param container
   * @return true/false
   */
  public static boolean isMediaKeyContainer(IContainer container) {
    IContainer parent = container.getParent();
    IContainer grandParent = null;
    if (parent != null && parent.isAccessible()) {
      grandParent = parent.getParent();
      if (grandParent != null && isDesignFolder(grandParent)) {
        WGADesignStructureHelper helper = new WGADesignStructureHelper(grandParent);
        return helper.getTmlRoot().equals(parent);
      }
    }
    return false;
  }
 
  /**
   *
   * @param selectedContainer
   * @return
   */
 
  public static String getScriptTypeOf(IContainer selectedContainer) {
    if(selectedContainer instanceof IFolder){
       IResource resourceVar = selectedContainer;
       while(resourceVar!=null && resourceVar instanceof IFolder){
         if(resourceVar.getParent().getName().equals("scripts") &&  isDesignFolder(resourceVar.getParent().getParent())){
           return resourceVar.getName();
         }
         resourceVar = resourceVar.getParent();
      
    }
    return null;
  }
 
 
  /**
   * Checks if the given container is a valid script location
   * @param container
   * @return true/false
   */
  public static boolean isValidScriptLocation(IContainer container){
     if(container instanceof IFolder){
       IResource resourceVar = container;
       while(resourceVar!=null && resourceVar instanceof IFolder){
         if(resourceVar.getParent().getName().equals("scripts") &&  isDesignFolder(resourceVar.getParent().getParent())){
           return true;
         }
         resourceVar = resourceVar.getParent();
       }      
     }  
     return false;
   }
  
 
  /**
   * checks if the given container is a wga design
   *
   * @param container
   * @return true/false
   */
  public static boolean isDesignFolder(IContainer container) {
    if (container != null && !ResourcesPlugin.getWorkspace().getRoot().equals(container)) {
      return isDesignFolder(container.getLocation().toFile());
    } else {
      return false;
    }
  }
 

  /**
   * checks if the given directory is a wga design
   *
   * @param directory
   * @return true/false
   */
  public static boolean isDesignFolder(File directory) {
    if (directory != null && directory.isDirectory()) {
      return new File(directory, DesignDirectory.DESIGN_DEFINITION_FILE).exists() || new File(directory, DesignDirectory.SYNCINFO_FILE).exists();
    } else {
      return false;
    }
  }
 
  /**
   * checks if the given folder is a wga plugin
   *
   * @param folder
   * @return true/false
   */ 
  public static boolean isDesignPluginFolder(IContainer container) {
    if (isDesignFolder(container)){
      WGADesignStructureHelper helper = new WGADesignStructureHelper(container);
      try {
        WGADesignConfigurationModelWrapper model = new WGADesignConfigurationModelWrapper(helper.getSyncInfo());
        return model.hasPluginConfig();
      } catch (IOException e) {
        Activator.getDefault().logError("Cannot parse WGADesignConfigurationModel of file " + container.getLocation(), e);
        return false;       
      }     
    }else
    {
      return false;
    }
  }

  public static boolean hasDesignFolders(IContainer container) {
    if (container instanceof IFolder) {
      IFolder folder = (IFolder) container;
      try {
        for (IResource current : folder.members()) {
          if (current instanceof IContainer) {
            IContainer currentContainer = (IContainer) current;
            if (isDesignFolder(currentContainer)) {
              return true;
            }
          }
        }
      } catch (CoreException e) {
      }
    }

    return false;
  }

  public WGADesignConfigurationModel createModel() throws IOException {
    return new WGADesignConfigurationModelWrapper(_syncInfo);
  }

  /**
   * finds referenced TMLModules e.g. <tml:include ref="::foo">
   *
   * @param referer
   * @param reference
   * @param medium
   * @return IFile or null if reference does not exist
   */
  public static IFile findReferencedTMLModule(IFile referer, String reference, String medium) {

    if (referer != null) {
      WGADesignStructureHelper helper = new WGADesignStructureHelper(referer);   
      String referencePath = helper.computeTMLModuleReferencePath(referer, reference, medium);
      return lookupFileReference(referencePath, helper.getTmlRoot());
    }
    return null;
  }

  public IFile findReferencedTMLModule(String absoluteReference, String medium) {
    String referencePath = computeTMLModuleReferencePath(null, absoluteReference, medium);
    return lookupFileReference(referencePath, getTmlRoot());
  }

  /**
     * find referenced file containers e.g. <tml:url doc="::foo">
     *
     * @param referer
     * @param reference
     * @return IFolder or null if reference does not exist
     */
    public static IFolder findReferencedFileContainer(IFile referer, String reference) {
        if (referer != null) {
            WGADesignStructureHelper helper = new WGADesignStructureHelper(referer);       
            String referencePath = WGADesignStructureHelper.computeFileContainerReferencePath(referer, reference);
            return lookupFolderReference(referencePath, helper.getFileContainerRoot());
        }
        return null;
    }
 
  private static String computeFileContainerReferencePath(IFile referer, String reference) {     
      if (reference.startsWith("::") && referer == null) {
            throw new IllegalArgumentException("Cannot lookup file container reference relativ without referer.");
        }     
        if (referer != null) {
            WGADesignStructureHelper helper = new WGADesignStructureHelper(referer);

            IFolder mediumFolder = helper.getTmlRoot().getFolder(WGADesignStructureHelper.determineMediaKey(referer));
           
            // cut off path up to medium
            int segments = referer.getLocation().matchingFirstSegments(mediumFolder.getLocation());
            IPath relativePathToMediumFolder = referer.getLocation().removeFirstSegments(segments).removeLastSegments(1);

            String referencePath = null;
            reference = reference.toLowerCase();
            if (reference.startsWith("::")) {
                String tmpPath = "/";
                for (int i = 0; i < relativePathToMediumFolder.segmentCount(); i++) {
                    tmpPath += relativePathToMediumFolder.segment(i) + "/";
                }

                referencePath = reference.replace("::", tmpPath);
                referencePath = referencePath.replaceAll(":", "/");
                //referencePath += "." + scriptType;
            } else {
                VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(referer);       
                if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
                    if (reference.endsWith("@base")) {                            
                        IPath path = referer.getFullPath();
                        if (path.toString().contains("/overlay/")) {
                            // this is a base reference in an overlay provider - remove @base marker and add overlay root
                            reference = reference.substring(0, reference.lastIndexOf("@"));               
                            reference = "overlay:" + reference;   
                        }
                    }
                }
               
                referencePath = "/" + reference.replaceAll(":", "/");
                //referencePath += "." + scriptType;
            }
            return referencePath;
        } else {
            return null;
        }     
    }

    /**
   * find referenced script files for given type
   * {@link WGADesignStructureHelper.SCRIPT_TYPE_CSS},
   * {@link WGADesignStructureHelper.SCRIPT_TYPE_JS}
   *
   * @param referer
   * @param reference
   * @param scriptType
   *            {@link WGADesignStructureHelper.SCRIPT_TYPE_CSS},
   *            {@link WGADesignStructureHelper.SCRIPT_TYPE_JS}
   * @return IFile or null if reference does not exist
   */
  public static IFile findReferencedScript(IFile referer, String reference, String scriptType) {
    if (referer != null) {
      String referencePath = computeScriptReferencePath(referer, reference, scriptType);
      WGADesignStructureHelper helper = new WGADesignStructureHelper(referer);
      IFolder folder = helper.getScriptsRoot();
      return lookupFileReference(referencePath, folder);
    }
    return null;
  }

  private static IFile lookupFileReference(String referencePath, IFolder folder) {
    if (referencePath != null) {
      // first try - use unmodified referencePath
      IFile referenceFile = folder.getFile(new Path(referencePath));
      if (referenceFile.exists()) {
        return referenceFile;
      }

      // second try - lowercase referencePath
      referenceFile = folder.getFile(new Path(referencePath.toLowerCase()));
      if (referenceFile.exists()) {
        return referenceFile;
      }

      // third try - case insensitiv match on all files in folder
      CaseInsensitivReferenceSearcher searcher = new CaseInsensitivReferenceSearcher(referencePath, folder);
      try {
        folder.accept(searcher);
      } catch (CoreException e) {
        Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Error searching for file reference.", e));
      }
      return searcher.getReferenceFile();
    } else {
      return null;
    }
  }
 
     private static IFolder lookupFolderReference(String referencePath, IFolder folder) {
          if (referencePath != null) {
              // first try - use unmodified referencePath
              IFolder referenceFolder = folder.getFolder(new Path(referencePath));
              if (referenceFolder.exists()) {
                  return referenceFolder;
              }

              // second try - lowercase referencePath
              referenceFolder = folder.getFolder(new Path(referencePath.toLowerCase()));
              if (referenceFolder.exists()) {
                  return referenceFolder;
              }

              // third try - case insensitiv match on all files in folder
              CaseInsensitivFolderReferenceSearcher searcher = new CaseInsensitivFolderReferenceSearcher(referencePath, folder);
              try {
                  folder.accept(searcher);
              } catch (CoreException e) {
                  Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Error searching for folder reference.", e));
              }
              return searcher.getReferenceFolder();
          } else {
              return null;
          }
      }

  public String computeTMLModuleReferencePath(IFile referer, String reference, String medium) {
    if (reference.startsWith("::") && referer == null) {
      throw new IllegalArgumentException("Cannot lookup TMLModule reference relativ without referer.");
    }
    if (medium == null && referer == null) {
      throw new IllegalArgumentException("Cannot lookup TMLModule reference without referer and unspecified medium.");
    }

    if (referer != null) {
      if (medium == null || medium.trim().equals("")) {
        medium = WGADesignStructureHelper.determineMediaKey(referer);
      }
    }
    IFolder tmlFolder = getTmlRoot();
   
    if (referer != null) {
        VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(referer);   
        if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
            IPath path = referer.getFullPath().makeRelativeTo(getTmlRoot().getFullPath().append(WGADesignStructureHelper.determineMediaKey(referer)));
                if (path.toString().startsWith("overlay/") && !reference.startsWith("::")) {
                    if (reference.toLowerCase().endsWith("@base")) {
                        reference = reference.substring(0, reference.lastIndexOf("@"));       
                    } else  {
                        // this is a reference within an overlay - remove @base marker and add overlay folder to complete the path
                        reference = reference.substring(0, reference.lastIndexOf("@"));               
                        reference = "overlay:" + reference;
                    }
                }
        }
    }
   
    String referencePath = null;

    reference = reference.toLowerCase();
    if (reference.startsWith("::")) {
      int matchingSegments = referer.getLocation().matchingFirstSegments(tmlFolder.getLocation());
      String tmpPath = medium + "/";
      for (int i = matchingSegments + 1; i < referer.getLocation().segmentCount() - 1; i++) {
        tmpPath += referer.getLocation().segment(i) + "/";
      }

      referencePath = reference.replace("::", tmpPath);
      referencePath = referencePath.replaceAll(":", "/");
      referencePath += ".tml";

    } else {
      referencePath = medium + "/" + reference.replaceAll(":", "/");
      referencePath += ".tml";
    }
    return referencePath;
  }

  private static class CaseInsensitivReferenceSearcher implements IResourceVisitor {

    private String _referencePath;
    private IFolder _tmlFolder;
    private IFile _referenceFile;

    public CaseInsensitivReferenceSearcher(String referencePath, IFolder tmlFolder) {
      _referencePath = referencePath;
      _tmlFolder = tmlFolder;
    }

    public boolean visit(IResource resource) throws CoreException {
      if (resource instanceof IFile) {
        int firstEqualSegments = resource.getProjectRelativePath().matchingFirstSegments(_tmlFolder.getProjectRelativePath());
        String pathToCompareTo = resource.getProjectRelativePath().removeFirstSegments(firstEqualSegments).toString();
        if (_referencePath.equalsIgnoreCase(pathToCompareTo)) {
          // found
          _referenceFile = (IFile) resource;
          return false;
        }
      }
      return true;
    }

    public IFile getReferenceFile() {
      return _referenceFile;
    }

  };
 
     private static class CaseInsensitivFolderReferenceSearcher implements IResourceVisitor {

          private String _referencePath;
          private IFolder _tmlFolder;
          private IFolder _referenceFolder;

          public CaseInsensitivFolderReferenceSearcher(String referencePath, IFolder tmlFolder) {
              _referencePath = referencePath;
              _tmlFolder = tmlFolder;
          }

          public boolean visit(IResource resource) throws CoreException {
              if (resource instanceof IFolder) {
                  int firstEqualSegments = resource.getProjectRelativePath().matchingFirstSegments(_tmlFolder.getProjectRelativePath());
                  String pathToCompareTo = resource.getProjectRelativePath().removeFirstSegments(firstEqualSegments).toString();
                  if (_referencePath.equalsIgnoreCase(pathToCompareTo)) {
                      // found
                      _referenceFolder = (IFolder) resource;
                      return false;
                  }
              }
              return true;
          }

          public IFolder getReferenceFolder() {
              return _referenceFolder;
          }

      };

  private static class CaseInsensitivSearcher implements IResourceVisitor {

    private String _name;
    private IResource _resource;

    public CaseInsensitivSearcher(String name) {
      _name = name;
    }

    public boolean visit(IResource resource) throws CoreException {
      if (resource.getName().equalsIgnoreCase(_name)) {
        _resource = resource;
        return false;
      }
      return true;
    }

    public IResource getResource() {
      return _resource;
    }

  };
 
  public String determineDesignEncoding() {
    return determineDesignEncoding(System.getProperty("file.encoding", "UTF-8"));
  }

  private String determineDesignEncoding(String fallBackEncoding) {
    IFile syncInfo = _syncInfo;
    if (syncInfo != null) {
      try {
        WGADesignConfigurationModel model = new WGADesignConfigurationModel(syncInfo.getLocation().toFile());
        Encoding encoding = model.getDesignEncoding();
        if (encoding != null && !encoding.getKey().equals(WGADesignConfigurationModel.STRING_NOT_SET)) {
          return encoding.getKey();
        } else {
          Activator.getDefault().getLog().log(
              new Status(Status.WARNING, Activator.PLUGIN_ID, "Design encoding not set for design '" + syncInfo.getParent().getLocation().toString() + "'. Using platform encoding '"
                  + fallBackEncoding + "'."));
          return fallBackEncoding;
        }
      } catch (IOException e) {
        Activator.getDefault().getLog().log(
            new Status(Status.ERROR, Activator.PLUGIN_ID, "Unable to determine design encoding for design '" + syncInfo.getParent().getLocation().toString()
                + "'. Using platform encoding '" + fallBackEncoding + "'."));
        return fallBackEncoding;
      }
    } else {
      Activator.getDefault().getLog().log(new Status(Status.WARNING, Activator.PLUGIN_ID, "TMLFile is not member of an WGADesign. Using platform encoding '" + fallBackEncoding + "'."));
      return fallBackEncoding;
    }
  }

  public static String computeScriptReferencePath(IFile referer, String reference, String scriptType) {
    if (referer != null) {
      String medium = determineMediaKey(referer);
      if (medium == null) {
        medium = "html";
      }
     
      WGADesignStructureHelper helper = new WGADesignStructureHelper(referer);
      IFolder mediumFolder = helper.getTmlRoot().getFolder(medium);

      // cut off path up to medium
      int segments = referer.getLocation().matchingFirstSegments(mediumFolder.getLocation());
      IPath relativePathToMediumFolder = referer.getLocation().removeFirstSegments(segments).removeLastSegments(1);

      String referencePath = null;
      reference = reference.toLowerCase();
      if (reference.startsWith("::")) {
        String tmpPath = scriptType + "/";
        for (int i = 0; i < relativePathToMediumFolder.segmentCount(); i++) {
          tmpPath += relativePathToMediumFolder.segment(i) + "/";
        }

        referencePath = reference.replace("::", tmpPath);
        referencePath = referencePath.replaceAll(":", "/");
        referencePath += "." + scriptType;
      } else {
          VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(referer);       
          if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
              IPath path = referer.getFullPath();
                    if (path.toString().contains("/overlay/")) {
                        if (reference.endsWith("@base")) {                                                
                        reference = reference.substring(0, reference.lastIndexOf("@"));                   
                    } else {
                        // this is a base reference in an overlay provider - remove @base marker and add overlay root               
                            reference = "overlay:" + reference;
                    }
                }
          }
         
        referencePath = scriptType + "/" + reference.replaceAll(":", "/");
        referencePath += "." + scriptType;
      }
      return referencePath;
    } else {
      return null;
    }
  }

  public IFolder getFileContainer(String containername) {
      /*
    IFolder container = getFileContainerRoot().getFolder(containername);
    if (container.exists()) {
      return container;
    } else {
      // try caseinsensitiv search
      CaseInsensitivSearcher searcher = new CaseInsensitivSearcher(containername);
      try {
        getFileContainerRoot().accept(searcher, IResource.DEPTH_ONE, IResource.FOLDER);
        return (IFolder) searcher.getResource();
      } catch (CoreException e) {
      }
      return null;
    }*/
      String referencePath = computeFileContainerReferencePath(null, containername);
        return lookupFolderReference(referencePath, getFileContainerRoot());
  }

  public Set<IFolder> getFileContainers() {
    final IFolder fFilesFolder = getFileContainerRoot();
    final Set<IFolder> fFileContainers = new HashSet<IFolder>();
    try {
      fFilesFolder.accept(new IResourceVisitor() {

        public boolean visit(IResource resource) throws CoreException {
          if (resource.equals(fFilesFolder)) {
            return true;
          } else if (resource instanceof IFolder) {
            fFileContainers.add((IFolder) resource);
            VersionCompliance compliance = WGADesignStructureHelper.getWGAVersionCompliance(getSyncInfo());
            if (compliance != null && compliance.toWGAVersion() != null && compliance.toWGAVersion().isAtLeast(5,4)) {
                return true;
            } else {
                return false;
            }
          } else {
            return false;
          }
        }

      });
    } catch (CoreException e) {
    }
    return fFileContainers;
  }

  public Set<IFile> getLabelFiles(IFolder labelContainer) throws CoreException {

    Set<IFile> files = new HashSet<IFile>();
    for (IResource innercurrent : labelContainer.members()) {
      if (innercurrent instanceof IFile) {
        IFile currentFile = (IFile) innercurrent;
        if (currentFile.getFileExtension().equals("properties")) {
          files.add(currentFile);
        }

      }
    }
    return files;

  }

  public Set<IFolder> getLabelContainers() throws CoreException {
    IFolder filesFolder = getFileContainerRoot();
    Set<IFolder> containers = new HashSet<IFolder>();
    for (IResource current : filesFolder.members()) {
      if (current instanceof IFolder) {
        IFolder currentFolder = (IFolder) current;
        if (currentFolder.getName().toLowerCase().startsWith("labels_")) {
          containers.add(currentFolder);
        }
      }
    }
    return containers;
  }

  public IFolder getLabelContainer(String language) {
    try {
      return getLabelContainer(language, false);
    } catch (CoreException e) {
      // should happen bc. auto create is off
      return null;
    }
  }

  public IFolder getLabelContainer(String language, boolean createIfMissing) throws CoreException {
    IFolder container = getFileContainer("labels_" + language.toLowerCase());
    if (container == null) {
      container = getFileContainerRoot().getFolder(new Path("labels_" + language.toLowerCase()));
    }
    if (!container.exists()) {
      container.create(false, true, new NullProgressMonitor());
    }
    return container;
  }

  public static Properties loadLabel(IFile labelFile) {

    Properties props = new Properties();
    FileInputStream fileStream = null;
    try {
      // _currentselection.clear();
      if (labelFile != null && labelFile.exists()) {
         fileStream = new FileInputStream(labelFile.getLocation().toFile());
        props.load(fileStream);
      }
    } catch (FileNotFoundException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't find propertyfile : " + labelFile.getLocation(), e));
    } catch (IOException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't read propertyfile : " + labelFile.getLocation(), e));

    }finally{
      if(fileStream!=null){
        try {
          fileStream.close();
        } catch (IOException e) {
        }
      }

    }

    return props;

  }

  /**
   * loads a labelfile from the current design by the given labelfilename
   * (without extension) language is determined by {@link
   * getDevelopmentLanguage()}
   *
   * @param labelFileName
   *            (without extension)
   * @return
   * @throws CoreException
   */
  public Properties loadLabel(String labelFileName) throws CoreException {
    return loadLabel(getLabelFile(labelFileName));
  }

  public IFile getLabelFile(String labelFileName) throws CoreException {
    IFolder labelContainer = getLabelContainer(getDevelopmentLanguage());
    if (labelContainer != null) {
      String completeFilename = labelFileName + ".properties";
      IFile labelFile = labelContainer.getFile(completeFilename);
      if (labelFile.exists()) {
        return labelFile;
      } else {
        // try case insensitiv search
        CaseInsensitivSearcher searcher = new CaseInsensitivSearcher(completeFilename);
        labelContainer.accept(searcher, IResource.DEPTH_ONE, IResource.FILE);
        return (IFile) searcher.getResource();
      }
    } else {
      return null;
    }
  }
 
  public static VersionCompliance getWGAVersionCompliance(IResource resource) {
      return getWGAVersionCompliance(resource, true);
  }
 
  public static boolean isWGAVersionComplianceCompatible(IResource resource) {
      return getWGAVersionCompliance(resource, false) != null;
  }

  private static VersionCompliance getWGAVersionCompliance(IResource resource, boolean fallbackToDefault) { 
    IFile wgaConfFile = WGADesignStructureHelper.determineSyncInfo(resource);   
    VersionCompliance wgaVersionCompliance = null;
    if (wgaConfFile != null) {
      WGADesignConfigurationModel currentWGAConf;
      try {
        currentWGAConf = new WGADesignConfigurationModelWrapper(wgaConfFile);
        wgaVersionCompliance = currentWGAConf.getVersionCompliance();
      } catch (IOException e) {
      }
    }
    if (wgaVersionCompliance == null && fallbackToDefault) {
        Activator.getDefault().logWarning("Unable to determine version compliance for '" + resource.getLocation() + "'. Using default version compliance '" + Activator.DEFAULT_VERSION_COMPLIANCE.getValue() + "'.");
        wgaVersionCompliance = Activator.DEFAULT_VERSION_COMPLIANCE;
    }
    return wgaVersionCompliance;
  }

  public void createLabel(IFile labelFile, String key, String value) {
    // create new file if not exists
    if (!labelFile.exists()) {
      try {
        if (!labelFile.getParent().exists()) {
          if (labelFile.getParent() instanceof IFolder) {
            IFolder parentFolder = (IFolder) labelFile.getParent();
            parentFolder.create(false, true, new NullProgressMonitor());
          }
        }
        ByteArrayInputStream byteStream = new ByteArrayInputStream("".getBytes());
        labelFile.create(byteStream, true, new NullProgressMonitor());
       
      } catch (CoreException e) {
        Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't create propertyfile : " + labelFile.getLocation(), e));
      }      
    }

    // stores labelfile
    FileOutputStream labelFileStream = null;
    BufferedWriter writer = null;
    try {
      if (isSortLabelFiles()) {
        Properties labelProperties = loadLabel(labelFile);
        labelProperties.setProperty(key, value);
        SortedProperties sortedProperties = new SortedProperties();
        sortedProperties.putAll(labelProperties);
        labelFileStream = new FileOutputStream(labelFile.getLocation().toString());
        sortedProperties.store(labelFileStream, null);
     
       
      } else {
        // append new label to existing file
        Properties labelProperties = loadLabel(labelFile);
        if (!labelProperties.containsKey(key)) {
          writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(labelFile.getLocation().toFile(), true), "8859_1"));
          if(labelProperties.entrySet().size()>0){
            writer.newLine();
          }         
          writer.write(LabelFileEncodingHelper.saveConvert(key, true));
          writer.write("=");
          writer.write(LabelFileEncodingHelper.saveConvert(value, false));         
          writer.flush();
       
        }
      }
      labelFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
    } catch (FileNotFoundException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't find propertyfile : " + labelFile.getLocation(), e));
    } catch (IOException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't write propertyfile : " + labelFile.getLocation(), e));
    } catch (CoreException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Can't find propertyfile : " + labelFile.getLocation(), e));
    }finally{
      if(labelFileStream!=null){
        try {
          labelFileStream.close();
        } catch (IOException e) {       
        }
      }
      if(writer!=null){
        try {
          writer.close();
        } catch (IOException e) {
        }
      }
     
    }
   
  }

  public void createLabel(String labelFileName, String key, String value) throws CoreException {
    IFolder labelContainer = getLabelContainer(getDevelopmentLanguage(), true);
    IFile labelFile = labelContainer.getFile(new Path(labelFileName.toLowerCase()) + ".properties");
    createLabel(labelFile, key, value);
  }

  public IFile getSyncInfo() {
    return _syncInfo;
  }

  public IContainer getDesignRoot() {
    return _designRoot;
  }

  public IFolder getFileContainerRoot() {
    return _fileContainerRoot;
  }

  public IFolder getScriptsRoot() {
    return _scriptsRoot;
  }

  public IFolder getTmlRoot() {
    return _tmlRoot;
  }

  public IFile getCsConfig() {
    return _csConfig;
  }
 
  public IFile getSchemaDefinition() {
      return _schemaDefinition;
  }

  /**
   * returns a list of available label_language_codes in this design
   *
   * @return
   * @throws CoreException
   */
  public List<String> getAvailableLableLanguages() throws CoreException {
    List<String> languages = new ArrayList<String>();
    Iterator<IFolder> labelContainers = getLabelContainers().iterator();
    while (labelContainers.hasNext()) {
      languages.add(labelContainers.next().getName().substring("labels_".length()).toLowerCase());
    }
    return languages;
  }

  /**
   * returns the development language for this design if the configured
   * language is not available bc. of missing label_containers the first
   * label_container language is returned if no label_containers are present
   * {@link WGADesignStructureHelper.DEFAULT_DESIGN_DEV_LANGUAGE} is used
   *
   * @return
   * @throws CoreException
   */
  public String getDevelopmentLanguage() throws CoreException {
    IScopeContext projectScope = new ProjectScope(getDesignRoot().getProject());
    IEclipsePreferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
    String langCode = projectNode.get("WGA_DESIGN_DEV_LANGUAGE/" + getDesignRoot().getLocation(), null);
    if (langCode != null && getAvailableLableLanguages().contains(langCode.toLowerCase())) {
      return langCode.toLowerCase();
    } else {
      List<String> languages = getAvailableLableLanguages();
      if (languages.isEmpty()) {
        return DEFAULT_DESIGN_DEV_LANGUAGE;
      } else {
        return languages.get(0);
      }
    }
  }

  public boolean isSortLabelFiles() {
    IScopeContext projectScope = new ProjectScope(getDesignRoot().getProject());
    IEclipsePreferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
    return projectNode.getBoolean("WGA_DESIGN_SORT_LABEL_FILES/" + getDesignRoot().getLocation(), false);
  }

  public void setSortLabelFiles(boolean sort) throws BackingStoreException {
    IScopeContext projectScope = new ProjectScope(getDesignRoot().getProject());
    IEclipsePreferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
    projectNode.putBoolean("WGA_DESIGN_SORT_LABEL_FILES/" + getDesignRoot().getLocation(), sort);
    projectNode.flush();
  }

  /**
   * sets the given development language for this design
   *
   * @param langCode
   * @throws CoreException
   * @throws BackingStoreException
   */
  public void setDevelopmentLanguage(String langCode) throws CoreException, BackingStoreException {
    if (langCode != null) {
      langCode = langCode.toLowerCase();
    }
    IScopeContext projectScope = new ProjectScope(getDesignRoot().getProject());
    IEclipsePreferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
    projectNode.put("WGA_DESIGN_DEV_LANGUAGE/" + getDesignRoot().getLocation(), langCode);
    projectNode.flush();
  }

  /**
   * retrieve all keys of the given labelfile in the current development
   * language
   *
   * @param labelFilename
   * @return
   */
  public Set<String> getLabelKeys(String labelFilename) {
    Set<String> labelKeys = new HashSet<String>();
    try {
      Properties labelProps = loadLabel(labelFilename);
      if (labelProps != null) {
        Enumeration<Object> keys = labelProps.keys();
        while (keys.hasMoreElements()) {
          labelKeys.add((String) keys.nextElement());
        }
      }
    } catch (CoreException e) {
    }
    return labelKeys;
  }

  /**
   * returns the metadata file for the given tml file
   *
   * @param tmlfile
   * @return metadata file (must not exist)
   */
  public static IFile getMetadataFile(IFile tmlfile) {
    IFolder metadataFolder = tmlfile.getParent().getFolder(new Path("metadata"));
    return metadataFolder.getFile(tmlfile.getName().substring(0, tmlfile.getName().length() - tmlfile.getFileExtension().length()) + "metadata.xml");
  }

  public static TMLMetadataInfo getMetaDataInfo(IFile tmlFile) {
    WGADesignStructureHelper helper = new WGADesignStructureHelper(tmlFile);
    TMLMetadataInfo metaData = new TMLMetadataInfo();

    try {
      WGADesignConfigurationModel wgaDesignConfig = helper.createModel();
      metaData.setDirectAccess(wgaDesignConfig.isDirectAccessDefault());
    } catch (IOException e) {
      Activator.getDefault().logError("Cannot load WGADesignConfig", e);
    }

    if (getMetadataFile(tmlFile).exists()) {
      TMLFileMetadataModel metaDataFile = new TMLFileMetadataModel(tmlFile);
      metaData.setDirectAccess(metaDataFile.isDirectAccess());
    }

    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new InputStreamReader(tmlFile.getContents()));
      String readLine = null;
      readLine = reader.readLine();
      while (readLine != null && readLine.startsWith("##MDSET ")) {
        try {
        metaData.processDesignHeader(readLine);
                }
                catch (Exception e) {
                    Activator.getDefault().logError("Unable to process design metadata header '" + readLine + "' in file '" + tmlFile.getLocation().toString() + "'.", e);
                }
        readLine = reader.readLine();
      }

    } catch (CoreException e) {
    } catch (IOException e) {
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {         
        }
      }
    }

    return metaData;
  }

  public static boolean isDirectAccess(IFile tmlFile){
    return getMetaDataInfo(tmlFile).isDirectAccess();   
  }
 
  /**
   * creates a dirlink in the given container
   * @param container
   * @param target
   * @throws IOException
   * @throws CoreException
   */
  public static void createDirlink(IContainer container, IContainer target) throws IOException, CoreException{
    String linkTarget = computeDirLinkTarget(container, target);
    WGUtils.createDirLink(container.getLocation().toFile(), linkTarget);
    container.refreshLocal(IResource.DEPTH_ONE, null);
  }
 
  public static String computeDirLinkTarget(IContainer container, IContainer target) {
    return computeDirLinkTarget(container, target.getFullPath());
      }
 
  public static String computeDirLinkTarget(IContainer container, IPath target) {
//    // compute relative path from container to target
//    int matchingSegments = target.matchingFirstSegments(container.getFullPath());
//    String relativePrefix = "";
//    String pathTokens[] = container.getFullPath().toPortableString().split("/");
//    for (int i = 0; i < pathTokens.length - 1 - matchingSegments; i++) {
//      relativePrefix += "../";
//    }
//    String linkTarget = target.toPortableString();
//    if (linkTarget.startsWith("/")) {
//      linkTarget = linkTarget.substring(1);
//    }
    return FileUtils.makeRelative(container.getFullPath(), target).toString();
      }

 
 
  public static void changeDirLink(IFolder dirlinkFolder, IContainer target) {
    IFile dirlink = dirlinkFolder.getFile(WGUtils.DIRLINK_FILE);
    String linkTarget = computeDirLinkTarget(dirlinkFolder, target);
    SAXReader saxReader = new SAXReader();
    try {
      File dirlinkFile = new File(dirlink.getLocationURI().getPath());
      Document document = saxReader.read(dirlinkFile);
      Element ele = (Element)document.selectSingleNode("/dirlink/path");    //TODO use statics
      ele.addAttribute("location", linkTarget);                //TODO use statics
        XMLWriter output = new XMLWriter(new FileWriter(dirlinkFile));
        try {
          output.write( document );
        } finally {
          try {
            output.close();
          } catch (IOException e) {           
          }
        }
    } catch (DocumentException e) {
      Activator.getDefault().logError("Can not parse xmlfile " + dirlink.getLocation(), e);
    } catch (IOException e) {
      Activator.getDefault().logError("Can not read/write file " + dirlink.getLocation(), e);
    }
  }
 
  public static void createDirklink(IContainer designRoot, IContainer folder, String name) throws CoreException, IOException {
    // create new folder in designroot with given name and add dirlink
    IFolder designFolder = FileUtils.createFolder(designRoot, name);
    createDirlink(designFolder, folder);
    }

  public static String getDesignNameByFolder(IFolder folder) {
    if (isDesignFolder(folder)) {
      String folderName = folder.getName();
      if (folder.getParent() instanceof IProject) {
        folderName = folder.getParent().getName();
      }
      return folderName;
    }
    return null;
  }

  public static IFile createPortlet(List<String> modes, String portletName, IFolder parent, String header) throws CoreException {
      if(header==null){
          header ="";
      }
     
    IResource portletFolderResource = FileUtils.createFolder(parent, portletName);
    IFolder portletFolder = (IFolder) portletFolderResource;     
   
    String tmlInclude = header + "\n<tml:include ref=\"{'::mode-' + portlet.mode}\"/>";   
    ByteArrayInputStream input = new ByteArrayInputStream(tmlInclude.getBytes());
    IFile portletFile = portletFolder.getFile("portlet.tml");
    portletFile.create(input, true, new NullProgressMonitor());
   
    portletFolder.getFile("form.tml").create(new ByteArrayInputStream(header.getBytes()), true, new NullProgressMonitor());   
    //TODO Form for form.tml
   
   
   
    Iterator<String> it = modes.iterator();   
    while (it.hasNext()) {
      String current = it.next();
      portletFolder.getFile("mode-"+current+".tml").create(new ByteArrayInputStream(header.getBytes()), true, new NullProgressMonitor());     
    }   
    return portletFile;
  }
 
  /**
   * returns all design resources of the current workspace
   * @return
   */
  public static List<IContainer> retrieveDesignResources(final ResourceFilter filter) {
    final List<IContainer> designResources = new ArrayList<IContainer>();
    try {
      ResourcesPlugin.getWorkspace().getRoot().accept(new IResourceVisitor() {
       
        public boolean visit(IResource resource) throws CoreException {   
         
          if (resource instanceof IContainer) {
            IContainer container = (IContainer) resource;       
              if (filter.accept(resource)) {
                designResources.add(container);             
                return false;
              }
              return true;           
          }
          return false;
        }
       
      });   
    } catch (CoreException e) {
      e.printStackTrace();
    }
    return designResources;
  }
 
  /**
   * generates an http url to the given tml resource
   * @param root the root url of the wga server
   * @param dbkey the dbkey of the database to use for tml rendering
   * @param tmlfile the tmlfile
   * @return
   * @throws MalformedURLException
   */
  public static URL generateHttpURL(URL root, String dbkey, IFile tmlFile) throws MalformedURLException {
    WGADesignStructureHelper helper = new WGADesignStructureHelper(tmlFile);
    String medium = determineMediaKey(tmlFile);
    if (medium == null) {
      medium = "html";
    }
    IPath tmlFilePath = FileUtils.makeRelative(helper.getTmlRoot().getLocation().append(medium), tmlFile.getLocation());
    String tmlReference = tmlFilePath.removeFileExtension().toString().replaceAll("/", ":");
    URL url = new URL(root.toString() + "/" + dbkey + "/" + medium + "/" + tmlReference);
    return url;
  }
 
 
 
 
 
  /**
   * checks if the given container is a valid location for tml files
   * @param container
   * @return true/false
   */ 
  public static boolean isValidTMLLocation(IContainer container){
    if (container == null) {
      return false;
    }
   
    IFile syncinfo = determineSyncInfo(container);
    if (syncinfo==null || !syncinfo.exists()) {
      return false;           
    }
   
    WGADesignStructureHelper helper = new WGADesignStructureHelper(syncinfo);
    if (helper.getTmlRoot().getFullPath().isPrefixOf(container.getFullPath()) && !helper.getTmlRoot().getFullPath().equals(container.getFullPath())) {
      // seams to be a valid tml location ... ensure that we are not below a metadata folder
      int matches = container.getFullPath().matchingFirstSegments(helper.getTmlRoot().getFullPath());
      String[] segments = container.getFullPath().removeFirstSegments(matches).segments();
      for (String segment : segments) {
        if (segment.equalsIgnoreCase("metadata")) {
          return false;
        }
      }
      return true;
    }
   
    return false;
  }
 
 
  public static boolean isValidPortletLocation(IContainer folder){
    return isValidTMLLocation(folder);   
  }

  public static boolean isWGADesignResource(IFile file) {   
    return determineSyncInfo(file) != null;
  }
  public boolean isWGADesignResourceOfCurrentDesign(IResource resource) {
    return isWGADesignResource(resource) &&  _designRoot.getFullPath().isPrefixOf(resource.getFullPath())
  }
 
  public static boolean isWGADesignResource(IResource resource) {
    if (resource == null || !resource.exists()) {
      return false;
    } else if (resource.getType() == IResource.FILE) {
      return isWGADesignResource((IFile)resource);
    } else if (resource instanceof IContainer) {
      IContainer parent = (IContainer)resource;
      while (parent instanceof IFolder) {
        parent = parent.getParent();
        IFile syncInfo = parent.getFile(new Path(DesignDirectory.DESIGN_DEFINITION_FILE));
        if (syncInfo.exists()) {
          return true;
        }
        syncInfo = parent.getFile(new Path(DesignDirectory.SYNCINFO_FILE));
        if (syncInfo.exists()) {
          return true;
        }
      }
      return false;
    }
    return false;     
  }
 
  public void enforceDesignEncoding() throws CoreException {
   
    getDesignRoot().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
   
    final Map<String,String> fDefaultCharsetsByExtension = new HashMap<String,String>();
    fDefaultCharsetsByExtension.put("xml", "UTF-8");
    fDefaultCharsetsByExtension.put("css", "UTF-8");
   
    // no explicit encoding for design root & file container root
    if (getDesignRoot() != null && getDesignRoot().isAccessible()) {
      getDesignRoot().setDefaultCharset(null, new NullProgressMonitor());
    }
    if (getFileContainerRoot() != null && getFileContainerRoot().isAccessible()) {
      getFileContainerRoot().setDefaultCharset(null, new NullProgressMonitor());
    }
   
    // set explicit encodings where necessary
    final String fEncoding = determineDesignEncoding();
    if (getTmlRoot() != null && getTmlRoot().isAccessible()) {
      getTmlRoot().setDefaultCharset(fEncoding, new NullProgressMonitor());
    }
    if (getScriptsRoot() != null && getScriptsRoot().isAccessible()) {
      getScriptsRoot().setDefaultCharset(fEncoding, new NullProgressMonitor());
      getScriptsRoot().accept(new IResourceVisitor() {
 
        public boolean visit(IResource resource) throws CoreException {
          if (resource instanceof IFile) {
            IFile file = (IFile) resource;
            String defaultEncoding = fDefaultCharsetsByExtension.get(file.getFileExtension().toLowerCase());
            if (defaultEncoding != null) {
              String resourceCharset = file.getCharset();
              if (resourceCharset == null) {
                resourceCharset = defaultEncoding;
              }
              if (!fEncoding.equals(resourceCharset)) {
                // if design encoding differ from default encoding we define it explicit
                file.setCharset(fEncoding, new NullProgressMonitor());
              } else {
                file.setCharset(null, new NullProgressMonitor());
              }
            }
          }
          return true;
        }
       
      });
    }
  }
 
  public static IContainer resolveDirLink(IFile file) {
    File linkedFile = WGUtils.resolveDirLink(file.getParent().getLocation().toFile());
    return  (IContainer) file.getWorkspace().getRoot().findContainersForLocationURI(linkedFile.toURI())[0];
  }
 
  public static IContainer resolveDirLink(IContainer container) {
    return resolveDirLink(container.getFile(new Path(WGUtils.DIRLINK_FILE)));
  }

  public boolean hasPluginConfig() {
    IFile syncInfo = getSyncInfo();
    if (syncInfo.exists()) {     
      try {
        WGADesignConfigurationModelWrapper wrapper = new WGADesignConfigurationModelWrapper(syncInfo);
        return wrapper.hasPluginConfig();
      } catch (IOException e) {
      }     
    }
    return false;
  }

  /**
   * ensures that syncinfo and csconfig is writeable
   * @throws CoreException
   */
  public void makeDesignConfigWriteable() throws CoreException {
    if (getSyncInfo() != null && getSyncInfo().isReadOnly()) {
      ResourceAttributes attributes = getSyncInfo().getResourceAttributes();
      attributes.setReadOnly(false);
      getSyncInfo().setResourceAttributes(attributes);
    }
   
    if (getCsConfig() != null && getCsConfig().isReadOnly()) {
      ResourceAttributes attributes = getCsConfig().getResourceAttributes();
      attributes.setReadOnly(false);
      getCsConfig().setResourceAttributes(attributes);
    }
   
    if (getSchemaDefinition() != null && getSchemaDefinition().isReadOnly()) {
        ResourceAttributes attributes = getSchemaDefinition().getResourceAttributes();
            attributes.setReadOnly(false);
            getSchemaDefinition().setResourceAttributes(attributes);
    }
  }

 
  /**
   * checks if the given name is a valid module name
   * only alphanumeric ascii characters and '.' , '_', '-', '$' are allowed
   * @param name
   * @return true/ false
   */
  public static boolean isValidModuleName(String name) {
    for (int i=0; i < name.length(); i++) {
      char c = name.charAt(i);
     
      if (!(c > 127) && (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.' || c == '$')) {
        continue;
      } else {
        return false;
      }
    }
    return true;
  }

 
 
  public static boolean isMetadataFolder(IContainer container) {
      if (container instanceof IFolder && container.getName().equals("metadata")) {
          return true;
      } else {
          return false;
      }
  }
 
  public String computeTMLReference(IFile tmlFile) {
      String mediaKey = determineMediaKey(tmlFile);     
      IContainer base = getTmlRoot().getFolder(mediaKey);
     
      IPath basePath = base.getFullPath();
      IPath path = tmlFile.getFullPath();     
      if(basePath.isPrefixOf(path)){
          path = path.removeFirstSegments(basePath.segmentCount());            
          path = path.removeFileExtension();
            return path.toString().replace('/', ':');
        }
      return null;
  }
 
  public List<String> getMediaKeys() {
      List<String> mediaKeys = new ArrayList<String>();
      try {
            for (IResource mediaFolder : getTmlRoot().members(IResource.FOLDER)) {
                mediaKeys.add(mediaFolder.getName().toLowerCase());
            }
        }
        catch (CoreException e) {
        }
      return mediaKeys;
  }
 
  public static IContainer retrieveDesignContainerFromSelection(IStructuredSelection selection) {
      IContainer container = null;
      if (selection.getFirstElement() instanceof IContainer) {
          container = (IContainer) selection.getFirstElement();
      } else if (selection.getFirstElement() instanceof IFile) {
          container = ((IFile)selection.getFirstElement()).getParent();
      } else if (selection.getFirstElement() instanceof IJavaProject) {
          container = ((IJavaProject)selection.getFirstElement()).getProject();
      }
     
      if (container != null) {
            if (WGADesignStructureHelper.isDesignFolder(container)) {
                return container;
            }
            container = container.getParent();
            while (container != null) {               
                if (WGADesignStructureHelper.isDesignFolder(container)) {
                    return container;
                }
                container = container.getParent();
            }
        }
      return null;
  }
 
 
  public static VersionCompliance wgaVersionToVersionCompliance(Version version){     
      if(version!=null){
          Set<String> keySet = WGADesignConfigurationModel.VERSIONCOMPLIANCE_TO_WGA_VERSION.keySet();
          for(String currentKey : keySet){
              Version currentVersion = (Version)WGADesignConfigurationModel.VERSIONCOMPLIANCE_TO_WGA_VERSION.get(currentKey);
              if(currentVersion.getMajorVersion() == version.getMajorVersion() && currentVersion.getMinorVersion() == version.getMinorVersion()){
                  return WGADesignConfigurationModel.VERSIONCOMPLIANCES.get(currentKey);           
              }             
          }     
      }     
      return null;
 
 
}
TOP

Related Classes of de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper$CaseInsensitivFolderReferenceSearcher

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.