Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reformatted code: tabs -> 2 spaces, line length 80 -> 120
- Loading branch information
Showing
10 changed files
with
427 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.