1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 22:25:25 +02:00

Added support to look in jarred plugin for patterns file.

This commit is contained in:
David Dykstal 2006-04-28 15:45:56 +00:00
parent 50a3f9dd3f
commit f36c370794
3 changed files with 93 additions and 251 deletions

View file

@ -13,202 +13,100 @@
* Contributors:
* {Name} (company) - description of contribution.
********************************************************************************/
package org.eclipse.rse.internal.services.local.shells;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PluginVersionIdentifier;
import org.eclipse.rse.services.local.Activator;
import org.osgi.framework.Bundle;
public class Patterns
{
public class Patterns {
private ArrayList _theCommands;
private String _currentCommand;
private String _pluginsPath;
private String _version;
private long _timeStamp = 0;
private File _thePatternsFile;
private static String PATTERNS_PACKAGE = "org.eclipse.rse.services.local";
private static String PATTERNS_FILE = "patterns.dat";
public Patterns()
{
public Patterns() {
_theCommands = new ArrayList();
parsePatternsFile();
parsePatterns();
}
protected String getPatternsFilePath(Bundle bundle)
{
URL pluginsURL = bundle.getEntry("/");
String path = null;
try
{
path = Platform.resolve(pluginsURL).getPath();
File systemsPluginDir = new File(path);
path = systemsPluginDir.getParentFile().getAbsolutePath();
}
catch (IOException e)
{
}
return path;
}
private String getPatternsFilePath()
{
if (_pluginsPath == null)
{
Bundle bundle = Activator.getDefault().getBundle();
_pluginsPath = getPatternsFilePath(bundle);
String version = (String)(bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION));
_version = (new PluginVersionIdentifier(version)).toString();
}
return _pluginsPath;
}
public void refresh(String theCommand)
{
_currentCommand = theCommand;
parsePatternsFile();
}
public void update(String theCommand)
{
_currentCommand = theCommand;
}
private File getPatternsFile()
{
if (_thePatternsFile == null)
{
String pluginDir = getPatternsFilePath();
File thePatternsFile = new File(pluginDir + "/" + PATTERNS_PACKAGE + "/" + PATTERNS_FILE);
if (!thePatternsFile.exists())
{
thePatternsFile = new File(pluginDir + "/" + PATTERNS_PACKAGE + "_" + _version + "/" + PATTERNS_FILE);
if (!thePatternsFile.exists())
{
File parentFile = new File(pluginDir);
if (parentFile.exists())
{
// now we're really desparate!
// search for a file that looks like it
File[] files = parentFile.listFiles();
for (int i = 0; i < files.length && !thePatternsFile.exists(); i++)
{
File c = files[i];
if (c.getName().startsWith(PATTERNS_PACKAGE))
{
thePatternsFile = c;
}
private void parsePatterns() {
Bundle bundle = Activator.getDefault().getBundle();
URL patterns = bundle.getEntry("/patterns.dat");
if (patterns != null) {
try {
InputStream in = patterns.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
_theCommands.clear();
String curLine;
CommandPattern curCommand = null;
while ((curLine = reader.readLine()) != null) {
curLine = curLine.trim();
// Skip the current line if it is empty or starts with a #
if ((curLine.length() == 0) || (curLine.charAt(0) == '#')) {
continue;
}
// Check if this line is the start of a new command section
if (curLine.startsWith("command")) {
int colon = curLine.indexOf(":");
// Check that there is something after the colon
if (colon == (curLine.length() - 1)) {
continue;
}
Pattern thePattern = Pattern.compile(curLine.substring(colon + 1, curLine.length()).trim());
curCommand = new CommandPattern(thePattern);
_theCommands.add(curCommand);
}
// If we get here, the line must be an output pattern
else {
int firstSpace = curLine.indexOf(" ");
int patternWord = curLine.indexOf("pattern");
int firstEquals = curLine.indexOf("=");
if ((firstEquals == -1) || (firstEquals == (curLine.length() - 1))) {
continue;
}
String objType = curLine.substring(0, firstSpace);
String matchOrder = curLine.substring(firstSpace + 1, patternWord).trim();
String patternString = curLine.substring(firstEquals + 1, curLine.length());
Pattern thePattern = Pattern.compile(patternString.trim());
if (curCommand != null) {
curCommand.addOutputPattern(new OutputPattern(objType, matchOrder, thePattern));
}
}
}
in.close();
} catch (IOException e) {
Activator.getDefault().logException(e);
}
_thePatternsFile = thePatternsFile;
}
return _thePatternsFile;
}
private void parsePatternsFile()
{
File thePatternsFile = getPatternsFile();
long newTimeStamp = 0;
if (!thePatternsFile.exists() || ((newTimeStamp = thePatternsFile.lastModified()) == _timeStamp))
return;
_timeStamp = newTimeStamp;
//If we get here, we are actually going to read\parse the file.
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(thePatternsFile));
_theCommands.clear();
String curLine;
CommandPattern curCommand = null;
//Main Loop that reads each line.
while ((curLine = reader.readLine()) != null)
{
curLine = curLine.trim();
//Skip the current line if it is empty or starts with a #
if ((curLine.length() == 0) || (curLine.charAt(0) == '#'))
continue;
//Check if this line is the start of a new command section
if (curLine.startsWith("command"))
{
int colon = curLine.indexOf(":");
//Check that there is something after the colon
if (colon == (curLine.length() - 1))
continue;
Pattern thePattern = Pattern.compile(curLine.substring(colon + 1, curLine.length()).trim());
curCommand = new CommandPattern(thePattern);
_theCommands.add(curCommand);
}
//If we get here, the line must be an output pattern
else
{
int firstSpace = curLine.indexOf(" ");
int patternWord = curLine.indexOf("pattern");
int firstEquals = curLine.indexOf("=");
if ((firstEquals == -1) || (firstEquals == (curLine.length() - 1)))
continue;
String objType = curLine.substring(0, firstSpace);
String matchOrder = curLine.substring(firstSpace + 1, patternWord).trim();
String patternString = curLine.substring(firstEquals + 1, curLine.length());
Pattern thePattern = Pattern.compile(patternString.trim());
if (curCommand != null)
curCommand.addOutputPattern(new OutputPattern(objType, matchOrder, thePattern));
}
}
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
return;
}
catch (IOException e)
{
System.out.println(e.getMessage());
return;
}
}
public ParsedOutput matchLine(String theLine)
{
public void refresh(String theCommand) {
_currentCommand = theCommand;
parsePatterns();
}
public void update(String theCommand) {
_currentCommand = theCommand;
}
public ParsedOutput matchLine(String theLine) {
CommandPattern curCommand;
ParsedOutput matchedOutput = null;
int commands = _theCommands.size();
if (_currentCommand != null)
{
for (int i = 0; i < commands; i++)
{
if (_currentCommand != null) {
for (int i = 0; i < commands; i++) {
curCommand = (CommandPattern) _theCommands.get(i);
if (curCommand.matchCommand(_currentCommand))
if (curCommand.matchCommand(_currentCommand)) {
matchedOutput = curCommand.matchLine(theLine);
if (matchedOutput != null)
}
if (matchedOutput != null) {
return matchedOutput;
}
}
}
return null;

View file

@ -17,6 +17,9 @@
package org.eclipse.rse.services.local;
import org.eclipse.ui.plugin.*;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;
@ -28,6 +31,24 @@ public class Activator extends AbstractUIPlugin {
//The shared instance.
private static Activator plugin;
/**
* Returns the shared instance.
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("com.ibm.rse.services.files.local", path);
}
/**
* The constructor.
*/
@ -51,20 +72,13 @@ public class Activator extends AbstractUIPlugin {
}
/**
* Returns the shared instance.
* Logs an throwable to the log for this plugin.
* @param t the Throwable to be logged.
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("com.ibm.rse.services.files.local", path);
public void logException(Throwable t) {
ILog log = getLog();
String id = getBundle().getSymbolicName();
IStatus status = new Status(IStatus.ERROR, id, 0, "Unexpected exception", t);
log.log(status);
}
}

View file

@ -1,70 +0,0 @@
/********************************************************************************
* Copyright (c) 2006 IBM Corporation. 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
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* {Name} (company) - description of contribution.
********************************************************************************/
package org.eclipse.rse.services.local.shells;
import org.eclipse.ui.plugin.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;
/**
* The main plugin class to be used in the desktop.
*/
public class Activator extends AbstractUIPlugin {
//The shared instance.
private static Activator plugin;
/**
* The constructor.
*/
public Activator() {
plugin = this;
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.rse.services.shells.local", path);
}
}