From a809daca8e35b07e70a75a9d46ccc9121fb329c4 Mon Sep 17 00:00:00 2001
From: Michael Valenta
Date: Mon, 19 Feb 2007 16:03:41 +0000
Subject: *** empty log message ***
---
bundles/org.eclipse.core.net/.classpath | 7 +
bundles/org.eclipse.core.net/.cvsignore | 1 +
bundles/org.eclipse.core.net/.project | 28 ++
.../.settings/org.eclipse.jdt.core.prefs | 73 ++++
bundles/org.eclipse.core.net/META-INF/MANIFEST.MF | 15 +
bundles/org.eclipse.core.net/build.properties | 8 +
bundles/org.eclipse.core.net/plugin.properties | 19 +
.../org/eclipse/net/core/IProxyChangeEvent.java | 75 ++++
.../org/eclipse/net/core/IProxyChangeListener.java | 27 ++
.../src/org/eclipse/net/core/IProxyData.java | 143 ++++++++
.../src/org/eclipse/net/core/IProxyManager.java | 128 +++++++
.../src/org/eclipse/net/core/NetCore.java | 38 ++
.../eclipse/net/internal/core/NetCorePlugin.java | 70 ++++
.../net/internal/core/ProxyChangeEvent.java | 53 +++
.../org/eclipse/net/internal/core/ProxyData.java | 76 ++++
.../eclipse/net/internal/core/ProxyManager.java | 242 +++++++++++++
.../org/eclipse/net/internal/core/ProxyType.java | 291 +++++++++++++++
.../eclipse/net/internal/core/StringMatcher.java | 395 +++++++++++++++++++++
18 files changed, 1689 insertions(+)
create mode 100644 bundles/org.eclipse.core.net/.classpath
create mode 100644 bundles/org.eclipse.core.net/.cvsignore
create mode 100644 bundles/org.eclipse.core.net/.project
create mode 100644 bundles/org.eclipse.core.net/.settings/org.eclipse.jdt.core.prefs
create mode 100644 bundles/org.eclipse.core.net/META-INF/MANIFEST.MF
create mode 100644 bundles/org.eclipse.core.net/build.properties
create mode 100644 bundles/org.eclipse.core.net/plugin.properties
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyChangeEvent.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyChangeListener.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyData.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyManager.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/core/NetCore.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/NetCorePlugin.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyChangeEvent.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyData.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyManager.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyType.java
create mode 100644 bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/StringMatcher.java
(limited to 'bundles/org.eclipse.core.net')
diff --git a/bundles/org.eclipse.core.net/.classpath b/bundles/org.eclipse.core.net/.classpath
new file mode 100644
index 000000000..ce7393340
--- /dev/null
+++ b/bundles/org.eclipse.core.net/.classpath
@@ -0,0 +1,7 @@
+
+
+ * This interface is not intended to be implemented by clients. + * @see IProxyManager + * @since 1.0 + */ +public interface IProxyChangeEvent { + + /** + * Type constant that indicates that the list of non-proxied hosts has changed.\ + * @see #getChangeType() + */ + public static final int NONPROXIED_HOSTS_CHANGED = 1; + + /** + * Type constant that indicates that the data for one or more proxies has changed + * @see #getChangeType() + */ + public static final int PROXY_DATA_CHANGED = 2; + + /** + * Return the type of change this event represents. Clients + * should ignore types they do not recognize. + * @return the type of change this event represents + * @see #NONPROXIED_HOSTS_CHANGED + * @see #PROXY_DATA_CHANGED + */ + public int getChangeType(); + + /** + * For a change type of {@link #NONPROXIED_HOSTS_CHANGED}, this method will + * return the list of non-proxied hosts before the change occurred. + * @return the list of non-proxied hosts before the change occurred + */ + public String[] getOldNonProxiedHosts(); + + /** + * For a change type of {@link #NONPROXIED_HOSTS_CHANGED}, this method will + * return the list of non-proxied hosts after the change occurred. + * @return the list of non-proxied hosts after the change occurred + */ + public String[] getNonProxiedHosts(); + + /** + * For a change type of {@link #PROXY_DATA_CHANGED}, this method returns + * the state of all known proxies before the change occurred. + * @return the state of all known proxies before the change occurred + */ + public IProxyData[] getOldProxyData(); + + /** + * For a change type of {@link #PROXY_DATA_CHANGED}, this method returns + * the state of the changed known proxies after the change occurred. Clients + * should check the {@link IProxyManager#isProxiesEnabled()} method to see + * if the proxy data change was the result of proxies being disabled. + * @return the state of the changed known proxies after the change occurred + */ + public IProxyData[] getChangedProxyData(); + +} diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyChangeListener.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyChangeListener.java new file mode 100644 index 000000000..97fd6ca7f --- /dev/null +++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyChangeListener.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.net.core; + +/** + * A listener that, when registered with the {@link IProxyManager}, gets notified when the + * proxy information changes. + *
+ * This interface may be implemented by clients + * @since 1.0 + */ +public interface IProxyChangeListener { + + /** + * Callback that occurs when information related to proxies has changed. + * @param event the event that describes the change + */ + void proxyInfoChanged(IProxyChangeEvent event); +} diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyData.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyData.java new file mode 100644 index 000000000..f148d7afe --- /dev/null +++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyData.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.net.core; + +/** + * An {@link IProxyData} contains the information that is required to connect + * to a particular proxy server. + *
+ * This interface is not intended to be implemented by clients.
+ *
+ * @since 1.0
+ */
+public interface IProxyData {
+
+ /**
+ * Type constant (value "HTTP") which identifies an HTTP proxy.
+ * @see #getType()
+ */
+ public static final String HTTP_PROXY_TYPE = "HTTP"; //$NON-NLS-1$
+
+ /**
+ * Type constant (value "HTTPS") which identifies an HTTPS proxy.
+ * @see #getType()
+ */
+ public static final String HTTPS_PROXY_TYPE = "HTTPS"; //$NON-NLS-1$
+
+ /**
+ * Type constant (value "SOCKS") which identifies an SOCKS proxy.
+ * @see #getType()
+ */
+ public static final String SOCKS_PROXY_TYPE = "SOCKS"; //$NON-NLS-1$
+
+ /**
+ * Return the type of this proxy. Additional proxy types may be
+ * added in the future so clients should accommodate this.
+ * @return the type of this proxy
+ * @see #HTTP_PROXY_TYPE
+ * @see #HTTPS_PROXY_TYPE
+ * @see #SOCKS_PROXY_TYPE
+ */
+ String getType();
+
+ /**
+ * Return the host name for the proxy server or null
+ * if a proxy server of this type is not available.
+ * @return the host name for the proxy server or null
+ */
+ String getHost();
+
+ /**
+ * Set the host name for the proxy server of this type.
+ * If no proxy server of this type is available, the host name should
+ * be set to null
.
+ *
+ * Setting this value will not affect the data returned from {@link IProxyManager#getProxyData()}.
+ * Clients can change the global settings by changing the proxy data instances and then
+ * by calling {@link IProxyManager#setProxyData(IProxyData[])} with the adjusted data.
+ * @param host the host name for the proxy server or null
+ */
+ void setHost(String host);
+
+ /**
+ * Return the port that should be used when connecting to the host or -1
+ * if the default port for the proxy protocol should be used.
+ * @return the port that should be used when connecting to the host
+ */
+ int getPort();
+
+ /**
+ * Set the port that should be used when connecting to the host. Setting the port
+ * to a value of -1 will indicate that the default port number for
+ * the proxy type should be used.
+ *
+ * Setting this value will not affect the data returned from {@link IProxyManager#getProxyData()}.
+ * Clients can change the global settings by changing the proxy data instances and then
+ * by calling {@link IProxyManager#setProxyData(IProxyData[])} with the adjusted data.
+ * @param port the port that should be used when connecting to the host
+ * or -1 if the default port is to be used
+ */
+ void setPort(int port);
+
+ /**
+ * Return the id of the user that should be used when authenticating
+ * for the proxy. A null
is returned if there is no
+ * authentication information.
+ * @return the id of the user that should be used when authenticating
+ * for the proxy or null
+ */
+ String getUserId();
+
+ /**
+ * Set the id of the user that should be used when authenticating
+ * for the proxy. A null
should be used if there is no
+ * authentication information.
+ *
+ * Setting this value will not affect the data returned from {@link IProxyManager#getProxyData()}.
+ * Clients can change the global settings by changing the proxy data instances and then
+ * by calling {@link IProxyManager#setProxyData(IProxyData[])} with the adjusted data.
+ * @param userid the id of the user that should be used when authenticating
+ * for the proxy or null
+ */
+ void setUserid(String userid);
+
+ /**
+ * Return the password that should be used when authenticating
+ * for the proxy. A null
is returned if there is no
+ * password or the password is not known.
+ * @return the password that should be used when authenticating
+ * for the proxy or null
+ */
+ String getPassword();
+
+ /**
+ * Set the password that should be used when authenticating
+ * for the proxy. A null
should be passed if there is no
+ * password or the password is not known.
+ *
+ * Setting this value will not affect the data returned from {@link IProxyManager#getProxyData()}.
+ * Clients can change the global settings by changing the proxy data instances and then
+ * by calling {@link IProxyManager#setProxyData(IProxyData[])} with the adjusted data.
+ * @param password the password that should be used when authenticating
+ * for the proxy or null
+ */
+ void setPassword(String password);
+
+ /**
+ * Returns whether the proxy requires authentication. If the proxy
+ * requires authentication but the user name and password fields of
+ * this proxy data are null, the client can expect the connection
+ * to fail unless they somehow obtain the authentication information.
+ * @return whether the proxy requires authentication
+ */
+ boolean isRequiresAuthentication();
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyManager.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyManager.java
new file mode 100644
index 000000000..b78fa074f
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/IProxyManager.java
@@ -0,0 +1,128 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.core;
+
+/**
+ * Manages the proxy data and related information.
+ *
+ * This interface is not intended to be implemented by clients.
+ * @since 1.0
+ */
+public interface IProxyManager {
+
+ /**
+ * Set whether proxy support should be enabled. When disabled, the data for all the
+ * known proxy types will contain a null
in the host field
+ * (see {@link IProxyData#getHost()}). When enabled, the proxy data will not have any
+ * values until the next time it is set (see {@link #setProxyData(IProxyData[])}).
+ * @param enabled whether proxy support should be enabled
+ */
+ void setProxiesEnabled(boolean enabled);
+
+ /**
+ * Return whether proxy support should be enabled. When disabled, all connections
+ * will be direct.
+ * @return whether proxy support should be enabled
+ */
+ boolean isProxiesEnabled();
+
+ /**
+ * Return the list of know proxy types and their settings.
+ * Some of the returned proxy types may not be enabled (i.e,
+ * their hosts may be null
.
+ * @return the list of know proxy types and their settings
+ */
+ IProxyData[] getProxyData();
+
+ /**
+ * Return the list of known proxy types and their settings for the
+ * given host. If proxies are disabled
+ * or if the host matches any entries in the non-proxied
+ * hosts lists or if a matching proxy type has no data, then
+ * an empty array is returned.
+ * @param host the host for which a connection is desired
+ * @return the list of known proxy types and their settings for the
+ * given host
+ */
+ IProxyData[] getProxyDataForHost(String host);
+
+ /**
+ * Return the proxy data for the proxy of the given type
+ * or null
if the proxy type is not known by this
+ * manager.
+ * @param type the proxy type
+ * @return the proxy data for the proxy of the given type
+ * or null
+ * @see IProxyData#HTTP_PROXY_TYPE
+ * @see IProxyData#HTTPS_PROXY_TYPE
+ * @see IProxyData#SOCKS_PROXY_TYPE
+ */
+ IProxyData getProxyData(String type);
+
+ /**
+ * Return the proxy data for the proxy of the given type
+ * or null
if the proxy type is not known by this
+ * manager, the proxy data is empty for that type or the
+ * host is in the non-proxied host list.
+ * @param host the host for which a connection is desired
+ * @param type the proxy type
+ * @return the proxy data for the proxy of the given type
+ * or null
+ * @see IProxyData#HTTP_PROXY_TYPE
+ * @see IProxyData#HTTPS_PROXY_TYPE
+ * @see IProxyData#SOCKS_PROXY_TYPE
+ */
+ IProxyData getProxyDataForHost(String host, String type);
+
+ /**
+ * Set the information associated with known proxy types.
+ * If an unknown type is provided, it will be ignored. Any
+ * known types that are not present in the list of the provided
+ * proxy data will be unaffected. Calls to this method when proxies
+ * are disabled will be ignored.
+ * @param proxies the proxy data whose information is to be set.
+ */
+ void setProxyData(IProxyData[] proxies);
+
+ /**
+ * Return the list of hosts for which non proxy should be used.
+ * The values returned from this method should only be used for displaying
+ * the non-proxed hosts list. Clients that which to make a connection and need
+ * to determine whether to use a proxy or not shoudl call either {@link #getProxyDataForHost(String)}
+ * or {@link #getProxyDataForHost(String, String)}.
+ * @return the list of hosts for which non proxy should be used.
+ * @see #getProxyDataForHost(String)
+ * @see #getProxyDataForHost(String, String)
+ */
+ String[] getNonProxiedHosts();
+
+ /**
+ * Set the list of hosts for which non proxy should be used.
+ * @param hosts the list of hosts for which non proxy should be used.
+ */
+ void setNonProxiedHosts(String[] hosts);
+
+ /**
+ * Register a listener that will be notified when proxy related
+ * information changes. Adding a listener that is already registered
+ * has no effect.
+ * @param listener a change listener
+ */
+ void addProxyChangeListener(IProxyChangeListener listener);
+
+ /**
+ * Remove a listener. Removing a listener that is not registered
+ * has no effect.
+ * @param listener a change listener
+ */
+ void removeProxyChangeListener(IProxyChangeListener listener);
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/core/NetCore.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/NetCore.java
new file mode 100644
index 000000000..abc69ea64
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/core/NetCore.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.core;
+
+import org.eclipse.net.internal.core.ProxyManager;
+
+/**
+ * Provides access to the {@link IProxyManager} and other net related functionality.
+ * *
+ * This class is not intended to be subclasses or instantiated by clients.
+ * @since 1.0
+ */
+public final class NetCore {
+
+ private static IProxyManager proxyManager;
+
+ private NetCore() {
+ super();
+ }
+
+ /**
+ * Return the proxy manager.
+ * @return the proxy manager
+ */
+ public synchronized static IProxyManager getProxyManager() {
+ if (proxyManager == null)
+ proxyManager = new ProxyManager();
+ return proxyManager;
+ }
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/NetCorePlugin.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/NetCorePlugin.java
new file mode 100644
index 000000000..558636981
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/NetCorePlugin.java
@@ -0,0 +1,70 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ * yyyymmdd bug Email and other contact information
+ * -------- -------- -----------------------------------------------------------
+ * 20070119 161112 makandre@ca.ibm.com - Andrew Mak, WSE: can't find business thru a proxy server that needs basic auth
+ * 20070201 154100 pmoogk@ca.ibm.com - Peter Moogk, Port internet code from WTP to Eclipse base.
+ *******************************************************************************/
+
+package org.eclipse.net.internal.core;
+
+import org.eclipse.core.runtime.*;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.net.core.NetCore;
+import org.osgi.framework.BundleContext;
+
+public class NetCorePlugin extends Plugin {
+ /**
+ * The identifier of the descriptor of this plugin in plugin.xml.
+ */
+ public static final String ID = "org.eclipse.net.core"; //$NON-NLS-1$
+
+ /**
+ * The instance of this plugin.
+ */
+ private static NetCorePlugin instance;
+
+ /**
+ * Constructor for use by the Eclipse platform only.
+ */
+ public NetCorePlugin() {
+ super();
+ instance = this;
+ }
+
+ /**
+ * Returns the instance of this plugin.
+ * @return the singleton instance of this plug-in class
+ */
+ static public NetCorePlugin getInstance() {
+ return instance;
+ }
+
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ ((ProxyManager)NetCore.getProxyManager()).initialize();
+ }
+
+ public static void logError(String message, Throwable exc) {
+ IStatus status = new Status(IStatus.ERROR, ID, 0, message, exc);
+
+ getInstance().getLog().log(status);
+ }
+
+ public static void logInfo(String message, Throwable exc) {
+ IStatus status = new Status(IStatus.INFO, ID, 0, message, exc);
+
+ getInstance().getLog().log(status);
+ }
+
+ public org.osgi.service.prefs.Preferences getInstancePreferences() {
+ return new InstanceScope().getNode(getBundle().getSymbolicName());
+ }
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyChangeEvent.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyChangeEvent.java
new file mode 100644
index 000000000..8e3106230
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyChangeEvent.java
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.internal.core;
+
+import org.eclipse.net.core.IProxyChangeEvent;
+import org.eclipse.net.core.IProxyData;
+
+public class ProxyChangeEvent implements IProxyChangeEvent {
+
+ private final int type;
+ private final String[] oldHosts;
+ private final String[] nonProxiedHosts;
+ private final IProxyData[] oldData;
+ private final IProxyData[] changeData;
+
+ public ProxyChangeEvent(int type, String[] oldHosts,
+ String[] nonProxiedHosts, IProxyData[] oldData, IProxyData[] changedData) {
+ this.type = type;
+ this.oldHosts = oldHosts;
+ this.nonProxiedHosts = nonProxiedHosts;
+ this.oldData = oldData;
+ this.changeData = changedData;
+ }
+
+ public int getChangeType() {
+ return type;
+ }
+
+ public IProxyData[] getChangedProxyData() {
+ return changeData;
+ }
+
+ public String[] getNonProxiedHosts() {
+ return nonProxiedHosts;
+ }
+
+ public String[] getOldNonProxiedHosts() {
+ return oldHosts;
+ }
+
+ public IProxyData[] getOldProxyData() {
+ return oldData;
+ }
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyData.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyData.java
new file mode 100644
index 000000000..10b906d89
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyData.java
@@ -0,0 +1,76 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.internal.core;
+
+import org.eclipse.net.core.IProxyData;
+
+public class ProxyData implements IProxyData {
+
+ private String type;
+ private String host;
+ private int port;
+ private String user;
+ private String password;
+ private boolean requiresAuthentication;
+
+ public ProxyData(String type, String host, int port, boolean requiresAuthentication) {
+ this.type = type;
+ this.host = host;
+ this.port = port;
+ this.requiresAuthentication = requiresAuthentication;
+ }
+
+ public ProxyData(String type) {
+ this.type = type;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getUserId() {
+ return user;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public void setUserid(String userid) {
+ this.user = userid;
+ requiresAuthentication = userid != null;
+ }
+
+ public boolean isRequiresAuthentication() {
+ return requiresAuthentication;
+ }
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyManager.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyManager.java
new file mode 100644
index 000000000..c1821f039
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyManager.java
@@ -0,0 +1,242 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.internal.core;
+
+import java.util.*;
+
+import org.eclipse.core.runtime.*;
+import org.eclipse.net.core.*;
+import org.osgi.service.prefs.BackingStoreException;
+
+public class ProxyManager implements IProxyManager {
+
+ private static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$
+ private static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$
+
+ ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
+ private String[] nonProxiedHosts;
+ private final ProxyType[] proxies = new ProxyType[] {
+ new ProxyType(IProxyData.HTTP_PROXY_TYPE),
+ new ProxyType(IProxyData.HTTPS_PROXY_TYPE),
+ new ProxyType(IProxyData.SOCKS_PROXY_TYPE)
+ };
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#addProxyChangeListener(org.eclipse.core.net.IProxyChangeListener)
+ */
+ public void addProxyChangeListener(IProxyChangeListener listener) {
+ listeners.add(listener);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#removeProxyChangeListener(org.eclipse.core.net.IProxyChangeListener)
+ */
+ public void removeProxyChangeListener(IProxyChangeListener listener) {
+ listeners.remove(listener);
+ }
+
+ private void fireChange(final IProxyChangeEvent event) {
+ Object[] l = listeners.getListeners();
+ for (int i = 0; i < l.length; i++) {
+ final IProxyChangeListener listener = (IProxyChangeListener)l[i];
+ SafeRunner.run(new ISafeRunnable() {
+ public void run() throws Exception {
+ listener.proxyInfoChanged(event);
+ }
+ public void handleException(Throwable exception) {
+ // Logged by SafeRunner
+ }
+ });
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#getNonProxiedHosts()
+ */
+ public synchronized String[] getNonProxiedHosts() {
+ if (nonProxiedHosts == null) {
+ String prop = NetCorePlugin.getInstance().getInstancePreferences().get(PREF_NON_PROXIED_HOSTS, "localhost|127.0.0.1"); //$NON-NLS-1$
+ nonProxiedHosts = ProxyType.convertPropertyStringToHosts(prop);
+ }
+ if (nonProxiedHosts.length == 0)
+ return nonProxiedHosts;
+ String[] result = new String[nonProxiedHosts.length];
+ System.arraycopy(nonProxiedHosts, 0, result, 0, nonProxiedHosts.length );
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#setNonProxiedHosts(java.lang.String[])
+ */
+ public void setNonProxiedHosts(String[] hosts) {
+ Assert.isNotNull(hosts);
+ for (int i = 0; i < hosts.length; i++) {
+ String host = hosts[i];
+ Assert.isNotNull(host);
+ Assert.isTrue(host.length() > 0);
+ }
+ String[] oldHosts = nonProxiedHosts;
+ nonProxiedHosts = hosts;
+ NetCorePlugin.getInstance().getInstancePreferences().put(PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts));
+ try {
+ NetCorePlugin.getInstance().getInstancePreferences().flush();
+ } catch (BackingStoreException e) {
+ NetCorePlugin.logError(
+ "An error occurred while writing out the non-proxied hosts list", e); //$NON-NLS-1$
+ }
+ IProxyData[] data = getProxyData();
+ IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.NONPROXIED_HOSTS_CHANGED, oldHosts, getNonProxiedHosts(), data, new IProxyData[0]);
+ fireChange(event);
+ }
+
+
+ public IProxyData[] getProxyData() {
+ IProxyData[] result = new IProxyData[proxies.length];
+ for (int i = 0; i < proxies.length; i++) {
+ ProxyType type = proxies[i];
+ result[i] = type.getProxyData();
+ }
+ return result;
+ }
+
+ public void setProxyData(IProxyData[] proxies) {
+ if (isProxiesEnabled())
+ doSetProxyData(proxies);
+ }
+
+ private void doSetProxyData(IProxyData[] proxyDatas) {
+ IProxyData[] oldData = getProxyData();
+ String[] hosts = getNonProxiedHosts();
+ IProxyData[] changedProxies = internalSetProxyData(proxyDatas);
+ if (changedProxies.length > 0) {
+ IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.PROXY_DATA_CHANGED, hosts, hosts, oldData, changedProxies);
+ fireChange(event);
+ }
+ }
+
+ private IProxyData[] internalSetProxyData(IProxyData[] proxyDatas) {
+ List result = new ArrayList();
+ for (int i = 0; i < proxyDatas.length; i++) {
+ IProxyData proxyData = proxyDatas[i];
+ ProxyType type = getType(proxyData);
+ if (type != null && type.setProxyData(proxyData)) {
+ result.add(proxyData);
+ }
+ }
+ return (IProxyData[]) result.toArray(new IProxyData[result.size()]);
+ }
+
+ private ProxyType getType(IProxyData proxyData) {
+ for (int i = 0; i < proxies.length; i++) {
+ ProxyType type = proxies[i];
+ if (type.getName().equals(proxyData.getType())) {
+ return type;
+ }
+ }
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#isProxiesEnabled()
+ */
+ public boolean isProxiesEnabled() {
+ return NetCorePlugin.getInstance().getInstancePreferences().getBoolean(PREF_ENABLED, false);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.net.IProxyManager#setProxiesEnabled(boolean)
+ */
+ public void setProxiesEnabled(boolean enabled) {
+ boolean current = isProxiesEnabled();
+ if (current == enabled)
+ return;
+ NetCorePlugin.getInstance().getInstancePreferences().putBoolean(PREF_ENABLED, enabled);
+ Properties sysProps = System.getProperties();
+ sysProps.put("proxySet", enabled ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ if (!enabled) {
+ // This will fire a change event
+ doSetProxyData(new IProxyData[] {
+ new ProxyData(IProxyData.HTTP_PROXY_TYPE),
+ new ProxyData(IProxyData.HTTPS_PROXY_TYPE),
+ new ProxyData(IProxyData.SOCKS_PROXY_TYPE)
+ });
+ }
+ try {
+ NetCorePlugin.getInstance().getInstancePreferences().flush();
+ } catch (BackingStoreException e) {
+ NetCorePlugin.logError(
+ "An error occurred while writing out the enablement state", e); //$NON-NLS-1$
+ }
+ }
+
+ public void initialize() {
+ // Only initialize the system settings if proxies are enabled
+ if (isProxiesEnabled()) {
+ for (int i = 0; i < proxies.length; i++) {
+ ProxyType type = proxies[i];
+ type.initialize();
+ }
+ }
+ }
+
+ public IProxyData getProxyData(String type) {
+ IProxyData[] data = getProxyData();
+ for (int i = 0; i < data.length; i++) {
+ IProxyData proxyData = data[i];
+ if (proxyData.getType().equals(type)) {
+ return proxyData;
+ }
+ }
+ return null;
+ }
+
+ public IProxyData[] getProxyDataForHost(String host) {
+ if (isHostFiltered(host))
+ return new IProxyData[0];
+ IProxyData[] data = getProxyData();
+ List result = new ArrayList();
+ for (int i = 0; i < data.length; i++) {
+ IProxyData proxyData = data[i];
+ if (proxyData.getHost() != null)
+ result.add(proxyData);
+ }
+ return (IProxyData[]) result.toArray(new IProxyData[result.size()]);
+ }
+
+ private boolean isHostFiltered(String host) {
+ String[] filters = getNonProxiedHosts();
+ for (int i = 0; i < filters.length; i++) {
+ String filter = filters[i];
+ if (matchesFilter(host, filter))
+ return true;
+ }
+ return false;
+ }
+
+ private boolean matchesFilter(String host, String filter) {
+ StringMatcher matcher = new StringMatcher(filter, false, false);
+ return matcher.match(host);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.net.core.IProxyManager#getProxyDataForHost(java.lang.String, java.lang.String)
+ */
+ public IProxyData getProxyDataForHost(String host, String type) {
+ IProxyData[] data = getProxyDataForHost(host);
+ for (int i = 0; i < data.length; i++) {
+ IProxyData proxyData = data[i];
+ if (proxyData.getType().equals(type) && proxyData.getHost() != null)
+ return proxyData;
+ }
+ return null;
+ }
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyType.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyType.java
new file mode 100644
index 000000000..370519559
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/ProxyType.java
@@ -0,0 +1,291 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.internal.core;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.*;
+
+import org.eclipse.core.runtime.*;
+import org.eclipse.net.core.IProxyData;
+import org.eclipse.net.core.NetCore;
+import org.eclipse.osgi.util.NLS;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
+
+public class ProxyType {
+
+ /**
+ * Preference keys
+ */
+ private static final String PREF_PROXY_DATA_NODE = "proxyData"; //$NON-NLS-1$
+ private static final String PREF_PROXY_HOST = "host"; //$NON-NLS-1$
+ private static final String PREF_PROXY_PORT = "port"; //$NON-NLS-1$
+ private static final String PREF_PROXY_HAS_AUTH = "hasAuth"; //$NON-NLS-1$
+
+ /*
+ * Fields used to cache authentication information in the keyring
+ */
+ private static final String INFO_PROXY_USER = "user"; //$NON-NLS-1$
+ private static final String INFO_PROXY_PASS = "pass"; //$NON-NLS-1$
+ private static final URL FAKE_URL;
+ static {
+ URL temp = null;
+ try {
+ temp = new URL("http://org.eclipse.core.net.proxy.auth");//$NON-NLS-1$
+ } catch (MalformedURLException e) {
+ // Should never fail
+ }
+ FAKE_URL = temp;
+ }
+
+ private String name;
+
+ public static String convertHostsToPropertyString(String[] value) {
+ StringBuffer buffer = new StringBuffer();
+
+ if (value == null)
+ return ""; //$NON-NLS-1$
+
+ if (value.length > 0) {
+ buffer.append(value[0]);
+ }
+
+ for (int index = 1; index < value.length; index++) {
+ buffer.append('|');
+ buffer.append(value[index]);
+ }
+
+ return buffer.toString();
+ }
+
+ public static String[] convertPropertyStringToHosts(String property) {
+ return property.split("\\|"); //$NON-NLS-1$
+ }
+
+ public ProxyType(String name) {
+ super();
+ this.name = name;
+ }
+
+ private Preferences getPreferenceNode() {
+ return getParentPreferences().node(getName());
+ }
+
+ /**
+ * Return the preferences node whose child nodes are the know proxy types
+ *
+ * @return a preferences node
+ */
+ private Preferences getParentPreferences() {
+ return NetCorePlugin.getInstance().getInstancePreferences().node(
+ PREF_PROXY_DATA_NODE);
+ }
+
+ public IProxyData getProxyData() {
+ return createProxyData(name, getPreferenceNode());
+ }
+
+ private IProxyData createProxyData(String type, Preferences node) {
+ String host = node.get(PREF_PROXY_HOST, null);
+ int port = node.getInt(PREF_PROXY_PORT, -1);
+ boolean requiresAuth = node.getBoolean(PREF_PROXY_HAS_AUTH, false);
+ ProxyData proxyData = new ProxyData(type, host, port, requiresAuth);
+ loadProxyAuth(proxyData);
+ return proxyData;
+ }
+
+ public boolean setProxyData(IProxyData proxyData) {
+ Assert.isTrue(proxyData.getType().equals(getName()));
+ IProxyData oldData = getProxyData();
+ if (oldData.equals(proxyData))
+ return false;
+ Preferences node = getPreferenceNode();
+ if (proxyData.getHost() == null) {
+ try {
+ Preferences parent = node.parent();
+ node.removeNode();
+ parent.flush();
+ } catch (BackingStoreException e) {
+ NetCorePlugin.logError(NLS.bind(
+ "An error occurred removing the {0} proxy node from the preference store", proxyData.getType()), e); //$NON-NLS-1$
+ }
+ } else {
+ node.put(PREF_PROXY_HOST, proxyData.getHost());
+ node.putInt(PREF_PROXY_PORT, proxyData.getPort());
+ node.putBoolean(PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null);
+ saveProxyAuth(proxyData);
+ try {
+ node.flush();
+ } catch (BackingStoreException e) {
+ NetCorePlugin.logError(NLS.bind(
+ "The {0} proxy node could not be written", proxyData.getType()), e); //$NON-NLS-1$
+ }
+ }
+ updateSystemProperties(proxyData);
+ return true;
+ }
+
+ private void updateSystemProperties(IProxyData proxyData) {
+ try {
+ if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
+ updateHttpSystemProperties(proxyData);
+ } else if (proxyData.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) {
+ updateHttpsSystemProperties(proxyData);
+ } else if (proxyData.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) {
+ updateSocksSystemProperties(proxyData);
+ }
+ } catch (SecurityException e) {
+ NetCorePlugin.logError("A security exception occurred while trying to put the proxy data into the system properties", e); //$NON-NLS-1$
+ }
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ private void updateHttpSystemProperties(IProxyData data) {
+ Assert.isTrue(data.getType().equals(IProxyData.HTTP_PROXY_TYPE));
+ Properties sysProps = System.getProperties();
+ if (data.getHost() == null) {
+ sysProps.remove("http.proxySet"); //$NON-NLS-1$
+ sysProps.remove("http.proxyHost"); //$NON-NLS-1$
+ sysProps.remove("http.proxyPort"); //$NON-NLS-1$
+ sysProps.remove("http.nonProxyHosts"); //$NON-NLS-1$
+ sysProps.remove("http.proxyUser"); //$NON-NLS-1$
+ sysProps.remove("http.proxyUserName"); //$NON-NLS-1$
+ sysProps.remove("http.proxyPassword"); //$NON-NLS-1$
+ } else {
+ sysProps.put("http.proxySet", "true"); //$NON-NLS-1$ //$NON-NLS-2$
+ sysProps.put("http.proxyHost", data.getHost()); //$NON-NLS-1$
+ int port = data.getPort();
+ if (port == -1) {
+ sysProps.remove("http.proxyPort"); //$NON-NLS-1$
+ } else {
+ sysProps.put("http.proxyPort", String.valueOf(port)); //$NON-NLS-1$
+ }
+ sysProps.put("http.nonProxyHosts", //$NON-NLS-1$
+ convertHostsToPropertyString(NetCore.getProxyManager().getNonProxiedHosts()));
+
+ String userid = data.getUserId();
+ String password = data.getPassword();
+ if (userid == null || password == null || userid.length() == 0
+ || password.length() == 0) {
+ sysProps.remove("http.proxyUser"); //$NON-NLS-1$
+ sysProps.remove("http.proxyUserName"); //$NON-NLS-1$
+ sysProps.remove("http.proxyPassword"); //$NON-NLS-1$
+ } else {
+ sysProps.put("http.proxyUser", userid); //$NON-NLS-1$
+ sysProps.put("http.proxyUserName", userid); //$NON-NLS-1$
+ sysProps.put("http.proxyPassword", password); //$NON-NLS-1$
+ }
+ }
+ }
+
+ private void updateHttpsSystemProperties(IProxyData data) {
+ Assert.isTrue(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE));
+ Properties sysProps = System.getProperties();
+ if (data.getHost() == null) {
+ sysProps.remove("https.proxySet"); //$NON-NLS-1$
+ sysProps.remove("https.proxyHost"); //$NON-NLS-1$
+ sysProps.remove("https.proxyPort"); //$NON-NLS-1$
+ sysProps.remove("https.nonProxyHosts"); //$NON-NLS-1$
+ sysProps.remove("https.proxyUser"); //$NON-NLS-1$
+ sysProps.remove("https.proxyUserName"); //$NON-NLS-1$
+ sysProps.remove("https.proxyPassword"); //$NON-NLS-1$
+ } else {
+ sysProps.put("https.proxySet", "true"); //$NON-NLS-1$ //$NON-NLS-2$
+ sysProps.put("https.proxyHost", data.getHost()); //$NON-NLS-1$
+ int port = data.getPort();
+ if (port == -1) {
+ sysProps.remove("https.proxyPort"); //$NON-NLS-1$
+ } else {
+ sysProps.put("https.proxyPort", String.valueOf(port)); //$NON-NLS-1$
+ }
+ sysProps.put("https.nonProxyHosts", //$NON-NLS-1$
+ convertHostsToPropertyString(NetCore.getProxyManager().getNonProxiedHosts()));
+
+ String userid = data.getUserId();
+ String password = data.getPassword();
+ if (userid == null || password == null || userid.length() == 0
+ || password.length() == 0) {
+ sysProps.remove("https.proxyUser"); //$NON-NLS-1$
+ sysProps.remove("https.proxyUserName"); //$NON-NLS-1$
+ sysProps.remove("https.proxyPassword"); //$NON-NLS-1$
+ } else {
+ sysProps.put("https.proxyUser", userid); //$NON-NLS-1$
+ sysProps.put("https.proxyUserName", userid); //$NON-NLS-1$
+ sysProps.put("https.proxyPassword", password); //$NON-NLS-1$
+ }
+ }
+ }
+
+ private void updateSocksSystemProperties(IProxyData data) {
+ Assert.isTrue(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE));
+ Properties sysProps = System.getProperties();
+ if (data.getHost() == null) {
+ sysProps.remove("socksProxyHost"); //$NON-NLS-1$
+ sysProps.remove("socksProxyPort"); //$NON-NLS-1$
+ } else {
+ sysProps.put("socksProxyHost", data.getHost()); //$NON-NLS-1$
+ int port = data.getPort();
+ if (port == -1) {
+ sysProps.remove("socksProxyPort"); //$NON-NLS-1$
+ } else {
+ sysProps.put("socksProxyPort", String.valueOf(port)); //$NON-NLS-1$
+ }
+ // TODO: There does appear to be a way to set the non-proxy hosts for Socks
+ // TODO: See http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html for a description
+ // of how to set the Socks user and password
+ }
+ }
+
+ public void initialize() {
+ updateSystemProperties(getProxyData());
+ }
+
+ private Map getAuthInfo() {
+ // Retrieve username and password from keyring.
+ Map authInfo = Platform.getAuthorizationInfo(FAKE_URL, getName(), ""); //$NON-NLS-1$
+ return authInfo != null ? authInfo : Collections.EMPTY_MAP;
+ }
+
+ private void loadProxyAuth(IProxyData data) {
+ Map authInfo = getAuthInfo();
+ data.setUserid((String)authInfo.get(INFO_PROXY_USER));
+ data.setPassword((String)authInfo.get(INFO_PROXY_PASS));
+ }
+
+ private void saveProxyAuth(IProxyData data) {
+ Map authInfo = getAuthInfo();
+ if (authInfo.size() == 0) {
+ authInfo = new java.util.HashMap(4);
+ }
+ String proxyUser = data.getUserId();
+ if (proxyUser != null) {
+ authInfo.put(INFO_PROXY_USER, proxyUser);
+ }
+ String proxyPass = data.getPassword();
+ if (proxyPass != null) {
+ authInfo.put(INFO_PROXY_PASS, proxyPass);
+ }
+ try {
+ if (authInfo.isEmpty()) {
+ Platform.flushAuthorizationInfo(FAKE_URL, getName(), ""); //$NON-NLS-1$
+ } else {
+ Platform.addAuthorizationInfo(FAKE_URL, getName(), "", authInfo); //$NON-NLS-1$
+ }
+ } catch (CoreException e) {
+ NetCorePlugin.logError(e.getMessage(), e);
+ }
+ }
+
+}
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/StringMatcher.java b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/StringMatcher.java
new file mode 100644
index 000000000..ce73c450c
--- /dev/null
+++ b/bundles/org.eclipse.core.net/src/org/eclipse/net/internal/core/StringMatcher.java
@@ -0,0 +1,395 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.net.internal.core;
+
+import java.util.Vector;
+
+/**
+ * A string pattern matcher, suppporting * and ? wildcards.
+ * Note: code copied from org.eclipse.jdt.internal.core.util.StringMatcher on April 3, 2001
+ * (version 0.1 - 010901H18 [rename jbl]).
+ */
+public class StringMatcher {
+ protected String fPattern;
+ protected int fLength; // pattern length
+ protected boolean fIgnoreWildCards;
+ protected boolean fIgnoreCase;
+ protected boolean fHasLeadingStar;
+ protected boolean fHasTrailingStar;
+ protected String fSegments[]; //the given pattern is split into * separated segments
+
+ /* boundary value beyond which we don't need to search in the text */
+ protected int fBound = 0;
+
+
+ protected static final char fSingleWildCard = '\u0000';
+
+ public static class Position {
+ int start; //inclusive
+ int end; //exclusive
+ public Position(int start, int end) {
+ this.start = start;
+ this.end = end;
+ }
+ public int getStart() {
+ return start;
+ }
+ public int getEnd() {
+ return end;
+ }
+ }
+ /**
+ * Find the first occurrence of the pattern between start
end(exclusive).
+ * @param text the String object to search in
+ * @param start the starting index of the search range, inclusive
+ * @param end the ending index of the search range, exclusive
+ * @return an StringMatcher.Position
object that keeps the starting
+ * (inclusive) and ending positions (exclusive) of the first occurrence of the
+ * pattern in the specified range of the text; return null if not found or subtext
+ * is empty (start==end). A pair of zeros is returned if pattern is empty string
+ * Note that for pattern like "*abc*" with leading and trailing stars, position of "abc"
+ * is returned. For a pattern like"*??*" in text "abcdf", (1,3) is returned
+ */
+
+ public StringMatcher.Position find(String text, int start, int end) {
+ if (fPattern == null|| text == null)
+ throw new IllegalArgumentException();
+
+ int tlen = text.length();
+ if (start < 0)
+ start = 0;
+ if (end > tlen)
+ end = tlen;
+ if (end < 0 ||start >= end )
+ return null;
+ if (fLength == 0)
+ return new Position(start, start);
+ if (fIgnoreWildCards) {
+ int x = posIn(text, start, end);
+ if (x < 0)
+ return null;
+ return new Position(x, x+fLength);
+ }
+
+ int segCount = fSegments.length;
+ if (segCount == 0)//pattern contains only '*'(s)
+ return new Position (start, end);
+
+ int curPos = start;
+ int matchStart = -1;
+ int i;
+ for (i = 0; i < segCount && curPos < end; ++i) {
+ String current = fSegments[i];
+ int nextMatch = regExpPosIn(text, curPos, end, current);
+ if (nextMatch < 0 )
+ return null;
+ if(i == 0)
+ matchStart = nextMatch;
+ curPos = nextMatch + current.length();
+ }
+ if (i < segCount)
+ return null;
+ return new Position(matchStart, curPos);
+ }
+ /**
+ * StringMatcher constructor takes in a String object that is a simple
+ * pattern which may contain '*' for 0 and many characters and
+ * '?' for exactly one character.
+ *
+ * Literal '*' and '?' characters must be escaped in the pattern
+ * e.g., "\*" means literal "*", etc.
+ *
+ * Escaping any other character (including the escape character itself),
+ * just results in that character in the pattern.
+ * e.g., "\a" means "a" and "\\" means "\"
+ *
+ * If invoking the StringMatcher with string literals in Java, don't forget
+ * escape characters are represented by "\\".
+ *
+ * @param aPattern the pattern to match text with
+ * @param ignoreCase if true, case is ignored
+ * @param ignoreWildCards if true, wild cards and their escape sequences are ignored
+ * (everything is taken literally).
+ */
+ public StringMatcher(String aPattern, boolean ignoreCase, boolean ignoreWildCards) {
+ fIgnoreCase = ignoreCase;
+ fIgnoreWildCards = ignoreWildCards;
+ fLength = aPattern.length();
+
+ /* convert case */
+ if (fIgnoreCase) {
+ fPattern = aPattern.toUpperCase();
+ } else {
+ fPattern = aPattern;
+ }
+
+ if (fIgnoreWildCards) {
+ parseNoWildCards();
+ } else {
+ parseWildCards();
+ }
+ }
+ /**
+ * Given the starting (inclusive) and the ending (exclusive) poisitions in the
+ * text
, determine if the given substring matches with aPattern
+ * @return true if the specified portion of the text matches the pattern
+ * @param text a String object that contains the substring to match
+ * @param start marks the starting position (inclusive) of the substring
+ * @param end marks the ending index (exclusive) of the substring
+ */
+ public boolean match(String text, int start, int end) {
+ if (null == text)
+ throw new IllegalArgumentException();
+
+ if (start > end)
+ return false;
+
+ if (fIgnoreWildCards)
+ return (end - start == fLength) && fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength);
+ int segCount= fSegments.length;
+ if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) // pattern contains only '*'(s)
+ return true;
+ if (start == end)
+ return fLength == 0;
+ if (fLength == 0)
+ return start == end;
+
+ int tlen= text.length();
+ if (start < 0)
+ start= 0;
+ if (end > tlen)
+ end= tlen;
+
+ int tCurPos= start;
+ int bound= end - fBound;
+ if ( bound < 0)
+ return false;
+ int i=0;
+ String current= fSegments[i];
+ int segLength= current.length();
+
+ /* process first segment */
+ if (!fHasLeadingStar){
+ if(!regExpRegionMatches(text, start, current, 0, segLength)) {
+ return false;
+ }
+ ++i;
+ tCurPos= tCurPos + segLength;
+ }
+ if ((fSegments.length == 1) && (!fHasLeadingStar) && (!fHasTrailingStar)) {
+ // only one segment to match, no wildcards specified
+ return tCurPos == end;
+ }
+ /* process middle segments */
+ while (i < segCount) {
+ current= fSegments[i];
+ int currentMatch;
+ int k= current.indexOf(fSingleWildCard);
+ if (k < 0) {
+ currentMatch= textPosIn(text, tCurPos, end, current);
+ if (currentMatch < 0)
+ return false;
+ } else {
+ currentMatch= regExpPosIn(text, tCurPos, end, current);
+ if (currentMatch < 0)
+ return false;
+ }
+ tCurPos= currentMatch + current.length();
+ i++;
+ }
+
+ /* process final segment */
+ if (!fHasTrailingStar && tCurPos != end) {
+ int clen= current.length();
+ return regExpRegionMatches(text, end - clen, current, 0, clen);
+ }
+ return i == segCount ;
+ }
+ /**
+ * match the given text
with the pattern
+ * @return true if matched eitherwise false
+ * @param text a String object
+ */
+ public boolean match(String text) {
+ return match(text, 0, text.length());
+ }
+ /**
+ * This method parses the given pattern into segments seperated by wildcard '*' characters.
+ * Since wildcards are not being used in this case, the pattern consists of a single segment.
+ */
+ private void parseNoWildCards() {
+ fSegments = new String[1];
+ fSegments[0] = fPattern;
+ fBound = fLength;
+ }
+ /**
+ * This method parses the given pattern into segments seperated by wildcard '*' characters.
+ * @param p a String object that is a simple regular expression with '*' and/or '?'
+ */
+ private void parseWildCards() {
+ if(fPattern.startsWith("*"))//$NON-NLS-1$
+ fHasLeadingStar = true;
+ if(fPattern.endsWith("*")) {//$NON-NLS-1$
+ /* make sure it's not an escaped wildcard */
+ if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
+ fHasTrailingStar = true;
+ }
+ }
+
+ Vector temp = new Vector();
+
+ int pos = 0;
+ StringBuffer buf = new StringBuffer();
+ while (pos < fLength) {
+ char c = fPattern.charAt(pos++);
+ switch (c) {
+ case '\\':
+ if (pos >= fLength) {
+ buf.append(c);
+ } else {
+ char next = fPattern.charAt(pos++);
+ /* if it's an escape sequence */
+ if (next == '*' || next == '?' || next == '\\') {
+ buf.append(next);
+ } else {
+ /* not an escape sequence, just insert literally */
+ buf.append(c);
+ buf.append(next);
+ }
+ }
+ break;
+ case '*':
+ if (buf.length() > 0) {
+ /* new segment */
+ temp.addElement(buf.toString());
+ fBound += buf.length();
+ buf.setLength(0);
+ }
+ break;
+ case '?':
+ /* append special character representing single match wildcard */
+ buf.append(fSingleWildCard);
+ break;
+ default:
+ buf.append(c);
+ }
+ }
+
+ /* add last buffer to segment list */
+ if (buf.length() > 0) {
+ temp.addElement(buf.toString());
+ fBound += buf.length();
+ }
+
+ fSegments = new String[temp.size()];
+ temp.copyInto(fSegments);
+ }
+ /**
+ * @param text a string which contains no wildcard
+ * @param start the starting index in the text for search, inclusive
+ * @param end the stopping point of search, exclusive
+ * @return the starting index in the text of the pattern , or -1 if not found
+ */
+ protected int posIn(String text, int start, int end) {//no wild card in pattern
+ int max = end - fLength;
+
+ if (!fIgnoreCase) {
+ int i = text.indexOf(fPattern, start);
+ if (i == -1 || i > max)
+ return -1;
+ return i;
+ }
+
+ for (int i = start; i <= max; ++i) {
+ if (text.regionMatches(true, i, fPattern, 0, fLength))
+ return i;
+ }
+
+ return -1;
+ }
+ /**
+ * @param text a simple regular expression that may only contain '?'(s)
+ * @param start the starting index in the text for search, inclusive
+ * @param end the stopping point of search, exclusive
+ * @param p a simple regular expression that may contains '?'
+ * @return the starting index in the text of the pattern , or -1 if not found
+ */
+ protected int regExpPosIn(String text, int start, int end, String p) {
+ int plen = p.length();
+
+ int max = end - plen;
+ for (int i = start; i <= max; ++i) {
+ if (regExpRegionMatches(text, i, p, 0, plen))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ *
+ * @param text the text
+ * @param tStart the start
+ * @param p the pattern
+ * @param pStart the pattern start
+ * @param plen the pattern length
+ * @return whether the region matches
+ */
+ protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) {
+ while (plen-- > 0) {
+ char tchar = text.charAt(tStart++);
+ char pchar = p.charAt(pStart++);
+
+ /* process wild cards */
+ if (!fIgnoreWildCards) {
+ /* skip single wild cards */
+ if (pchar == fSingleWildCard) {
+ continue;
+ }
+ }
+ if (pchar == tchar)
+ continue;
+ if (fIgnoreCase) {
+ char tc = Character.toUpperCase(tchar);
+ if (tc == pchar)
+ continue;
+ }
+ return false;
+ }
+ return true;
+ }
+ /**
+ * @param text the string to match
+ * @param start the starting index in the text for search, inclusive
+ * @param end the stopping point of search, exclusive
+ * @param p a string that has no wildcard
+ * @return the starting index in the text of the pattern , or -1 if not found
+ */
+ protected int textPosIn(String text, int start, int end, String p) {
+
+ int plen = p.length();
+ int max = end - plen;
+
+ if (!fIgnoreCase) {
+ int i = text.indexOf(p, start);
+ if (i == -1 || i > max)
+ return -1;
+ return i;
+ }
+
+ for (int i = start; i <= max; ++i) {
+ if (text.regionMatches(true, i, p, 0, plen))
+ return i;
+ }
+
+ return -1;
+ }
+}
--
cgit v1.2.3