Revision: 2484
http://sourceforge.net/p/swingme/code/2484
Author: yuranet
Date: 2021-06-28 13:52:22 +0000 (Mon, 28 Jun 2021)
Log Message:
-----------
moved shared impl classes here
Modified Paths:
--------------
SwingME/build.gradle
SwingME/src/net/yura/mobile/gui/layout/XULLoader.java
Added Paths:
-----------
SwingME/src_se/
SwingME/src_se/net/
SwingME/src_se/net/yura/
SwingME/src_se/net/yura/midp/
SwingME/src_se/net/yura/midp/io/
SwingME/src_se/net/yura/midp/io/HttpConnectionImpl.java
SwingME/src_se/net/yura/midp/io/HttpsConnectionImpl.java
SwingME/src_se/net/yura/midp/io/JavaFileConnection.java
Modified: SwingME/build.gradle
===================================================================
--- SwingME/build.gradle 2021-06-28 11:00:47 UTC (rev 2483)
+++ SwingME/build.gradle 2021-06-28 13:52:22 UTC (rev 2484)
@@ -9,7 +9,7 @@
sourceSets {
main {
java {
- srcDirs = ['src', '../UtilME/src', '../UtilME/src_se']
+ srcDirs = ['src', 'src_se', '../UtilME/src', '../UtilME/src_se']
}
}
}
Modified: SwingME/src/net/yura/mobile/gui/layout/XULLoader.java
===================================================================
--- SwingME/src/net/yura/mobile/gui/layout/XULLoader.java 2021-06-28 11:00:47 UTC (rev 2483)
+++ SwingME/src/net/yura/mobile/gui/layout/XULLoader.java 2021-06-28 13:52:22 UTC (rev 2484)
@@ -56,6 +56,7 @@
import net.yura.mobile.gui.components.Window;
import net.yura.mobile.gui.plaf.Style;
import net.yura.mobile.gui.plaf.SynthLookAndFeel;
+import net.yura.mobile.io.FileUtil;
import net.yura.mobile.io.UTF8InputStreamReader;
import net.yura.mobile.io.kxml2.KXmlParser;
import net.yura.mobile.logging.Logger;
@@ -97,20 +98,16 @@
public static final int VK_F2 = 113;
public static XULLoader load(InputStream is, ActionListener listener) throws Exception {
-
XULLoader loader = new XULLoader();
-
loader.load(new UTF8InputStreamReader(is),listener);
-
+ FileUtil.close(is);
return loader;
}
public static XULLoader load(InputStream is, ActionListener listener, Properties properties) throws Exception {
-
XULLoader loader = new XULLoader();
-
loader.load(new UTF8InputStreamReader(is),listener,properties);
-
+ FileUtil.close(is);
return loader;
}
Added: SwingME/src_se/net/yura/midp/io/HttpConnectionImpl.java
===================================================================
--- SwingME/src_se/net/yura/midp/io/HttpConnectionImpl.java (rev 0)
+++ SwingME/src_se/net/yura/midp/io/HttpConnectionImpl.java 2021-06-28 13:52:22 UTC (rev 2484)
@@ -0,0 +1,240 @@
+package net.yura.midp.io;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.microedition.io.HttpConnection;
+
+public class HttpConnectionImpl implements HttpConnection {
+
+ private static final int STATE_SETUP = 0;
+ private static final int STATE_CONNECTED = 1;
+
+ private int state = STATE_SETUP;
+
+ private String urlString;
+ private String requestMethod = GET;
+
+ private URL url;
+ private HttpURLConnection connection;
+ private InputStream input;
+ private OutputStream output;
+
+ public HttpConnectionImpl(String url) {
+ if (url == null) {
+ throw new NullPointerException();
+ }
+ checkIsValidUrl(url);
+ this.urlString = url;
+ try {
+ this.url = new URL(url);
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException("invalid URL");
+ }
+ }
+
+ public HttpConnectionImpl(String url, int mode) {
+ this(url);
+ }
+
+ protected void checkIsValidUrl(String checkUrl) {
+ if (checkUrl.indexOf("http:") != 0
+ || (checkUrl.length() <= "http:".length())) {
+ throw new IllegalArgumentException("invalid URL " + checkUrl);
+ }
+ }
+
+ public void close() throws IOException {
+ if (input != null) {
+ input.close();
+ }
+ if (output != null) {
+ output.close();
+ }
+ }
+
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return new DataOutputStream(openOutputStream());
+ }
+
+ public OutputStream openOutputStream() throws IOException {
+ if (output != null) {
+ throw new IOException("already opened");
+ }
+ connect();
+ connection.setDoOutput(true);
+ output = this.connection.getOutputStream();
+ return output;
+ }
+
+ // only in setup state
+ public void setRequestMethod(String requestMethod) throws IOException {
+ if (state == STATE_SETUP) {
+ if (requestMethod.equals(GET)) {
+ this.requestMethod = requestMethod;
+ } else if (requestMethod.equals(POST)) {
+ this.requestMethod = requestMethod;
+ } else {
+ throw new IllegalArgumentException("illegal request method "
+ + requestMethod);
+ }
+ } else {
+ throw new IOException("already connected");
+ }
+ }
+
+ public void setRequestProperty(String key, String value) throws IOException {
+ connect();
+ connection.setRequestProperty(key, value);
+ }
+
+ // invoke at any time
+ public String getRequestMethod() {
+ return requestMethod;
+ }
+
+ public String getRequestProperty(String key) {
+ try {
+ connect();
+ } catch (IOException e) {
+ return "";
+ }
+ return connection.getRequestProperty(key);
+ }
+
+ public String getURL() {
+ return urlString;
+ }
+
+ public String getQuery() {
+ return url.getQuery();
+ }
+
+ public int getPort() {
+ return url.getPort();
+ }
+
+ public String getHost() {
+ return url.getHost();
+ }
+
+ public String getProtocol() {
+ return url.getProtocol();
+ }
+
+ public String getFile() {
+ return url.getFile();
+ }
+
+ public String getRef() {
+ return url.getRef();
+ }
+
+ // these calls force transition to connected state
+ public DataInputStream openDataInputStream() throws IOException {
+ return new DataInputStream(openInputStream());
+ }
+
+ public InputStream openInputStream() throws IOException {
+ connect();
+ input = connection.getInputStream();
+ return input;
+ }
+
+ public long getLength() {
+ try {
+ connect();
+ } catch (IOException ex) {
+ return 0;
+ }
+ return connection.getContentLength();
+ }
+
+ public String getType() {
+ try {
+ connect();
+ } catch (IOException ex) {
+ return "";
+ }
+ return connection.getContentType();
+ }
+
+ public String getEncoding() {
+ try {
+ connect();
+ } catch (IOException ex) {
+ return "";
+ }
+ return connection.getContentEncoding();
+ }
+
+ public String getHeaderField(String name) throws IOException {
+ connect();
+ return connection.getHeaderField(name);
+ }
+
+ public String getHeaderField(int n) throws IOException {
+ connect();
+ return connection.getHeaderField(n);
+ }
+
+ public int getResponseCode() throws IOException {
+ connect();
+ return connection.getResponseCode();
+ }
+
+ public String getResponseMessage() throws IOException {
+ connect();
+ return connection.getResponseMessage();
+ }
+
+ public int getHeaderFieldInt(String name, int def) throws IOException {
+ connect();
+ return connection.getHeaderFieldInt(name, def);
+ }
+
+ public long getHeaderFieldDate(String name, long def) throws IOException {
+ connect();
+ return connection.getHeaderFieldDate(name, def);
+ }
+
+ public String getHeaderFieldKey(int n) throws IOException {
+ connect();
+ return connection.getHeaderFieldKey(n);
+ }
+
+ public long getDate() throws IOException {
+ connect();
+ return connection.getDate();
+ }
+
+ public long getExpiration() throws IOException {
+ connect();
+ return connection.getExpiration();
+ }
+
+ public long getLastModified() throws IOException {
+ connect();
+ return connection.getLastModified();
+ }
+
+ protected synchronized void connect() throws IOException {
+ if (state == STATE_CONNECTED) {
+ if (connection == null) {
+ throw new IOException(
+ "Invalid State. No connection in state STATE_CONNECTED");
+ }
+ return;
+ } else {
+ state = STATE_CONNECTED;
+ }
+ connection = (HttpURLConnection) this.url.openConnection();
+ connection.setRequestMethod(this.requestMethod);
+ }
+}
Property changes on: SwingME/src_se/net/yura/midp/io/HttpConnectionImpl.java
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: SwingME/src_se/net/yura/midp/io/HttpsConnectionImpl.java
===================================================================
--- SwingME/src_se/net/yura/midp/io/HttpsConnectionImpl.java (rev 0)
+++ SwingME/src_se/net/yura/midp/io/HttpsConnectionImpl.java 2021-06-28 13:52:22 UTC (rev 2484)
@@ -0,0 +1,36 @@
+package net.yura.midp.io;
+
+import java.io.IOException;
+
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpsConnection;
+import javax.microedition.io.SecurityInfo;
+
+public class HttpsConnectionImpl extends HttpConnectionImpl implements
+ HttpsConnection {
+
+ public HttpsConnectionImpl(String url) {
+ this(url, Connector.READ_WRITE);
+ }
+
+ public HttpsConnectionImpl(String url, int mode) {
+ super(url, mode);
+ }
+
+ protected void checkIsValidUrl(String checkUrl) {
+ if (checkUrl.indexOf("https:") != 0
+ || (checkUrl.length() <= "https:".length())) {
+ throw new IllegalArgumentException("invalid URL " + checkUrl);
+ }
+ }
+
+ public SecurityInfo getSecurityInfo() throws IOException {
+ connect();
+ // TODO implement security info
+ return null;
+ }
+
+ public int getPort() {
+ return super.getPort();
+ }
+}
Property changes on: SwingME/src_se/net/yura/midp/io/HttpsConnectionImpl.java
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: SwingME/src_se/net/yura/midp/io/JavaFileConnection.java
===================================================================
--- SwingME/src_se/net/yura/midp/io/JavaFileConnection.java (rev 0)
+++ SwingME/src_se/net/yura/midp/io/JavaFileConnection.java 2021-06-28 13:52:22 UTC (rev 2484)
@@ -0,0 +1,316 @@
+package net.yura.midp.io;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.Vector;
+import java.util.regex.Pattern;
+import javax.microedition.io.file.FileConnection;
+
+public class JavaFileConnection implements FileConnection {
+
+ private static final String SPECIAL_CHARACTERS = "*.^?[]\\";
+
+ public static final File getParentDirectory(File file) {
+ String path = file.getAbsolutePath();
+ if (path.endsWith(File.separator)) {
+ path = path.substring(0, path.length() - File.separator.length());
+ }
+ int index = path.lastIndexOf(File.separator);
+ File result;
+ if (index >= 0) {
+ path = path.substring(0, index);
+ result = new File(path);
+ } else {
+ result = null;
+ }
+ return result;
+ }
+
+ private static final String escape(String s) {
+ StringBuffer result = new StringBuffer(s.length());
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ if (SPECIAL_CHARACTERS.indexOf(c) >= 0) {
+ result.append("\\");
+ }
+ result.append(c);
+ }
+ return result.toString();
+ }
+
+ private static final File getFile(String url) throws IOException {
+ final String URL_FILE_ROOT = "file:///";
+ if (url == null || !url.startsWith(URL_FILE_ROOT)) {
+ throw new IOException("Invalid file url");
+ }
+
+ String filePath = url.substring(URL_FILE_ROOT.length());
+ return new File(filePath);
+ }
+
+ private File file;
+ private boolean open;
+
+ public JavaFileConnection(String url) throws IOException {
+ this.file = getFile(url);
+ this.open = true;
+ }
+
+ // Override
+ public long availableSize() {
+ // this isn't available
+ return -1;
+ }
+
+ // Override
+ public boolean canRead() {
+ return this.file.canRead();
+ }
+
+ // Override
+ public boolean canWrite() {
+ return this.file.canWrite();
+ }
+
+ // Override
+ public void create() throws IOException {
+ if (!this.file.createNewFile()) {
+ throw new IOException("file creation failed");
+ }
+ }
+
+ // Override
+ public void delete() throws IOException {
+ if (!this.file.delete()) {
+ throw new IOException("file deletion failed");
+ }
+ }
+
+ // Override
+ public long directorySize(boolean includeSubDirs) throws IOException {
+ // why is this in the interface?
+ return getDirectorySize(this.file, includeSubDirs);
+ }
+
+ private long getDirectorySize(File file, boolean recursive) {
+ long size = 0;
+ File[] children = file.listFiles();
+ for (File child : children) {
+ if (child.isDirectory()) {
+ if (recursive) {
+ size += getDirectorySize(child, recursive);
+ }
+ } else {
+ size += child.length();
+ }
+ }
+ return size;
+ }
+
+ // Override
+ public boolean exists() {
+ return this.file.exists();
+ }
+
+ // Override
+ public long fileSize() throws IOException {
+ return this.file.length();
+ }
+
+ // Override
+ public String getName() {
+ return this.file.getName();
+ }
+
+ // Override
+ public String getPath() {
+ return this.file.getPath();
+ }
+
+ // Override
+ public String getURL() {
+ return this.file.toURI().toString();
+ }
+
+ // Override
+ public boolean isDirectory() {
+ return this.file.isDirectory();
+ }
+
+ // Override
+ public boolean isHidden() {
+ return this.file.isHidden();
+ }
+
+ // Override
+ public boolean isOpen() {
+ return this.open;
+ }
+
+ // Override
+ public long lastModified() {
+ return this.file.lastModified();
+ }
+
+ // Override
+ public Enumeration list() throws IOException {
+ return list(null, false);
+ }
+
+ /**
+ * An asterisk ("*") can be used as a wildcard to represent 0 or more occurrences of any character.
+ */
+ // Override
+ public Enumeration list(String filter, boolean includeHidden)
+ throws IOException {
+ Pattern pattern;
+ if (filter == null || "*".equals(filter)) {
+ pattern = Pattern.compile(".*");
+ } else {
+ String[] literalParts = filter.split("\\*");
+ StringBuffer sb = new StringBuffer(filter.length());
+ for (int i = 0; i < literalParts.length; i++) {
+ String part = literalParts[i];
+ String literalPart = escape(part);
+ if (i > 0) {
+ sb.append("*");
+ }
+ sb.append(literalPart);
+ }
+ pattern = Pattern.compile(sb.toString());
+ }
+
+ final Pattern filterPattern = pattern;
+ File[] a = this.file.listFiles(new FilenameFilter() {
+
+ // Override
+ public boolean accept(File dir, String name) {
+ return filterPattern.matcher(name).matches();
+ }
+
+ });
+
+ if (a==null) a = new File[0]; // avoid nullpointer
+
+ Vector<String> v = new Vector<String>();
+ for (File file : a) {
+ String fileName = file.getName();
+ if (file.isDirectory() && !fileName.endsWith(File.separator)) {
+ fileName += File.separator;
+ }
+ v.add(fileName);
+ }
+
+ return v.elements();
+ }
+
+ // Override
+ public void mkdir() throws IOException {
+ if (!this.file.mkdir()) {
+ throw new IOException("unable to create directory");
+ }
+
+ }
+
+ // Override
+ public DataInputStream openDataInputStream() throws IOException {
+ return new DataInputStream(this.openInputStream());
+ }
+
+ // Override
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return new DataOutputStream(this.openOutputStream());
+ }
+
+ // Override
+ public InputStream openInputStream() throws IOException {
+ return new FileInputStream(this.file);
+ }
+
+ // Override
+ public OutputStream openOutputStream() throws IOException {
+ return openOutputStream(0);
+ }
+
+ // Override
+ public OutputStream openOutputStream(long byteOffset) throws IOException {
+ boolean append;
+ if (byteOffset == 0) {
+ append = false;
+ } else if (byteOffset < this.file.length()) {
+ append = true;
+ } else {
+ throw new IOException("offsets not supported");
+ }
+ FileOutputStream fos = new FileOutputStream(this.file, append);
+ return fos;
+ }
+
+ // Override
+ public void rename(String newName) throws IOException {
+ File directory = getParentDirectory(this.file);
+ File targetFile = new File(directory, newName);
+ this.file.renameTo(targetFile);
+ }
+
+ // Override
+ public void setFileConnection(String fileName) throws IOException {
+ if (fileName.equals("..")) {
+ File directory = getParentDirectory(this.file);
+ if (directory == null) {
+ throw new IOException("no parent dir");
+ }
+ this.file = directory;
+ } else {
+ this.file = new File(this.file, fileName);
+ }
+ }
+
+ // Override
+ public void setHidden(boolean hidden) throws IOException {
+ throw new IOException("unsupported");
+ }
+
+ public void setReadable(boolean readable) throws IOException {
+ try {
+ file.setReadable(readable); // ONLY API-9 (2.3+) GINGERBREAD
+ }
+ catch (NoSuchMethodError ex) { }
+ }
+
+ public void setWritable(boolean writable) throws IOException {
+ try {
+ file.setWritable(writable); // ONLY API-9 (2.3+) GINGERBREAD
+ }
+ catch (NoSuchMethodError ex) { }
+ }
+
+ // Override
+ public long totalSize() {
+ return this.file.length();
+ }
+
+ // Override
+ public void truncate(long byteOffset) throws IOException {
+ throw new IOException("unsupported");
+ }
+
+ // Override
+ public long usedSize() {
+ return this.file.length();
+ }
+
+ // Override
+ public void close() throws IOException {
+ this.open = false;
+ }
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|