Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #126 from MITLibraries/104_finder_sandbox
Browse files Browse the repository at this point in the history
Adds an XPath expression testing UI
  • Loading branch information
Richard Rodgers committed Mar 20, 2015
2 parents 96a2dab + 217297d commit ff456e3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/controllers/Application.scala
Expand Up @@ -26,6 +26,7 @@ import services.contentModelJson._
import services.publisherModelJson._
import services.subscriberModelJson._
import services.Emailer
import workers.Cataloger

case class HubContext(user: Option[User])

Expand Down Expand Up @@ -936,6 +937,29 @@ object Application extends Controller {
Ok("Reindexing " + dtype + "s")
}

val sandboxForm = Form(
tuple(
"expression" -> nonEmptyText,
"resourceUrl" -> nonEmptyText
)
)

// sandbox for testing finder logic
def sandbox = Action { implicit request =>
Ok(views.html.static.sandbox(sandboxForm, List("<empty>")))
}

def testExpression = Action { implicit request =>
sandboxForm.bindFromRequest.fold(
errors => BadRequest(views.html.static.sandbox(errors, List("<error>"))),
value => {
val results = Cataloger.testExpression(value._1, "XPath", value._2)
val filledForm = sandboxForm.bindFromRequest
Ok(views.html.static.sandbox(filledForm, results))
}
)
}

// convenience method for now - refine for real use later
def purge = Action { implicit request =>
val now = new Date
Expand Down
28 changes: 28 additions & 0 deletions app/views/static/sandbox.scala.html
@@ -0,0 +1,28 @@
@*****************************************************************************
* Page for testing XPath/XQuery expressions against target resources *
* Useful for authoring Finders *
* Copyright (c) 2015 MIT Libraries *
*****************************************************************************@
@(sbForm: Form[(String, String)], results: List[String])@*(implicit hubContext: HubContext)*@
@import helper._
@layout.main("Sandbox - TopicHub") {
<div class="container">
<div class="row">
<div class="col-md-4">
@form(routes.Application.testExpression()) {
@textarea(sbForm("expression"), 'rows -> 8, 'cols -> 40)
@inputText(sbForm("resourceUrl"), 'size -> 50)
<input type="submit" value="Test Expression">
}
<br/>
<a href="@routes.Application.sandbox" class="btn btn-primary">Start Over</a>
</div>
<div class="col-md-8">
<h2>Results:</h2>
<div class="jumbotron">
@for(res <- results){<p>@res</p>}
</div>
</div>
</div>
</div>
}
1 change: 1 addition & 0 deletions app/views/static/workbench.scala.html
Expand Up @@ -22,6 +22,7 @@ <h3>Tools</h3>
@* likewise, next choice would top-level if open to all subscribers *@
<a id="sidenav_subscribers" href="@routes.Application.subscribers" class="list-group-item">Subscribers</a>
@*<a href="@routes.Application.pkgmaps" class="list-group-item">Packages</a>*@
<a id="sidenav_sandbox" href="@routes.Application.sandbox" class="list-group-item">Sandbox</a>
</div>
</div>
<div class="col-md-8">
Expand Down
30 changes: 30 additions & 0 deletions app/workers/Cataloger.scala
Expand Up @@ -6,11 +6,13 @@ package workers

import scala.xml.{Elem, Node, NodeSeq, XML}
import scala.util.matching.Regex
import scala.util.control.NonFatal
import scala.xml.factory.XMLLoader

import akka.actor.Actor

import java.io.InputStream
import java.net.URL
import java.util.Date

import javax.xml.parsers.SAXParser
Expand Down Expand Up @@ -296,6 +298,34 @@ object Cataloger {
//item.changeState("cataloged")
}

def testExpression(expr: String, exprType: String, source: String) = {
val srcStream = new URL(source).openConnection.getInputStream
val results = exprType match {
case "XPath" => testXPathExpression(expr, srcStream)
//case "XQuery" => testXQueryExpression(expr, srcStream)
case _ => List()
}
srcStream.close
results
}

def testXPathExpression(expr: String, in: InputStream) = {
val doc = convertFromScalaXml(LooseXml.load(in))
val xp = new ScalesXPath(expr).withNameConversion(ScalesXPath.localOnly)
try {
val hits = xp.evaluate(top(doc)) map ( hit =>
hit match {
case Left(x) => x.attribute.value
case Right(x) => x.foldLeft("")(_+_.item().value)
}
)
hits.toList
} catch {
case NonFatal(e) => List("<Error processing>")
}
}


def testFinder(item: Item, source: String, finder: Finder) = {
val coll = Collection.findById(item.collectionId).get
val resmap = ResourceMap.findById(coll.resmapId).get
Expand Down
2 changes: 2 additions & 0 deletions conf/routes
Expand Up @@ -127,6 +127,8 @@ GET /search/results controllers.Search.results(q: String, target
GET /reindex/:dtype controllers.Application.reindex(dtype: String)
GET /workbench controllers.Application.workbench
GET /purge controllers.Application.purge
GET /sandbox controllers.Application.sandbox
POST /testExpression controllers.Application.testExpression

# hub model services & page
GET /cmodel controllers.Application.contentModel
Expand Down

0 comments on commit ff456e3

Please sign in to comment.