Skip to content

Commit

Permalink
Reformatted code: tabs -> 2 spaces, line length 80 -> 120
Browse files Browse the repository at this point in the history
  • Loading branch information
Villane committed Oct 5, 2011
1 parent 0515ba0 commit 8650935
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 440 deletions.
90 changes: 45 additions & 45 deletions src/main/java/org/villane/shttpc/BetterXPath.scala
@@ -1,45 +1,45 @@
package org.villane.shttpc

import xml._

object BetterXPath {
implicit def betterXPath(node: NodeSeq) = new BetterXPath(node)
}

/**
* Supports a subset of XPath expressions in the form of
*
* /tag1/tag2[@attr='stringLiteral']/tag2
* /tag1/tag2[tag4=@attr2]/tag2
*/
class BetterXPath(node: NodeSeq) {
import BetterXPath._

def /(path: String): NodeSeq = {
if (path.contains("/")) {
val segs = path.split("/")
var n = node
for (seg <- segs) n /= seg
n
} else if (path.contains("[")) {
val segs = path.split("[\\[\\]]")
val ns = node \ segs(0)
val Array(left,right) = segs(1).split("=")
ns filter { n =>
evaluatePredicate(n, left) == evaluatePredicate(n, right)
}
} else {
node \ path
}
}

private def evaluatePredicate(n: NodeSeq, expr: String) = {
if (expr.startsWith("'") && expr.endsWith("'")) {
// string literal
expr.substring(1, expr.length - 1)
} else {
// usual attribute or element name
n \ expr
}
}
}
package org.villane.shttpc

import xml._

object BetterXPath {
implicit def betterXPath(node: NodeSeq) = new BetterXPath(node)
}

/**
* Supports a subset of XPath expressions in the form of
*
* /tag1/tag2[@attr='stringLiteral']/tag2
* /tag1/tag2[tag4=@attr2]/tag2
*/
class BetterXPath(node: NodeSeq) {
import BetterXPath._

def /(path: String): NodeSeq = {
if (path.contains("/")) {
val segs = path.split("/")
var n = node
for (seg <- segs) n /= seg
n
} else if (path.contains("[")) {
val segs = path.split("[\\[\\]]")
val ns = node \ segs(0)
val Array(left, right) = segs(1).split("=")
ns filter { n =>
evaluatePredicate(n, left) == evaluatePredicate(n, right)
}
} else {
node \ path
}
}

private def evaluatePredicate(n: NodeSeq, expr: String) = {
if (expr.startsWith("'") && expr.endsWith("'")) {
// string literal
expr.substring(1, expr.length - 1)
} else {
// usual attribute or element name
n \ expr
}
}
}
125 changes: 59 additions & 66 deletions src/main/java/org/villane/shttpc/Http.java
Expand Up @@ -24,82 +24,75 @@
* @author erkki.lindpere
*/
public class Http {
public static String DefaultPostEncoding = "UTF-8";
public static String DefaultURIEncoding = "UTF-8";
public static String DefaultPostEncoding = "UTF-8";
public static String DefaultURIEncoding = "UTF-8";

public final HttpClient client;
public final HttpClient client;

public Http() {
this.client = new DefaultHttpClient();
}
public Http() {
this.client = new DefaultHttpClient();
}

public Http(HttpClient client) {
this.client = client;
}
public Http(HttpClient client) {
this.client = client;
}

public static Http newTrustingInstace() {
return new Http(new TrustingHttpClient());
}
public static Http newTrustingInstace() {
return new Http(new TrustingHttpClient());
}

public SimpleHttpResponse get(String uri) throws ClientProtocolException,
IOException {
HttpGet get = new HttpGet(uri);
return new SimpleHttpResponse(client.execute(get));
}
public SimpleHttpResponse get(String uri) throws ClientProtocolException, IOException {
HttpGet get = new HttpGet(uri);
return new SimpleHttpResponse(client.execute(get));
}

public SimpleHttpResponse get(String uri, Object... params)
throws ClientProtocolException, IOException {
return get(formatURI(uri, params));
}
public SimpleHttpResponse get(String uri, Object... params) throws ClientProtocolException, IOException {
return get(formatURI(uri, params));
}

public SimpleHttpResponse post(String uri, Object[] uriParams,
Map<String, String> postParams) throws ClientProtocolException,
IOException {
return post(formatURI(uri, uriParams), postParams);
}
public SimpleHttpResponse post(String uri, Object[] uriParams, Map<String, String> postParams)
throws ClientProtocolException, IOException {
return post(formatURI(uri, uriParams), postParams);
}

public SimpleHttpResponse post(String uri, Map<String, String> postParams)
throws ClientProtocolException, IOException {
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : postParams.entrySet()) {
paramsList.add(new BasicNameValuePair(param.getKey(), param
.getValue()));
}
HttpPost post = new HttpPost(uri);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramsList,
DefaultPostEncoding);
post.setEntity(entity);
return new SimpleHttpResponse(client.execute(post));
}
public SimpleHttpResponse post(String uri, Map<String, String> postParams) throws ClientProtocolException,
IOException {
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : postParams.entrySet()) {
paramsList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
}
HttpPost post = new HttpPost(uri);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramsList, DefaultPostEncoding);
post.setEntity(entity);
return new SimpleHttpResponse(client.execute(post));
}

