You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by jo...@apache.org on 2011/08/01 16:07:12 UTC
svn commit: r1152783 [6/12] - in
/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core: ./ META-INF/
bin/ schema/ src/ src/main/ src/main/java/ src/main/java/org/
src/main/java/org/apache/ src/main/java/org/apache/uima/
src/main/java/org/apache/u...
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,600 @@
+package org.apache.uima.tm.dltk.internal.core.packages;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.uima.tm.dltk.core.TextMarkerPlugin;
+import org.apache.uima.tm.dltk.internal.core.packages.DLTKTextMarkerHelper.TextMarkerPackage;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.launching.IInterpreterInstall;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+
+/**
+ * This class persistently holds all required information for specified interpreter package to path
+ * associations.
+ */
+public class PackagesManager {
+ private static final String DEPENDENCY_TAG = "dependency"; //$NON-NLS-1$
+
+ private static final String INTERPRETER_TAG = "interpreter"; //$NON-NLS-1$
+
+ private static final String VALUE_ATTR = "value"; //$NON-NLS-1$
+
+ private static final String PACKAGES_FILE = "packages.txt"; //$NON-NLS-1$
+
+ private static final String PACKAGES_TAG = "packages"; //$NON-NLS-1$
+
+ private static final String PACKAGE_TAG = "package"; //$NON-NLS-1$
+
+ private static final String INTERPRETER_ATTR = INTERPRETER_TAG;
+
+ private static final String NAME_ATTR = "name"; //$NON-NLS-1$
+
+ private static final String PATH_TAG = "path"; //$NON-NLS-1$
+
+ private static PackagesManager manager;
+
+ /**
+ * Contains association of PackageKey to PackageInformation
+ */
+ private Map packages = new HashMap();
+
+ /**
+ * Contains set of interpreter to list of packages association.
+ */
+ private Map interpreterToPackages = new HashMap();
+
+ private Map packsWithDeps = new HashMap();
+
+ public static PackagesManager getInstance() {
+ if (manager == null) {
+ manager = new PackagesManager();
+ }
+ return manager;
+ }
+
+ private PackagesManager() {
+ initialize();
+ }
+
+ private static class PackageKey {
+ private String packageName;
+
+ private String interpreterPath;
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((interpreterPath == null) ? 0 : interpreterPath.hashCode());
+ result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ PackageKey other = (PackageKey) obj;
+ if (interpreterPath == null) {
+ if (other.interpreterPath != null)
+ return false;
+ } else if (!interpreterPath.equals(other.interpreterPath))
+ return false;
+ if (packageName == null) {
+ if (other.packageName != null)
+ return false;
+ } else if (!packageName.equals(other.packageName))
+ return false;
+ return true;
+ }
+
+ public String getPackageName() {
+ return packageName;
+ }
+
+ public void setPackageName(String packageName) {
+ this.packageName = packageName;
+ }
+
+ public String getInterpreterPath() {
+ return interpreterPath;
+ }
+
+ public void setInterpreterPath(String interpreterPath) {
+ this.interpreterPath = interpreterPath;
+ }
+ }
+
+ public static class PackageInformation {
+ private final Set paths = new HashSet();
+
+ private final Set dependencies = new HashSet();
+
+ public Set getPaths() {
+ return paths;
+ }
+
+ public Set getDependencies() {
+ return dependencies;
+ }
+ }
+
+ private void initialize() {
+ IPath packagesPath = TextMarkerPlugin.getDefault().getStateLocation().append(PACKAGES_FILE);
+ File packagesFile = packagesPath.toFile();
+ if (packagesFile.exists()) {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder;
+ try {
+ builder = factory.newDocumentBuilder();
+ Document document = builder.parse(new BufferedInputStream(
+ new FileInputStream(packagesFile), 2048));
+ populate(document.getDocumentElement());
+ } catch (ParserConfigurationException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (FileNotFoundException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (SAXException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (IOException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ private void save() {
+ IPath packagesPath = TextMarkerPlugin.getDefault().getStateLocation().append(PACKAGES_FILE);
+ File packagesFile = packagesPath.toFile();
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder;
+ try {
+ builder = factory.newDocumentBuilder();
+ Document document = builder.newDocument();
+ save(document);
+
+ FileOutputStream fos = new FileOutputStream(packagesFile, false);
+ BufferedOutputStream bos = new BufferedOutputStream(fos, 2048);
+
+ TransformerFactory serFactory = TransformerFactory.newInstance();
+ Transformer transformer = serFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+
+ DOMSource source = new DOMSource(document);
+ StreamResult outputTarget = new StreamResult(bos);
+ transformer.transform(source, outputTarget);
+ bos.close();
+ fos.close();
+
+ } catch (ParserConfigurationException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (FileNotFoundException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (IOException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ } catch (TransformerException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private synchronized void save(Document doc) {
+ Element packagesElement = doc.createElement(PACKAGES_TAG);
+ doc.appendChild(packagesElement);
+ for (Iterator iterator = this.packages.keySet().iterator(); iterator.hasNext();) {
+ PackageKey key = (PackageKey) iterator.next();
+
+ Element packageElement = doc.createElement(PACKAGE_TAG);
+ packageElement.setAttribute(NAME_ATTR, key.getPackageName());
+ packageElement.setAttribute(INTERPRETER_ATTR, key.getInterpreterPath());
+
+ PackageInformation info = (PackageInformation) this.packages.get(key);
+ Set paths = info.getPaths();
+ for (Iterator iterator2 = paths.iterator(); iterator2.hasNext();) {
+ IPath path = (IPath) iterator2.next();
+ Element pathElement = doc.createElement(PATH_TAG);
+ pathElement.setAttribute(VALUE_ATTR, path.toOSString());
+ packageElement.appendChild(pathElement);
+ }
+ Set deps = info.getDependencies();
+ for (Iterator iterator2 = deps.iterator(); iterator2.hasNext();) {
+ String pkgName = (String) iterator2.next();
+ Element pkgElement = doc.createElement(DEPENDENCY_TAG);
+ pkgElement.setAttribute(NAME_ATTR, pkgName);
+ packageElement.appendChild(pkgElement);
+ }
+ packagesElement.appendChild(packageElement);
+ }
+ for (Iterator iterator = this.interpreterToPackages.keySet().iterator(); iterator.hasNext();) {
+ String interpreter = (String) iterator.next();
+
+ Element interpreterElement = doc.createElement(INTERPRETER_TAG);
+ interpreterElement.setAttribute(NAME_ATTR, interpreter);
+ Set pkgs = (Set) this.interpreterToPackages.get(interpreter);
+ for (Iterator iterator2 = pkgs.iterator(); iterator2.hasNext();) {
+ String pkgName = (String) iterator2.next();
+ Element pathElement = doc.createElement(PACKAGE_TAG);
+ pathElement.setAttribute(VALUE_ATTR, pkgName);
+ interpreterElement.appendChild(pathElement);
+ }
+ packagesElement.appendChild(interpreterElement);
+ }
+ }
+
+ private synchronized void populate(Element documentElement) {
+ NodeList childNodes = documentElement.getChildNodes();
+ int length = childNodes.getLength();
+ for (int i = 0; i < length; i++) {
+ Node child = childNodes.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE) {
+ if (child.getNodeName().equalsIgnoreCase(PACKAGE_TAG)) {
+ Element e = (Element) child;
+ String packageName = e.getAttribute(NAME_ATTR);
+ String interpreter = e.getAttribute(INTERPRETER_ATTR);
+ PackageInformation packageInfo = new PackageInformation();
+ NodeList childrens = e.getChildNodes();
+ for (int j = 0; j < childrens.getLength(); j++) {
+ Node path = childrens.item(j);
+ if (path.getNodeType() == Node.ELEMENT_NODE) {
+ if (path.getNodeName().equalsIgnoreCase(PATH_TAG)) {
+ String pathValue = ((Element) path).getAttribute(VALUE_ATTR);
+ packageInfo.getPaths().add(new Path(pathValue));
+ } else if (path.getNodeName().equalsIgnoreCase(DEPENDENCY_TAG)) {
+ String pkgName = ((Element) path).getAttribute(NAME_ATTR);
+ packageInfo.getDependencies().add(pkgName);
+ }
+ }
+ }
+ this.packages.put(makeKey(packageName, interpreter), packageInfo);
+ } else if (child.getNodeName().equalsIgnoreCase(INTERPRETER_TAG)) {
+ Element e = (Element) child;
+ String interpreter = e.getAttribute(NAME_ATTR);
+ NodeList paths = e.getChildNodes();
+ Set packagesSet = new HashSet();
+ for (int j = 0; j < paths.getLength(); j++) {
+ Node packageNode = paths.item(j);
+ if (packageNode.getNodeType() == Node.ELEMENT_NODE) {
+ if (packageNode.getNodeName().equalsIgnoreCase(PACKAGE_TAG)) {
+ String packageNameValue = ((Element) packageNode).getAttribute(VALUE_ATTR);
+ packagesSet.add(packageNameValue);
+ }
+ }
+ }
+ this.interpreterToPackages.put(interpreter, packagesSet);
+ }
+ }
+ }
+ }
+
+ private PackageKey makeKey(String packageName, String interpreter) {
+ PackageKey key = new PackageKey();
+ key.setPackageName(packageName);
+ key.setInterpreterPath(interpreter);
+ return key;
+ }
+
+ /**
+ * Return paths specific only for selected package.
+ */
+ public synchronized IPath[] getPathsForPackage(IInterpreterInstall install, String packageName) {
+ PackageKey key = makeKey(packageName, getInterpreterKey(install));
+ if (this.packages.containsKey(key)) {
+ PackageInformation info = (PackageInformation) this.packages.get(key);
+ Set els = info.getPaths();
+ return (IPath[]) els.toArray(new IPath[els.size()]);
+ }
+ // Retrieve paths from interpreter with all dependencies.
+ TextMarkerPackage[] srcs = DLTKTextMarkerHelper.getSrcs(install.getExecEnvironment(), install
+ .getInstallLocation(), install.getEnvironmentVariables(), packageName);
+
+ PackageInformation resultInfo = null;
+ for (int i = 0; i < srcs.length; i++) {
+ Set paths2 = srcs[i].getPaths();
+ PackageKey okey = makeKey(srcs[i].getName(), getInterpreterKey(install));
+ PackageInformation info;
+ if (this.packages.containsKey(okey)) {
+ info = (PackageInformation) this.packages.get(okey);
+ } else {
+ info = new PackageInformation();
+ }
+ info.getPaths().addAll(paths2);
+ info.getDependencies().addAll(srcs[i].getDependencies());
+ this.packages.put(okey, info);
+ if (okey.equals(key)) {
+ resultInfo = info;
+ }
+ }
+ // Follow all dependencies
+ if (resultInfo == null) {
+ this.packages.put(key, new PackageInformation());
+ return new IPath[0];
+ }
+
+ Set resultPaths = new HashSet();
+ resultPaths.addAll(resultInfo.getPaths());
+
+ save();
+ return (IPath[]) resultPaths.toArray(new IPath[resultPaths.size()]);
+ }
+
+ public synchronized Map getDependencies(String pkgName, IInterpreterInstall install) {
+ Set checkedPackages = new HashSet();
+ Map packagesSet = new HashMap();
+ PackageKey key = makeKey(pkgName, install);
+ PackageInformation info = (PackageInformation) this.packages.get(key);
+ if (info != null) {
+ traverseDependencies(packagesSet, checkedPackages, info, install);
+ }
+ return packagesSet;
+ }
+
+ private PackageKey makeKey(String pkgName, IInterpreterInstall install) {
+ return makeKey(pkgName, getInterpreterKey(install));
+ }
+
+ private synchronized void traverseDependencies(Map packagesSet, Set checkedPackages,
+ PackageInformation resultInfo, IInterpreterInstall install) {
+ Set dependencies = resultInfo.getDependencies();
+ for (Iterator iterator = dependencies.iterator(); iterator.hasNext();) {
+ String pkgName = (String) iterator.next();
+ if (!checkedPackages.contains(pkgName)) {
+ checkedPackages.add(pkgName);
+ PackageKey pkgKey = makeKey(pkgName, getInterpreterKey(install));
+ if (this.packages.containsKey(pkgKey)) {
+ PackageInformation depInfo = (PackageInformation) this.packages.get(pkgKey);
+ packagesSet.put(pkgName, depInfo);
+ traverseDependencies(packagesSet, checkedPackages, depInfo, install);
+ }
+ }
+ }
+ }
+
+ public synchronized Set getPackageNames(IInterpreterInstall install) {
+ String key = getInterpreterKey(install);
+ if (this.interpreterToPackages.containsKey(key)) {
+ Set set = (Set) this.interpreterToPackages.get(key);
+ return set;
+ }
+ // Evaluate
+ Set packs = DLTKTextMarkerHelper.getPackages(install);
+ this.interpreterToPackages.put(key, packs);
+ save();
+ return packs;
+ }
+
+ private String getInterpreterKey(IInterpreterInstall install) {
+ if (install == null) {
+ return "";
+ }
+ return install.getInstallLocation().toOSString() + ":" //$NON-NLS-1$
+ + install.getEnvironment().getId();
+ }
+
+ private String getInterpreterProjectKey(IInterpreterInstall install, String projectName) {
+ return "internal|||" + projectName + "|||" //$NON-NLS-1$ //$NON-NLS-2$
+ + getInterpreterKey(install);
+ }
+
+ public Set getInternalPackageNames(IInterpreterInstall install, IScriptProject project) {
+ return getInternalPackageNames(install, project.getElementName());
+ }
+
+ public Set getInternalPackageNames(IInterpreterInstall install, IProject project) {
+ return getInternalPackageNames(install, project.getName());
+ }
+
+ public synchronized Set getInternalPackageNames(IInterpreterInstall install, String projectName) {
+ final String key = getInterpreterProjectKey(install, projectName);
+ if (this.interpreterToPackages.containsKey(key)) {
+ return (Set) this.interpreterToPackages.get(key);
+ }
+ return Collections.EMPTY_SET;
+ }
+
+ public synchronized void setInternalPackageNames(IInterpreterInstall install,
+ IScriptProject project, Set names) {
+ String key = getInterpreterProjectKey(install, project.getElementName());
+ // TODO compare and save only if there are changes
+ this.interpreterToPackages.put(key, new HashSet(names));
+ save();
+ }
+
+ /**
+ * Return all packages paths in one call.
+ */
+ public synchronized IPath[] getPathsForPackages(IInterpreterInstall install, Set packagesInBuild) {
+
+ StringBuffer buf = new StringBuffer();
+ String[] pkgs = (String[]) packagesInBuild.toArray(new String[packagesInBuild.size()]);
+ for (int i = 0; i < pkgs.length; i++) {
+ buf.append(pkgs[i]).append(" "); //$NON-NLS-1$
+ }
+ PackageKey key = makeKey(buf.toString(), getInterpreterKey(install));
+
+ if (this.packages.containsKey(key)) {
+ PackageInformation info = (PackageInformation) this.packages.get(key);
+ Set paths = info.getPaths();
+ return (IPath[]) paths.toArray(new IPath[paths.size()]);
+ }
+ // Retrieve paths from interpreter with all dependencies.
+ TextMarkerPackage[] srcs = DLTKTextMarkerHelper.getSrcs(install.getExecEnvironment(), install
+ .getInstallLocation(), install.getEnvironmentVariables(), buf.toString());
+ Set result = new HashSet();
+ if (srcs == null) {
+ return new IPath[0];
+ }
+ for (int i = 0; i < srcs.length; i++) {
+ Set paths2 = srcs[i].getPaths();
+ PackageKey okey = makeKey(srcs[i].getName(), getInterpreterKey(install));
+ PackageInformation info = null;
+ if (this.packages.containsKey(okey)) {
+ info = (PackageInformation) this.packages.get(okey);
+ } else {
+ info = new PackageInformation();
+ }
+ result.addAll(paths2);
+ info.getPaths().addAll(paths2);
+ info.getDependencies().addAll(srcs[i].getDependencies());
+ this.packages.put(okey, info);
+ }
+
+ PackageInformation info = new PackageInformation();
+ info.getPaths().addAll(result);
+ this.packages.put(key, info);
+
+ for (int i = 0; i < pkgs.length; i++) {
+ PackageKey lkey = makeKey(pkgs[i], getInterpreterKey(install));
+ if (!this.packages.containsKey(lkey)) {
+ this.packages.put(lkey, new PackageInformation());
+ }
+ }
+ save();
+
+ return (IPath[]) result.toArray(new IPath[result.size()]);
+ }
+
+ public IPath[] getAllPaths(String pkgName, IInterpreterInstall install) {
+ Set result = new HashSet();
+ IPath[] paths = this.getPathsForPackage(install, pkgName);
+ result.addAll(Arrays.asList(paths));
+ Map dependencies = this.getDependencies(pkgName, install);
+ for (Iterator iterator = dependencies.keySet().iterator(); iterator.hasNext();) {
+ String packageName = (String) iterator.next();
+ PackageInformation info = (PackageInformation) dependencies.get(packageName);
+ result.addAll(info.getPaths());
+ }
+ return (IPath[]) result.toArray(new IPath[result.size()]);
+ }
+
+ /**
+ * This method removes all information about specified interpreter.
+ *
+ * @param install
+ */
+ public synchronized void removeInterprterInfo(IInterpreterInstall install) {
+ // Remove interpreter to packages set
+ String interpreterPath = getInterpreterKey(install);
+ this.interpreterToPackages.remove(interpreterPath);
+ // Remove all values stored for interpreter packages
+ for (Iterator iterator = this.packages.keySet().iterator(); iterator.hasNext();) {
+ PackageKey key = (PackageKey) iterator.next();
+ String path = key.getInterpreterPath();
+ if (path.equals(interpreterPath)) {
+ iterator.remove();
+ }
+ }
+ save();
+ }
+
+ /**
+ * Clears all cached information.
+ */
+ public synchronized void clearCache() {
+ this.interpreterToPackages.clear();
+ this.packages.clear();
+ this.packsWithDeps.clear();
+ save();
+ }
+
+ public IPath[] getPathsForPackageWithDeps(IInterpreterInstall install, String name) {
+ Set result = new HashSet();
+ IPath[] paths = this.getPathsForPackage(install, name);
+ result.addAll(Arrays.asList(paths));
+
+ Map dependencies = getDependencies(name, install);
+ for (Iterator iterator = dependencies.keySet().iterator(); iterator.hasNext();) {
+ String pkgName = (String) iterator.next();
+ result.addAll(Arrays.asList(getPathsForPackage(install, pkgName)));
+ }
+ return (IPath[]) result.toArray(new IPath[result.size()]);
+ }
+
+ public IPath[] getPathsForPackagesWithDeps(IInterpreterInstall install, Set packagesSet) {
+
+ String pkey = makePKey(packagesSet);
+ if (this.packsWithDeps.containsKey(pkey)) {
+ return (IPath[]) this.packsWithDeps.get(pkey);
+ }
+
+ Set result = new HashSet();
+ IPath[] paths = this.getPathsForPackages(install, packagesSet);
+ result.addAll(Arrays.asList(paths));
+
+ for (Iterator jiterator = packagesSet.iterator(); jiterator.hasNext();) {
+ String name = (String) jiterator.next();
+ Map dependencies = getDependencies(name, install);
+ for (Iterator iterator = dependencies.keySet().iterator(); iterator.hasNext();) {
+ String pkgName = (String) iterator.next();
+ result.addAll(Arrays.asList(getPathsForPackage(install, pkgName)));
+ }
+ }
+ IPath[] array = (IPath[]) result.toArray(new IPath[result.size()]);
+ this.packsWithDeps.put(pkey, array);
+ return array;
+ }
+
+ private String makePKey(Set packagesSet) {
+ StringBuffer buffer = new StringBuffer();
+ List l = new ArrayList();
+ l.addAll(packagesSet);
+ Collections.sort(l);
+ for (Iterator iterator = l.iterator(); iterator.hasNext();) {
+ String object = (String) iterator.next();
+ buffer.append(object);
+ }
+ return buffer.toString();
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/PackagesManager.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,62 @@
+package org.apache.uima.tm.dltk.internal.core.packages;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.uima.tm.dltk.core.TextMarkerPlugin;
+import org.apache.uima.tm.dltk.parser.ast.declarations.TextMarkerPackageDeclaration;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.statements.Statement;
+
+
+public class TextMarkerBuildPathPackageCollector extends ASTVisitor {
+
+ private final List requireDirectives = new ArrayList();
+
+ private final Set packagesRequired = new HashSet();
+
+ private final Set packagesProvided = new HashSet();
+
+ public void process(ModuleDeclaration declaration) {
+ try {
+ declaration.traverse(this);
+ } catch (Exception e) {
+ TextMarkerPlugin.error(e);
+ }
+ }
+
+ @Override
+ public boolean visit(Statement s) throws Exception {
+ if (s instanceof TextMarkerPackageDeclaration) {
+ final TextMarkerPackageDeclaration pkg = (TextMarkerPackageDeclaration) s;
+ packagesProvided.add(pkg.getName());
+ return false;
+ }
+ return super.visit(s);
+ }
+
+ /**
+ * @return the requireDirectives
+ */
+ public List getRequireDirectives() {
+ return requireDirectives;
+ }
+
+ /**
+ * @return the packagesRequired
+ */
+ public Set getPackagesRequired() {
+ return packagesRequired;
+ }
+
+ /**
+ * @return the packagesProvided
+ */
+ public Set getPackagesProvided() {
+ return packagesProvided;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerBuildPathPackageCollector.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,306 @@
+package org.apache.uima.tm.dltk.internal.core.packages;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.uima.tm.dltk.core.CodeModel;
+import org.apache.uima.tm.dltk.core.TextMarkerPlugin;
+import org.apache.uima.tm.dltk.core.TextMarkerProblems;
+import org.apache.uima.tm.dltk.parser.ast.declarations.TextMarkerPackageDeclaration;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.compiler.problem.DefaultProblem;
+import org.eclipse.dltk.compiler.problem.IProblemReporter;
+import org.eclipse.dltk.compiler.problem.ProblemSeverities;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IBuildpathEntry;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.ISourceModule;
+import org.eclipse.dltk.core.ModelException;
+import org.eclipse.dltk.core.builder.IBuildContext;
+import org.eclipse.dltk.core.builder.IBuildParticipant;
+import org.eclipse.dltk.core.builder.IBuildParticipantExtension;
+import org.eclipse.dltk.core.builder.IBuildParticipantExtension2;
+import org.eclipse.dltk.core.builder.IScriptBuilder.DependencyResponse;
+import org.eclipse.dltk.core.environment.EnvironmentPathUtils;
+import org.eclipse.dltk.launching.IInterpreterInstall;
+import org.eclipse.dltk.launching.ScriptRuntime;
+import org.eclipse.osgi.util.NLS;
+
+
+public class TextMarkerCheckBuilder implements IBuildParticipant, IBuildParticipantExtension,
+ IBuildParticipantExtension2 {
+
+ private final IScriptProject project;
+
+ private final IInterpreterInstall install;
+
+ private final PackagesManager manager = PackagesManager.getInstance();
+
+ private final TextMarkerBuildPathPackageCollector packageCollector = new TextMarkerBuildPathPackageCollector();
+
+ private final Map codeModels = new HashMap();
+
+ private final Map resourceToModuleInfos = new HashMap();
+
+ private final Set knownPackageNames;
+
+ private final Set buildpath;
+
+ private static class ModuleInfo {
+ final List requireDirectives;
+
+ final IProblemReporter reporter;
+
+ public ModuleInfo(IProblemReporter reporter, List requireDirectives) {
+ this.reporter = reporter;
+ this.requireDirectives = requireDirectives;
+ }
+
+ }
+
+ /**
+ * @param project
+ * @throws CoreException
+ * @throws IllegalStateException
+ * if associated interpreter could not be found
+ */
+ public TextMarkerCheckBuilder(IScriptProject project) throws CoreException, IllegalStateException {
+ this.project = project;
+ install = ScriptRuntime.getInterpreterInstall(project);
+ if (install == null) {
+ // thrown exception is caught in the TextMarkerPackageCheckerType
+ throw new IllegalStateException(NLS.bind(Messages.TextMarkerCheckBuilder_interpreterNotFound,
+ project.getElementName()));
+ }
+ knownPackageNames = manager.getPackageNames(install);
+ buildpath = getBuildpath(project);
+ }
+
+ private int buildType;
+
+ public boolean beginBuild(int buildType) {
+ this.buildType = buildType;
+ if (buildType != FULL_BUILD) {
+ // retrieve packages provided by this project
+ packageCollector.getPackagesProvided().addAll(
+ manager.getInternalPackageNames(install, project));
+ }
+ loadProvidedPackagesFromRequiredProjects();
+ return true;
+ }
+
+ private void loadProvidedPackagesFromRequiredProjects() {
+ final IBuildpathEntry[] resolvedBuildpath;
+ try {
+ resolvedBuildpath = project.getResolvedBuildpath(true);
+ } catch (ModelException e) {
+ TextMarkerPlugin.error(e);
+ return;
+ }
+ final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
+ for (int i = 0; i < resolvedBuildpath.length; i++) {
+ final IBuildpathEntry entry = resolvedBuildpath[i];
+ if (entry.getEntryKind() == IBuildpathEntry.BPE_PROJECT) {
+ final IPath path = entry.getPath();
+ final IProject project = workspaceRoot.getProject(path.lastSegment());
+ if (project.exists()) {
+ packageCollector.getPackagesProvided().addAll(
+ manager.getInternalPackageNames(install, project));
+ }
+ }
+ }
+ }
+
+ public void buildExternalModule(IBuildContext context) throws CoreException {
+ final ModuleDeclaration ast = (ModuleDeclaration) context
+ .get(IBuildContext.ATTR_MODULE_DECLARATION);
+ if (ast != null) {
+ packageCollector.getRequireDirectives().clear();
+ packageCollector.process(ast);
+ }
+ }
+
+ public void build(IBuildContext context) throws CoreException {
+ ModuleDeclaration ast = (ModuleDeclaration) context.get(ModuleDeclaration.class.getName());
+ if (ast == null) {
+ return;
+ }
+ packageCollector.getRequireDirectives().clear();
+ packageCollector.process(ast);
+ if (!packageCollector.getRequireDirectives().isEmpty()) {
+ resourceToModuleInfos.put(context.getSourceModule(), new ModuleInfo(context
+ .getProblemReporter(), new ArrayList(packageCollector.getRequireDirectives())));
+ }
+ }
+
+ public void endBuild(IProgressMonitor monitor) {
+ if (buildType != RECONCILE_BUILD) {
+ // Save packages provided by the project
+ manager.setInternalPackageNames(install, project, packageCollector.getPackagesProvided());
+ }
+ monitor.subTask(Messages.TextMarkerCheckBuilder_retrievePackages);
+ // initialize manager caches after they are collected
+ final Set requiredPackages = packageCollector.getPackagesRequired();
+ if (!requiredPackages.isEmpty()) {
+ manager.getPathsForPackages(install, requiredPackages);
+ manager.getPathsForPackagesWithDeps(install, requiredPackages);
+ }
+ // process all modules
+ int remainingWork = resourceToModuleInfos.size();
+ for (Iterator i = resourceToModuleInfos.entrySet().iterator(); i.hasNext();) {
+ final Map.Entry entry = (Map.Entry) i.next();
+ final ISourceModule module = (ISourceModule) entry.getKey();
+ final ModuleInfo info = (ModuleInfo) entry.getValue();
+ monitor.subTask(NLS.bind(Messages.TextMarkerCheckBuilder_processing, module.getElementName(),
+ Integer.toString(remainingWork)));
+ codeModels.clear();
+ --remainingWork;
+ }
+ }
+
+ private CodeModel getCodeModel(ISourceModule module) {
+ CodeModel model = (CodeModel) codeModels.get(module);
+ if (model == null) {
+ try {
+ model = new CodeModel(module.getSource());
+ codeModels.put(module, model);
+ } catch (ModelException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+ }
+ return model;
+ }
+
+ private static Set getBuildpath(IScriptProject project) {
+ final IBuildpathEntry[] resolvedBuildpath;
+ try {
+ resolvedBuildpath = project.getResolvedBuildpath(true);
+ } catch (ModelException e1) {
+ TextMarkerPlugin.error(e1);
+ return Collections.EMPTY_SET;
+ }
+ final Set buildpath = new HashSet();
+ for (int i = 0; i < resolvedBuildpath.length; i++) {
+ final IBuildpathEntry entry = resolvedBuildpath[i];
+ if (entry.getEntryKind() == IBuildpathEntry.BPE_LIBRARY && entry.isExternal()) {
+ buildpath.add(EnvironmentPathUtils.getLocalPath(entry.getPath()));
+ }
+ }
+ return buildpath;
+ }
+
+ private void reportPackageProblem(TextMarkerPackageDeclaration pkg, IProblemReporter reporter,
+ ISourceModule module, String message, String pkgName) {
+ final CodeModel model = getCodeModel(module);
+ if (model == null) {
+ return;
+ }
+ reporter.reportProblem(new DefaultProblem(message, TextMarkerProblems.UNKNOWN_REQUIRED_PACKAGE,
+ new String[] { pkgName }, ProblemSeverities.Error, pkg.sourceStart(), pkg.sourceEnd(),
+ model.getLineNumber(pkg.sourceStart(), pkg.sourceEnd())));
+ }
+
+ private void checkPackage(ISourceModule module, TextMarkerPackageDeclaration pkg,
+ IProblemReporter reporter) {
+ final String packageName = pkg.getName();
+
+ if (packageCollector.getPackagesProvided().contains(packageName)) {
+ return;
+ }
+ if (!isValidPackageName(packageName)) {
+ return;
+ }
+
+ // Report unknown packages
+ if (!knownPackageNames.contains(packageName)) {
+ reportPackageProblem(pkg, reporter, module, NLS.bind(
+ Messages.TextMarkerCheckBuilder_unknownPackage, packageName), packageName);
+ return;
+ }
+
+ // Receive main package and it paths.
+ if (checkPackage(packageName)) {
+ reportPackageProblem(pkg, reporter, module, NLS.bind(
+ Messages.TextMarkerCheckBuilder_unresolvedDependencies, packageName), packageName);
+ return;
+ }
+
+ final Set dependencies = manager.getDependencies(packageName, install).keySet();
+ for (Iterator i = dependencies.iterator(); i.hasNext();) {
+ String pkgName = (String) i.next();
+ if (checkPackage(pkgName)) {
+ reportPackageProblem(pkg, reporter, module, NLS.bind(
+ Messages.TextMarkerCheckBuilder_unresolvedDependencies, packageName), packageName);
+ return;
+ }
+ }
+ }
+
+ static boolean isValidPackageName(String packageName) {
+ return packageName != null && packageName.length() != 0 && packageName.indexOf('$') == -1
+ && packageName.indexOf('[') == -1 && packageName.indexOf(']') == -1;
+ }
+
+ /**
+ * returns <code>true</code> on error
+ *
+ * @param packageName
+ * @return
+ */
+ private boolean checkPackage(String packageName) {
+ if (packageCollector.getPackagesProvided().contains(packageName)) {
+ return false;
+ }
+ return isOnBuildpath(buildpath, manager.getPathsForPackage(install, packageName));
+ }
+
+ private static boolean isOnBuildpath(Set buildpath, IPath path) {
+ if (!buildpath.contains(path)) {
+ for (Iterator i = buildpath.iterator(); i.hasNext();) {
+ IPath pp = (IPath) i.next();
+ if (pp.isPrefixOf(path)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ return true;
+ }
+
+ private static boolean isOnBuildpath(Set buildpath, IPath[] paths) {
+ if (paths != null) {
+ for (int i = 0; i < paths.length; i++) {
+ final IPath path = paths[i];
+ if (!isOnBuildpath(buildpath, path)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ public DependencyResponse getDependencies(int buildType, Set localElements, Set externalElements,
+ Set oldExternalFolders, Set externalFolders) {
+ if (buildType == FULL_BUILD || !oldExternalFolders.equals(externalFolders)) {
+ return DependencyResponse.FULL_EXTERNAL_BUILD;
+ } else {
+ return null;
+ }
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerCheckBuilder.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,18 @@
+package org.apache.uima.tm.dltk.internal.core.packages;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.builder.AbstractBuildParticipantType;
+import org.eclipse.dltk.core.builder.IBuildParticipant;
+
+public class TextMarkerPackageCheckerType extends AbstractBuildParticipantType {
+
+ @Override
+ public IBuildParticipant createBuildParticipant(IScriptProject project) throws CoreException {
+ try {
+ return new TextMarkerCheckBuilder(project);
+ } catch (IllegalStateException e) {
+ return null;
+ }
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackageCheckerType.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,137 @@
+package org.apache.uima.tm.dltk.internal.core.packages;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IAccessRule;
+import org.eclipse.dltk.core.IBuildpathAttribute;
+import org.eclipse.dltk.core.IBuildpathEntry;
+import org.eclipse.dltk.core.IInterpreterContainerExtension;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.environment.EnvironmentManager;
+import org.eclipse.dltk.core.environment.EnvironmentPathUtils;
+import org.eclipse.dltk.core.environment.IEnvironment;
+import org.eclipse.dltk.internal.core.BuildpathEntry;
+import org.eclipse.dltk.launching.IInterpreterInstall;
+import org.eclipse.dltk.launching.InterpreterContainerHelper;
+import org.eclipse.dltk.launching.ScriptRuntime;
+
+public class TextMarkerPackagesInterpreterContainerExtension implements
+ IInterpreterContainerExtension {
+
+ private static final IAccessRule[] EMPTY_RULES = new IAccessRule[0];
+
+ public TextMarkerPackagesInterpreterContainerExtension() {
+ }
+
+ public void processEntres(IScriptProject project, List buildpathEntries) {
+ IPath[] locations = null;
+ IInterpreterInstall install = null;
+ try {
+ install = ScriptRuntime.getInterpreterInstall(project);
+ List locs = new ArrayList();
+ for (Iterator iterator = buildpathEntries.iterator(); iterator.hasNext();) {
+ IBuildpathEntry entry = (IBuildpathEntry) iterator.next();
+ if (entry.getEntryKind() == IBuildpathEntry.BPE_LIBRARY && entry.isExternal()) {
+ locs.add(entry.getPath());
+ }
+ locations = (IPath[]) locs.toArray(new IPath[locs.size()]);
+ }
+ } catch (CoreException e) {
+ if (DLTKCore.DEBUG) {
+ e.printStackTrace();
+ }
+ }
+ if (install != null) {
+ Set<String> set = new HashSet<String>();
+ Set<String> autoSet = new HashSet<String>();
+ InterpreterContainerHelper.getInterpreterContainerDependencies(project, set, autoSet);
+ Set<String> packages = new HashSet<String>();
+ packages.addAll(set);
+ packages.addAll(autoSet);
+ if (set.size() == 0) {
+ return;
+ }
+ IEnvironment env = EnvironmentManager.getEnvironment(project);
+ // This is very slow if no information is available.
+ Set allPaths = new HashSet();
+ // for (Iterator iterator = packages.iterator();
+ // iterator.hasNext();) {
+ // String pkgName = (String) iterator.next();
+ IPath[] libs = PackagesManager.getInstance().getPathsForPackagesWithDeps(install, packages);
+ for (int i = 0; i < libs.length; i++) {
+ IPath fullPath = EnvironmentPathUtils.getFullPath(env, libs[i]);
+ allPaths.add(fullPath);
+ }
+ // }
+
+ Set rawEntries = new HashSet(allPaths.size());
+ for (Iterator iterator = allPaths.iterator(); iterator.hasNext();) {
+ IPath entryPath = (IPath) iterator.next();
+
+ if (!entryPath.isEmpty()) {
+ // TODO Check this
+ // resolve symlink
+ // {
+ // IFileHandle f = env.getFile(entryPath);
+ // if (f == null)
+ // continue;
+ // entryPath = new Path(f.getCanonicalPath());
+ // }
+ if (rawEntries.contains(entryPath))
+ continue;
+
+ /*
+ * if (!entryPath.isAbsolute()) Assert.isTrue(false, "Path for IBuildpathEntry must be
+ * absolute"); //$NON-NLS-1$
+ */
+ IBuildpathAttribute[] attributes = new IBuildpathAttribute[0];
+ ArrayList excluded = new ArrayList(); // paths to exclude
+ for (Iterator iterator2 = allPaths.iterator(); iterator2.hasNext();) {
+ IPath otherPath = (IPath) iterator2.next();
+ if (otherPath.isEmpty())
+ continue;
+ // TODO Check this
+ // resolve symlink
+ // {
+ // IFileHandle f = env.getFile(entryPath);
+ // if (f == null)
+ // continue;
+ // entryPath = new Path(f.getCanonicalPath());
+ // }
+ // compare, if it contains some another
+ if (entryPath.isPrefixOf(otherPath) && !otherPath.equals(entryPath)) {
+ IPath pattern = otherPath.removeFirstSegments(entryPath.segmentCount()).append("*");
+ if (!excluded.contains(pattern)) {
+ excluded.add(pattern);
+ }
+ }
+ }
+ boolean inInterpreter = false;
+ if (locations != null) {
+ for (int i = 0; i < locations.length; i++) {
+ IPath path = locations[i];
+ if (path.isPrefixOf(entryPath)) {
+ inInterpreter = true;
+ break;
+ }
+ }
+ }
+ if (!inInterpreter) {
+ // Check for interpreter container libraries.
+ buildpathEntries.add(DLTKCore.newLibraryEntry(entryPath, EMPTY_RULES, attributes,
+ BuildpathEntry.INCLUDE_ALL, (IPath[]) excluded.toArray(new IPath[excluded
+ .size()]), false, true));
+ rawEntries.add(entryPath);
+ }
+ }
+ }
+ }
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/TextMarkerPackagesInterpreterContainerExtension.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties Mon Aug 1 14:06:27 2011
@@ -0,0 +1,5 @@
+TextMarkerCheckBuilder_interpreterNotFound=Interpreter associated with the {0} project is not found
+TextMarkerCheckBuilder_processing=processing {1}: {0}
+TextMarkerCheckBuilder_retrievePackages=retrieve package paths...
+TextMarkerCheckBuilder_unknownPackage=Unknown package: {0}
+TextMarkerCheckBuilder_unresolvedDependencies=Package {0} has unresolved dependencies.
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/packages/messages.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,192 @@
+package org.apache.uima.tm.dltk.internal.core.parser;
+
+import org.antlr.runtime.CommonToken;
+import org.antlr.runtime.Token;
+import org.apache.uima.tm.dltk.parser.ast.TMTypeConstants;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.expressions.StringLiteral;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.ast.statements.Statement;
+import org.eclipse.dltk.core.IField;
+import org.eclipse.dltk.core.IMember;
+import org.eclipse.dltk.core.IMethod;
+import org.eclipse.dltk.core.IType;
+import org.eclipse.dltk.core.ModelException;
+
+
+public class TextMarkerParseUtils {
+
+ public static int endLineOrSymbol(int from, String content) {
+ int pos = 0;
+ for (pos = from; pos < content.length(); ++pos) {
+ char c = content.charAt(pos);
+ if (c == '\n' || c == '\r' || c == ';') {
+ return pos;
+ }
+ if (!Character.isWhitespace(c)) {
+ return pos;
+ }
+ }
+ if (pos == content.length()) {
+ return pos;
+ }
+ return from;
+ }
+
+ public static int startLineOrSymbol(int from, String content) {
+ if (from == -1) {
+ from = 0;
+ }
+ if (from >= content.length())
+ from--;
+ for (int pos = from - 1; pos > 0; --pos) {
+ char c = content.charAt(pos);
+ if (c == '\n' || c == '\r' || c == ';') {
+ return pos + 1;
+ }
+ if (!Character.isWhitespace(c)) {
+ return pos + 1;
+ }
+ }
+ return from;
+ }
+
+ public static int endLineOrNoSymbol(int from, String content) {
+ int pos = 0;
+ if (from == -1) {
+ from = 0;
+ }
+ if (from >= content.length())
+ from--;
+ for (pos = from; pos < content.length(); ++pos) {
+ if (checkBounds(content, pos)) {
+ return pos;
+ }
+ }
+ if (pos == content.length()) {
+ return pos;
+ }
+ return pos;
+ }
+
+ private static boolean checkBounds(String content, int pos) {
+ char[] syms = { ' ', '\t', '\n', '\r', ']', '[', '}', '{', '(', ')' };
+ char c = content.charAt(pos);
+ for (int i = 0; i < syms.length; ++i) {
+ if (syms[i] == c) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static int startLineOrNoSymbol(int from, String content) {
+ if (from == -1) {
+ from = 0;
+ }
+ if (from >= content.length())
+ from--;
+ int pos;
+ for (pos = from; pos > 0; --pos) {
+ if (checkBounds(content, pos)) {
+ return pos + 1;
+ }
+ }
+ return pos;
+ }
+
+ public static String[] returnVariable(Statement node) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static SimpleReference extractVariableFromString(int sourceStart, int sourceEnd, int i,
+ String name) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static SimpleReference extractVariableFromString(StringLiteral completionNode, int pos) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static ASTNode getScopeParent(ModuleDeclaration module, Expression s) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static String processFieldName(IField field, String token) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static String processMethodName(IMethod method, String token) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static String processTypeName(IType method, String token) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ * @param token
+ * @return (start, end)
+ * @throws IllegalArgumentException
+ * when token==null or !(token instanceof CommonToken)
+ */
+ public static final int[] getBounds(Token token) throws IllegalArgumentException {
+ if (token == null) {
+ throw new IllegalArgumentException();
+ }
+ if (!(token instanceof CommonToken)) {
+ throw new IllegalArgumentException();
+ }
+ CommonToken ct = (CommonToken) token;
+ int[] bounds = { ct.getStartIndex(), ct.getStopIndex() + 1 };
+ return bounds;
+ }
+
+ /**
+ * @param tokenA
+ * startToken
+ * @param tokenB
+ * endToken
+ * @return positions of a.start // b.end
+ * @throws IllegalArgumentException
+ * when some token is null or not instanceof CommonToken
+ */
+ public static final int[] getBounds(Token tokenA, Token tokenB) throws IllegalArgumentException {
+ if (!((tokenA instanceof CommonToken) && (tokenB instanceof CommonToken || tokenB == null))) {
+ throw new IllegalArgumentException();
+ }
+ CommonToken ctA = (CommonToken) tokenA;
+ if (tokenB == null) {
+ int[] bounds = { ctA.getStartIndex(), ctA.getStopIndex() + 1 };
+ return bounds;
+ } else {
+ CommonToken ctB = (CommonToken) tokenB;
+ int[] bounds = { ctA.getStartIndex(), ctB.getStopIndex() + 1 };
+ return bounds;
+ }
+ }
+
+ /**
+ * @param member
+ * @return see {@link TMTypeConstants}, -1 if not valid
+ */
+ public static final int getTypeOfIModelElement(IMember member) {
+ int type;
+ try {
+ type = member.getFlags();
+ } catch (ModelException e) {
+ return -1;
+ }
+ type &= TMTypeConstants.TM_TYPE_BITMASK;
+ return type;
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerParseUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,27 @@
+package org.apache.uima.tm.dltk.internal.core.parser;
+
+import org.apache.uima.tm.dltk.core.TextMarkerNature;
+import org.apache.uima.tm.dltk.internal.core.parser.visitor.TextMarkerSourceElementRequestVisitor;
+import org.eclipse.dltk.compiler.SourceElementRequestVisitor;
+import org.eclipse.dltk.core.AbstractSourceElementParser;
+
+
+public class TextMarkerSourceElementParser extends AbstractSourceElementParser {
+
+ /*
+ * @see org.eclipse.dltk.core.AbstractSourceElementParser#createVisitor()
+ */
+ @Override
+ protected SourceElementRequestVisitor createVisitor() {
+ return new TextMarkerSourceElementRequestVisitor(getRequestor());
+ }
+
+ /*
+ * @see org.eclipse.dltk.core.AbstractSourceElementParser#getNatureId()
+ */
+ @Override
+ protected String getNatureId() {
+ return TextMarkerNature.NATURE_ID;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceElementParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,124 @@
+package org.apache.uima.tm.dltk.internal.core.parser;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.Token;
+import org.apache.uima.tm.dltk.core.ITextMarkerKeywords;
+import org.apache.uima.tm.dltk.core.TextMarkerKeywordsManager;
+import org.apache.uima.tm.dltk.internal.core.builder.DescriptorManager;
+import org.apache.uima.tm.dltk.internal.core.parsers.DLTKTextMarkerErrorReporter;
+import org.apache.uima.tm.dltk.internal.core.parsers.DLTKTokenConverter;
+import org.apache.uima.tm.dltk.internal.core.parsers.TextMarkerParser;
+import org.apache.uima.tm.dltk.parser.ast.TextMarkerModuleDeclaration;
+import org.apache.uima.tm.textmarker.parser.TextMarkerLexer;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.parser.AbstractSourceParser;
+import org.eclipse.dltk.compiler.problem.IProblemReporter;
+import org.eclipse.dltk.core.DLTKCore;
+
+
+public class TextMarkerSourceParser extends AbstractSourceParser {
+
+ private CommonTokenStream tokenStream;
+
+ private IProblemReporter problemReporter = null;
+
+ public TextMarkerSourceParser(/* IProblemReporter reporter */) {
+ // this.problemReporter = reporter;
+ }
+
+ public static class TMLexer extends TextMarkerLexer {
+ public TMLexer(CharStream lexer) {
+ super(lexer);
+ }
+
+ @Override
+ public Token nextToken() {
+ startPos = getCharPositionInLine();
+ return super.nextToken();
+ }
+ }
+
+ public ModuleDeclaration parse(char[] fileName, char[] content, IProblemReporter reporter) {// throws
+ this.problemReporter = reporter;
+
+ TextMarkerModuleDeclaration moduleDeclaration = new TextMarkerModuleDeclaration(content.length,
+ true);
+
+ CharStream st = new ANTLRStringStream(new String(content));
+ TextMarkerLexer lexer = new TMLexer(st);
+
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ this.tokenStream = tokens;
+
+ TextMarkerParser parser = new TextMarkerParser(this.tokenStream);
+
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.BASIC)) {
+ parser.addPredefinedType(each);
+ }
+ // TODO refacor , also in grammar
+ List<String> variables = parser.getVariables();
+ Map<String, String> variableTypes = parser.getVariableTypes();
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.CONDITION)) {
+ variables.add(each);
+ variableTypes.put(each, "CONDITION");
+ }
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.ACTION)) {
+ variables.add(each);
+ variableTypes.put(each, "ACTION");
+ }
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.BOOLEANFUNCTION)) {
+ variables.add(each);
+ variableTypes.put(each, "BOOLEANFUNCTION");
+ }
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.NUMBERFUNCTION)) {
+ variables.add(each);
+ variableTypes.put(each, "NUMBERFUNCTION");
+ }
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.STRINGFUNCTION)) {
+ variables.add(each);
+ variableTypes.put(each, "STRINGFUNCTION");
+ }
+ for (String each : TextMarkerKeywordsManager.getKeywords(ITextMarkerKeywords.TYPEFUNCTION)) {
+ variables.add(each);
+ variableTypes.put(each, "TYPEFUNCTION");
+ }
+ parser.addPredefinedType("Document");
+ parser.addPredefinedType("Annotation");
+
+ parser.md = moduleDeclaration;
+ parser.length = content.length;
+ parser.converter = new DLTKTokenConverter(content);
+ parser.reporter = new DLTKTextMarkerErrorReporter(parser.converter, problemReporter, parser);
+
+ parser.descriptor = new DescriptorManager();
+
+ moduleDeclaration.descriptorInfo = parser.descriptor;
+
+ String name = "Dynamic";
+ if (fileName != null) {
+ File fn = new File(new String(fileName));
+ name = fn.getName();
+ int lastIndexOf = name.lastIndexOf(".tm");
+ name = name.substring(0, lastIndexOf);
+ }
+ try {
+ parser.file_input(name);
+ } catch (Throwable e) {
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+ return moduleDeclaration;
+ }
+
+ public CommonTokenStream getTokenStream() {
+ return tokenStream;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,12 @@
+package org.apache.uima.tm.dltk.internal.core.parser;
+
+import org.eclipse.dltk.ast.parser.ISourceParser;
+import org.eclipse.dltk.ast.parser.ISourceParserFactory;
+
+public class TextMarkerSourceParserFactory implements ISourceParserFactory {
+
+ public ISourceParser createSourceParser() {
+ return new TextMarkerSourceParser();
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerSourceParserFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,60 @@
+package org.apache.uima.tm.dltk.internal.core.parser;
+
+import org.apache.uima.tm.dltk.core.TextMarkerPlugin;
+import org.apache.uima.tm.dltk.parser.ast.TextMarkerScriptBlock;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.compiler.task.ITodoTaskPreferences;
+import org.eclipse.dltk.compiler.task.TodoTaskPreferences;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.builder.AbstractTodoTaskBuildParticipantType;
+import org.eclipse.dltk.core.builder.IBuildParticipant;
+
+
+public class TextMarkerTodoParserType extends AbstractTodoTaskBuildParticipantType {
+
+ // @Override
+ // protected ITodoTaskPreferences getPreferences(IScriptProject project) {
+ // return new TodoTaskPreferencesOnPreferenceLookupDelegate(TextMarkerPlugin.PLUGIN_ID, project);
+ // }
+
+ @Override
+ protected ITodoTaskPreferences getPreferences(IScriptProject project) {
+ return new TodoTaskPreferences(TextMarkerPlugin.getDefault().getPluginPreferences());
+ }
+
+ @Override
+ protected IBuildParticipant getBuildParticipant(ITodoTaskPreferences preferences) {
+ return new TextMarkerTodoTaskAstParser(preferences);
+ }
+
+ private static class TextMarkerTodoTaskAstParser extends TodoTaskBuildParticipant implements
+ IBuildParticipant {
+
+ public TextMarkerTodoTaskAstParser(ITodoTaskPreferences preferences) {
+ super(preferences);
+ }
+
+ @Override
+ protected boolean isSimpleNode(ASTNode node) {
+ if (node instanceof TextMarkerScriptBlock) {
+ return true;
+ }
+ return super.isSimpleNode(node);
+ }
+
+ @Override
+ protected int findCommentStart(char[] content, int begin, int end) {
+ if (!isCheckRanges()) {
+ return super.findCommentStart(content, begin, end);
+ }
+ for (int i = begin; i < end - 1; ++i) {
+ if (content[i] == '/' && isValid(i)) {
+ return i + 2;
+ }
+ }
+ return -1;
+ }
+
+ }
+
+}
\ No newline at end of file
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/TextMarkerTodoParserType.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,275 @@
+package org.apache.uima.tm.dltk.internal.core.parser.visitor;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.uima.tm.dltk.parser.ast.TMTypeConstants;
+import org.apache.uima.tm.dltk.parser.ast.TextMarkerImportStatement;
+import org.apache.uima.tm.dltk.parser.ast.declarations.TextMarkerPackageDeclaration;
+import org.apache.uima.tm.dltk.parser.ast.declarations.TextMarkerTypeDeclaration;
+import org.apache.uima.tm.dltk.parser.ast.declarations.TextMarkerVariableDeclaration;
+import org.apache.uima.tm.dltk.parser.ast.expressions.TextMarkerBasicAnnotationType;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.Modifiers;
+import org.eclipse.dltk.ast.PositionInformation;
+import org.eclipse.dltk.ast.declarations.Argument;
+import org.eclipse.dltk.ast.declarations.FieldDeclaration;
+import org.eclipse.dltk.ast.declarations.MethodDeclaration;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.ast.references.VariableReference;
+import org.eclipse.dltk.ast.statements.Statement;
+import org.eclipse.dltk.compiler.ISourceElementRequestor;
+import org.eclipse.dltk.compiler.SourceElementRequestVisitor;
+
+
+public class TextMarkerSourceElementRequestVisitor extends SourceElementRequestVisitor {
+
+ private static class TypeField {
+ private String fName;
+
+ private String fInitValue;
+
+ private PositionInformation fPos;
+
+ private Expression fExpression;
+
+ private ASTNode fToNode;
+
+ private ASTNode declaredIn; // The node where the declaration was found
+
+ // (should be either class or method node)
+ TypeField(String name, String initValue, PositionInformation pos, Expression expression,
+ ASTNode toNode, ASTNode declaredIn) {
+
+ this.fName = name;
+ this.fInitValue = initValue;
+ this.fPos = pos;
+ this.fExpression = expression;
+ this.fToNode = toNode;
+ this.declaredIn = declaredIn;
+ }
+
+ String getName() {
+
+ return this.fName;
+ }
+
+ String getInitValue() {
+
+ return this.fInitValue;
+ }
+
+ PositionInformation getPos() {
+
+ return this.fPos;
+ }
+
+ Expression getExpression() {
+
+ return this.fExpression;
+ }
+
+ ASTNode getToNode() {
+
+ return this.fToNode;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+
+ if (obj instanceof TypeField) {
+ TypeField second = (TypeField) obj;
+ return second.fName.equals(this.fName) && second.fToNode.equals(this.fToNode);
+ }
+ return super.equals(obj);
+ }
+
+ @Override
+ public String toString() {
+
+ return this.fName;
+ }
+
+ public ASTNode getDeclaredIn() {
+ return declaredIn;
+ }
+
+ }
+
+ private static String ANONYMOUS_LAMBDA_FORM_MARKER = "<anonymous>";
+
+ // Used to prehold fields if adding in methods.
+ private List fNotAddedFields = new ArrayList();
+
+ private String lastLambdaFormName = ANONYMOUS_LAMBDA_FORM_MARKER;
+
+ /**
+ * Used to determine duplicate names.
+ */
+ private Map fTypeVariables = new HashMap();
+
+ //
+ public TextMarkerSourceElementRequestVisitor(ISourceElementRequestor requestor) {
+ super(requestor);
+ }
+
+ protected String makeLanguageDependentValue(Expression value) {
+ String outValue = "";
+ return outValue;
+ }
+
+ @Override
+ protected void onEndVisitMethod(MethodDeclaration method) {
+ if (fNotAddedFields.size() >= 1) {
+ TypeField typeField = (TypeField) fNotAddedFields.get(0);
+ if (null != typeField && typeField.getDeclaredIn().equals(method)) {
+ Iterator i = this.fNotAddedFields.iterator();
+ while (i.hasNext()) {
+ TypeField field = (TypeField) i.next();
+ if (canAddVariables(field.getToNode(), field.getName())) {
+
+ PositionInformation pos = field.getPos();
+
+ ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo();
+ info.modifiers = Modifiers.AccStatic;
+ info.name = field.getName();
+ info.nameSourceEnd = pos.nameEnd - 2;
+ info.nameSourceStart = pos.nameStart;
+ info.declarationStart = pos.sourceStart;
+ this.fRequestor.enterField(info);
+ this.fRequestor.exitField(pos.sourceEnd);
+
+ }
+ }
+ this.fNotAddedFields.clear();
+ }
+ }
+ }
+
+ @Override
+ public boolean visit(Statement statement) throws Exception {
+ super.visit(statement);
+ if (statement instanceof TextMarkerPackageDeclaration) {
+ this.processPackage(statement);
+ super.fNodes.pop();
+ return false;
+ }
+ if (statement instanceof FieldDeclaration) {
+ FieldDeclaration fieldDecl = (FieldDeclaration) statement;
+ processFieldDeclaration(fieldDecl.getRef(), fieldDecl);
+ super.fNodes.pop();
+ return false;
+ }
+ // TODO handle tm-import statement
+ if (statement instanceof TextMarkerImportStatement) {
+ TextMarkerImportStatement tmImport = (TextMarkerImportStatement) statement;
+ super.fNodes.pop();
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean visit(Expression expression) throws Exception {
+ if (expression instanceof VariableReference) {
+ VariableReference varRef = (VariableReference) expression;
+ this.fRequestor.acceptFieldReference(varRef.getName().toCharArray(), varRef.sourceStart());
+ }
+ return super.visit(expression);
+ }
+
+ private void processPackage(Statement statement) {
+ TextMarkerPackageDeclaration pack = (TextMarkerPackageDeclaration) statement;
+ this.fRequestor.acceptPackage(pack.getNameStart(), pack.getNameEnd(), (pack.getName())
+ .toCharArray());
+ }
+
+ /**
+ * @param variableIDRef
+ * @param fullDeclaration
+ * @author Martin Toepfer
+ */
+ private void processFieldDeclaration(SimpleReference variableIDRef, Statement fullDeclaration) {
+ int modifier = Modifiers.AccDefault;
+ if (fullDeclaration instanceof TextMarkerVariableDeclaration) {
+ modifier = Modifiers.AccPrivate;
+ modifier |= ((TextMarkerVariableDeclaration) fullDeclaration).getType();
+ } else if (fullDeclaration instanceof TextMarkerBasicAnnotationType) {
+ modifier = Modifiers.AccConstant;
+ modifier |= TMTypeConstants.TM_TYPE_AT;
+ } else if (fullDeclaration instanceof TextMarkerTypeDeclaration) {
+ modifier = Modifiers.AccPublic;
+ modifier |= TMTypeConstants.TM_TYPE_AT;
+ }
+ if (canAddVariables((ASTNode) this.fNodes.peek(), variableIDRef.getName())) {
+ ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo();
+ info.modifiers = modifier;
+ info.name = variableIDRef.getName();
+ info.nameSourceEnd = variableIDRef.sourceEnd() - 1;
+ info.nameSourceStart = variableIDRef.sourceStart();
+ info.declarationStart = variableIDRef.sourceStart();
+ this.fRequestor.enterField(info);
+ if (fullDeclaration != null) {
+ this.fRequestor.exitField(fullDeclaration.sourceEnd());
+ } else {
+ this.fRequestor.exitField(variableIDRef.sourceEnd());
+ }
+ }
+ }
+
+ private boolean canAddVariables(ASTNode type, String name) {
+ if (this.fTypeVariables.containsKey(type)) {
+ List variables = (List) this.fTypeVariables.get(type);
+ if (variables.contains(name)) {
+ return false;
+ }
+ variables.add(name);
+ return true;
+ } else {
+ List variables = new ArrayList();
+ variables.add(name);
+ this.fTypeVariables.put(type, variables);
+ return true;
+ }
+ }
+
+ @Override
+ public boolean endvisit(Statement s) throws Exception {
+ return true;
+ }
+
+ @Override
+ public boolean visit(MethodDeclaration method) throws Exception {
+ this.fNodes.push(method);
+ List<Argument> args = method.getArguments();
+ String[] parameter = new String[args.size()];
+ for (int a = 0; a < args.size(); a++) {
+ Argument arg = args.get(a);
+ parameter[a] = arg.getName();
+ }
+
+ ISourceElementRequestor.MethodInfo mi = new ISourceElementRequestor.MethodInfo();
+ mi.parameterNames = parameter;
+ mi.name = method.getName();
+ mi.modifiers = method.getModifiers();
+ mi.nameSourceStart = method.getNameStart();
+ mi.nameSourceEnd = method.getNameEnd() - 1;
+ mi.declarationStart = method.sourceStart();
+
+ this.fRequestor.enterMethodRemoveSame(mi);
+
+ this.fInMethod = true;
+ this.fCurrentMethod = method;
+ return true;
+ }
+
+ @Override
+ public boolean visit(ModuleDeclaration declaration) throws Exception {
+ return super.visit(declaration);
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerSourceElementRequestVisitor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java?rev=1152783&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java Mon Aug 1 14:06:27 2011
@@ -0,0 +1,94 @@
+package org.apache.uima.tm.dltk.internal.core.parser.visitor;
+//package org.apache.uima.tm.dltk.internal.core.parser.visitor;
+//
+//import java.util.List;
+//
+//import org.eclipse.dltk.ast.ASTVisitor;
+//import org.eclipse.dltk.ast.declarations.MethodDeclaration;
+//import org.eclipse.dltk.ast.expressions.Expression;
+//import org.eclipse.dltk.ast.statements.Statement;
+//
+//public class TextMarkerVersionReportVisitor extends ASTVisitor {
+//
+// /**
+// * 1.5.x and all before it.
+// */
+// public static final int VERSION_15x = 0x0105;
+//
+// /**
+// * Added strict assignments. +=, -=, *=, /=, %=, &= |=, ^=, <<=, >>=, **=
+// */
+// public static final int VERSION_20x = 0x0200;
+//
+// /**
+// * Added import as expressions.
+// */
+// public static final int VERSION_21x = 0x0201;
+//
+// /**
+// * Removed ^= Added //= In term added //.
+// *
+// */
+// public static final int VERSION_22x = 0x0202;
+//
+// public static final int VERSION_23x = 0x0203;
+//
+// /**
+// * Decorators Added.
+// */
+// public static final int VERSION_24x = 0x0204;
+//
+// private int fCurrentVersion = VERSION_15x;
+//
+// private int fProjectVersion = 0;
+//
+// /**
+// * Construct version detect visitor.
+// *
+// * TODO: Add reporting handler here.
+// */
+// public TextMarkerVersionReportVisitor(int version) {
+// this.fProjectVersion = version;
+// }
+//
+// public int getRequredVersion() {
+// return this.fCurrentVersion;
+// }
+//
+// public String getRequestedVersionStr() {
+// return this.getVersionStr(this.fCurrentVersion);
+// }
+//
+// public String getVersionStr(int version) {
+// return "TextMarker version not determined.";
+// }
+//
+// public boolean visit(Expression expression) throws Exception {
+//
+// return true;
+// }
+//
+// public boolean visit(MethodDeclaration s) throws Exception {
+// List decorators = s.getDecorators();
+// if (decorators != null && decorators.size() > 0) {
+// this.requreVersion(VERSION_24x, s);
+// }
+// return true;
+// }
+//
+// private void requreVersion(int version, Statement statement) {
+//
+// if (this.fCurrentVersion < version) {
+// this.fCurrentVersion = version;
+// }
+// if (this.fCurrentVersion > this.fProjectVersion) {
+////
+// // TODO:: Add version error setting here.
+// }
+// }
+//
+// public boolean visit(Statement statement) throws Exception {
+//
+// return true;
+// }
+// }
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.dltk.core/src/main/java/org/apache/uima/tm/dltk/internal/core/parser/visitor/TextMarkerVersionReportVisitor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain