/*******************************************************************************
* 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.helpers;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.VersionCompliance;
public class TMLReferenceLookup implements AttributeValueLookup {
public Set<String> lookupValues(TMLRegion region, IDocument document, ITextViewer viewer, IFile activeFile) {
return lookupValues(region, activeFile);
}
public static Set<String> lookupValues(TMLRegion region, IFile activeFile) {
Set<String> tmlfilerefs = new HashSet<String>();
Set<String> nontmlspecificrefs = null;
String medium;
WGADesignStructureHelper designHelper = new WGADesignStructureHelper(activeFile);
VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(activeFile);
String mymedium = WGADesignStructureHelper.determineMediaKey(activeFile);
String tagName = region.getTagName();
if (tagName != null && tagName.equals("url")) {
if (region.hasAttribute("db")) {
// cannot lookup references in foreign db
return Collections.emptySet();
}
} else if (region.hasAttribute("designdb")) {
// cannot lookup references in foreign db
return Collections.emptySet();
}
if (region.getAttributeValue("type") != null) {
if (!region.getAttributeValue("type").equals("portlet") && !region.getAttributeValue("type").equals("tml")) {
return Collections.emptySet();
}
}
if (region.getAttributeNames().contains("medium")) {
medium = region.getAttributeValue("medium");
} else {
medium = mymedium;
}
// the path of the selected medium
IPath mediumPath = designHelper.getTmlRoot().getFolder(new Path(medium)).getProjectRelativePath();
// the path to the file which we are currently editing
IPath pathOfEditedFile = activeFile.getParent().getProjectRelativePath();
pathOfEditedFile = pathOfEditedFile.removeFirstSegments(mediumPath.segmentCount());
ResourceIndexManager rsm = Plugin.getDefault().getResourceIndexManager();
if (rsm != null) {
nontmlspecificrefs = rsm.getTMLpaths(activeFile, medium);
Iterator<String> it = nontmlspecificrefs.iterator();
while (it.hasNext()) {
// current file from the db
IPath currentFile = new Path(it.next()).removeFileExtension();
// path of the current file
IPath currentFilePath = currentFile.removeLastSegments(1);
// removing the path of the elected medium
currentFilePath = currentFilePath.removeFirstSegments(mediumPath.segmentCount());
// removing the path of the elected medium
currentFile = currentFile.removeFirstSegments(mediumPath.segmentCount());
IPath tmp = null;
String reference = currentFile.toString().replaceAll("\\/", ":");
if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
if (reference.startsWith("overlay:") && pathOfEditedFile.toString().contains("overlay")) {
reference = reference.substring("overlay:".length());
} else if (pathOfEditedFile.toString().contains("overlay")) {
reference = reference + "@base";
}
}
tmlfilerefs.add(reference);
if (pathOfEditedFile.segmentCount() > 0 && (currentFilePath.segmentCount() >= pathOfEditedFile.segmentCount()) && medium.equals(mymedium)) {
int a = currentFilePath.segmentCount();
int b = pathOfEditedFile.segmentCount();
tmp = currentFilePath.removeLastSegments(a - b);
if (tmp.equals(pathOfEditedFile)) {
currentFilePath = currentFile.removeFirstSegments(tmp.segmentCount());
tmlfilerefs.add("::" + currentFilePath.toString().replaceAll("\\/", ":"));
}
}
}
}
return tmlfilerefs;
}
}