public SimpleHttpResponse delete(String uri)
throws ClientProtocolException, IOException {
HttpDelete del = new HttpDelete(uri);
return new SimpleHttpResponse(client.execute(del));
}
public SimpleHttpResponse delete(String uri) throws ClientProtocolException, IOException {
HttpDelete del = new HttpDelete(uri);
return new SimpleHttpResponse(client.execute(del));
}

public SimpleHttpResponse delete(String uri, Object... params)
throws ClientProtocolException, IOException {
return delete(formatURI(uri, params));
}
public SimpleHttpResponse delete(String uri, Object... params) throws ClientProtocolException, IOException {
return delete(formatURI(uri, params));
}

protected static String formatURI(String uri, Object... params) {
if (params.length > 0) {
String[] encodedParams = new String[params.length];
for (int i = 0; i < params.length; i++) {
encodedParams[i] = uriEncode(params[i].toString());
}
return MessageFormatter.arrayFormat(uri, encodedParams).getMessage();
}
return uri;
}
protected static String formatURI(String uri, Object... params) {
if (params.length > 0) {
String[] encodedParams = new String[params.length];
for (int i = 0; i < params.length; i++) {
encodedParams[i] = uriEncode(params[i].toString());
}
return MessageFormatter.arrayFormat(uri, encodedParams).getMessage();
}
return uri;
}

@SuppressWarnings("deprecation")
protected static String uriEncode(String value) {
try {
return URLEncoder.encode(value, DefaultURIEncoding);
} catch (UnsupportedEncodingException e) {
return URLEncoder.encode(value);
}
}
@SuppressWarnings("deprecation")
protected static String uriEncode(String value) {
try {
return URLEncoder.encode(value, DefaultURIEncoding);
} catch (UnsupportedEncodingException e) {
return URLEncoder.encode(value);
}
}
}
100 changes: 50 additions & 50 deletions src/main/java/org/villane/shttpc/IterableNodeList.java
@@ -1,50 +1,50 @@
package org.villane.shttpc;

import java.util.Iterator;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IterableNodeList implements Iterable<Node>, NodeList {

public final NodeList nodeList;

public IterableNodeList(NodeList nodeList) {
this.nodeList = nodeList;
}

@Override
public Iterator<Node> iterator() {
return new NodeListIterator();
}

public int getLength() {
return nodeList.getLength();
}

public Node item(int index) {
return nodeList.item(index);
}

public class NodeListIterator implements Iterator<Node> {

private int idx = 0;

@Override
public boolean hasNext() {
return idx < nodeList.getLength();
}

@Override
public Node next() {
idx += 1;
return nodeList.item(idx - 1);
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

}
}
package org.villane.shttpc;

import java.util.Iterator;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IterableNodeList implements Iterable<Node>, NodeList {

public final NodeList nodeList;

public IterableNodeList(NodeList nodeList) {
this.nodeList = nodeList;
}

@Override
public Iterator<Node> iterator() {
return new NodeListIterator();
}

public int getLength() {
return nodeList.getLength();
}

public Node item(int index) {
return nodeList.item(index);
}

public class NodeListIterator implements Iterator<Node> {

private int idx = 0;

@Override
public boolean hasNext() {
return idx < nodeList.getLength();
}

@Override
public Node next() {
idx += 1;
return nodeList.item(idx - 1);
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

}
}

0 comments on commit 8650935

Please sign in to comment.