add commons
This commit is contained in:
parent
cc94657d18
commit
cc7e10fb45
4 changed files with 231 additions and 0 deletions
48
src/de/unibremen/informatik/hets/common/io/IOUtils.java
Normal file
48
src/de/unibremen/informatik/hets/common/io/IOUtils.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
package de.unibremen.informatik.hets.common.io
|
||||
|
||||
public class IOUtils {
|
||||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
||||
|
||||
static {
|
||||
}
|
||||
|
||||
public IOUtils() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static long copy(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
|
||||
int nread = 0;
|
||||
long count = 0;
|
||||
while((nread = in.read(buf)) >= 0) {
|
||||
out.write(buf, 0, nread);
|
||||
count += nread;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static long copy(Reader in, Writer out) throws IOException {
|
||||
char[] buf = new char[DEFAULT_BUFFER_SIZE];
|
||||
int nread = 0;
|
||||
long count = 0;
|
||||
while((nread = in.read(buf)) >= 0) {
|
||||
out.write(buf, 0, nread);
|
||||
count += nread;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static StringWriter getStringWriter(InputStream in) throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
copy(new InputStreamReader(in), sw);
|
||||
return sw;
|
||||
}
|
||||
|
||||
public static String getString(InputStream in) throws IOException {
|
||||
return getStringWriter(in).toString();
|
||||
}
|
||||
|
||||
public static StringBuffer getBuffer(InputStream in) throws IOException {
|
||||
return getStringWriter(in).getBuffer();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package de.unibremen.informatik.hets.common.net;
|
||||
|
||||
import java.net.URLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
public class HttpPostMultipart {
|
||||
URLConnection connection;
|
||||
OutputStream os = null;
|
||||
String boundary = "---------------------------" + randomString() + randomString() + randomString();
|
||||
private static Random random = new Random();
|
||||
|
||||
protected void write(String s) throws IOException {
|
||||
os.write(s.getBytes());
|
||||
}
|
||||
|
||||
protected static String randomString() {
|
||||
return Long.toString(random.nextLong(), 36);
|
||||
}
|
||||
|
||||
public HttpPostMultipart(URL url) throws IOException {
|
||||
connection = url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestProperty("Content-Type",
|
||||
"multipart/form-data; boundary=" + boundary);
|
||||
os = connection.getOutputStream();
|
||||
}
|
||||
|
||||
public InputStream post(Map parameters) throws IOException {
|
||||
String name;
|
||||
Object object;
|
||||
if (parameters != null) {
|
||||
for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry)i.next();
|
||||
name = entry.getKey().toString();
|
||||
object = entry.getValue();
|
||||
|
||||
write("--" + boundary + "\r\n");
|
||||
write("Content-Disposition: form-data; name=\"" + name + "\"");
|
||||
if (object instanceof File) {
|
||||
write("; filename=\"" + ((File)object).getPath() + "\"\r\n");
|
||||
String type = connection.guessContentTypeFromName(((File)object).getPath());
|
||||
if (type == null)
|
||||
type = "application/octet-stream";
|
||||
write("Content-Type: " + type + "\r\n");
|
||||
write("\r\n");
|
||||
pipe(new FileInputStream((File)object), os);
|
||||
} else {
|
||||
write("\r\n");
|
||||
write("\r\n");
|
||||
write(object.toString());
|
||||
}
|
||||
write("\r\n");
|
||||
}
|
||||
}
|
||||
write("--" + boundary + "--\r\n");
|
||||
os.close();
|
||||
return connection.getInputStream();
|
||||
}
|
||||
|
||||
public static InputStream post(URL url, Map parameters) throws IOException {
|
||||
return new HttpPostMultipart(url).post(parameters);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package de.unibremen.informatik.hets.common.net;
|
||||
|
||||
import java.net.URLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.Iterator;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
public class HttpPostUrlencoded {
|
||||
URLConnection connection;
|
||||
OutputStream os = null;
|
||||
|
||||
protected void write(String s) throws IOException {
|
||||
os.write(s.getBytes());
|
||||
}
|
||||
|
||||
public HttpPostUrlencoded(URL url) throws IOException {
|
||||
connection = url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestProperty("Content-Type",
|
||||
"application/x-www-form-urlencoded");
|
||||
os = connection.getOutputStream();
|
||||
}
|
||||
|
||||
public InputStream post(Map parameters) throws IOException {
|
||||
String name;
|
||||
Object object;
|
||||
if (parameters != null) {
|
||||
for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry)i.next();
|
||||
name = entry.getKey().toString();
|
||||
object = entry.getValue();
|
||||
|
||||
write(name + "=");
|
||||
if (object instanceof File) {
|
||||
write(URLEncoder.encode(InputStreamToString(new FileInputStream((File)object)), "UTF-8"));
|
||||
} else {
|
||||
write(object.toString());
|
||||
}
|
||||
write("&");
|
||||
}
|
||||
}
|
||||
os.close();
|
||||
return connection.getInputStream();
|
||||
}
|
||||
|
||||
public static InputStream post(URL url, Map parameters) throws IOException {
|
||||
return new HttpPostUrlencoded(url).post(parameters);
|
||||
}
|
||||
}
|
57
src/de/unibremen/informatik/hets/common/xml/Dom.java
Normal file
57
src/de/unibremen/informatik/hets/common/xml/Dom.java
Normal file
|
@ -0,0 +1,57 @@
|
|||
package de.unibremen.informatik.hets.common.xml
|
||||
|
||||
public class Dom {
|
||||
static {
|
||||
}
|
||||
|
||||
public Dom() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static String getTextContent(Element item) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
NodeList list = ((Node)item).getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
Node child = list.item(i);
|
||||
if (child.getNodeType() == Node.TEXT_NODE) {
|
||||
sb.append(child.getNodeValue());
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static Element getFirstChildElement(Element item) {
|
||||
return getFirstChildElement((Node)item);
|
||||
}
|
||||
|
||||
public static Element getFirstChildElement(Node item) {
|
||||
NodeList list = item.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
Node child = list.item(i);
|
||||
if (child.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return (Element)child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Node getFirstChild(Node item, String name) {
|
||||
NodeList list = item.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
if (list.item(i).getNodeName() == name) {
|
||||
return list.item(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean hasChildNode(Node item, String name) {
|
||||
NodeList list = item.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
if (list.item(i).getNodeName() == name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue