Skip to content

Commit b6ff075

Browse files
committed
Merge pull request #117 from rhubner/url-decode
Url decode
2 parents 4726be4 + aa952fc commit b6ff075

File tree

17 files changed

+275
-7
lines changed

17 files changed

+275
-7
lines changed

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/api/CsvValidator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import uk.gov.nationalarchives.csv.validator.schema.{Schema, SchemaParser}
1212
import scalaz._, Scalaz._
1313
import scalax.file.Path
1414
import uk.gov.nationalarchives.csv.validator._
15-
import java.io.{Reader => JReader, File}
16-
import java.nio.charset.{Charset => JCharset}
15+
import _root_.java.io.{Reader => JReader, File}
16+
import _root_.java.nio.charset.{Charset => JCharset}
1717

1818
object CsvValidator {
1919

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/Schema.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import util.parsing.input.Positional
1515
case class Schema(globalDirectives: List[GlobalDirective], columnDefinitions: List[ColumnDefinition], version: String = Schema.version)
1616

1717
object Schema {
18-
val version = "1.1"
18+
val version = "1.2"
1919
}
2020

2121
abstract class GlobalDirective(val name: String) extends Positional

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/SchemaParser.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package uk.gov.nationalarchives.csv.validator.schema
1010

1111
import uk.gov.nationalarchives.csv.validator.schema.v1_0.{SchemaParser => SchemaParser1_0}
1212
import uk.gov.nationalarchives.csv.validator.schema.v1_1.{SchemaParser => SchemaParser1_1, _}
13+
import uk.gov.nationalarchives.csv.validator.schema.v1_2.{SchemaParser => SchemaParser1_2, _}
1314

1415
import scala.util.parsing.combinator._
1516
import scala.language.reflectiveCalls
@@ -147,6 +148,18 @@ with TraceableParsers {
147148

148149
SchemaValidator.versionValid(version).map(Failure(_, next)).getOrElse {
149150
version match {
151+
case "1.2" =>
152+
val parser1_2 = new SchemaParser1_2 {override val enforceCaseSensitivePathChecks: Boolean = ecspc
153+
override val pathSubstitutions: List[(String, String)] = ps
154+
override val trace: Boolean = t
155+
}
156+
157+
parser1_2.parseVersionAware(reader) match {
158+
case parser1_2.Success(s, n) => Success(s, n)
159+
case parser1_2.Failure(msg, n) => Failure(msg, n)
160+
case parser1_2.Error(msg, n) => Error(msg, n)
161+
}
162+
150163
case "1.1" =>
151164
val parser1_1 = new SchemaParser1_1 {override val enforceCaseSensitivePathChecks: Boolean = ecspc
152165
override val pathSubstitutions: List[(String, String)] = ps
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* http://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_2
10+
11+
import uk.gov.nationalarchives.csv.validator.metadata.Row
12+
import uk.gov.nationalarchives.csv.validator.schema.{Schema, ArgProvider}
13+
import java.net.{URLDecoder => JURLDecoder}
14+
15+
case class UriDecode(value: ArgProvider, charset: Option[ArgProvider]) extends ArgProvider {
16+
17+
val DefaultCharset = "UTF-8"
18+
19+
override def referenceValue(columnIndex: Int, row: Row, schema: Schema): Option[String] = value.referenceValue(columnIndex, row, schema).map( value => {
20+
21+
val codepage = charset.flatMap(x => x.referenceValue(columnIndex, row, schema)).getOrElse(DefaultCharset)
22+
23+
JURLDecoder.decode(value,codepage)
24+
25+
})
26+
27+
override def toError: String = "uriDecode(" + value.toError + ")"
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* http://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_2
10+
11+
import scala.language.reflectiveCalls
12+
import uk.gov.nationalarchives.csv.validator.schema.{Literal, ArgProvider}
13+
import uk.gov.nationalarchives.csv.validator.schema.v1_1.{SchemaParser => SchemaParser1_1}
14+
15+
trait SchemaParser extends SchemaParser1_1 {
16+
17+
/**
18+
* [59] StringProvider ::= ColumnRef | StringLiteral
19+
*/
20+
override lazy val stringProvider: PackratParser[ArgProvider] = "StringProvider" ::= noext | concat | urlDecode | columnRef | stringLiteral ^^ {
21+
s => Literal(Some(s))
22+
}
23+
24+
lazy val urlDecode: PackratParser[ArgProvider] = "UriDecode" ::= "uriDecode(" ~> stringProvider ~ opt("," ~> stringProvider) <~ ")" ^^ {
25+
case value ~ charset => UriDecode(value, charset)
26+
}
27+
28+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version 1.2
2+
@totalColumns 2 @noHeader
3+
identifier:
4+
filename: in(uriDecode($identifier))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file:/some/folder/some%21file.txt,some file.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file:/some/folder/some%20file.txt,some file.txt
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version 1.2
2+
@totalColumns 3 @noHeader
3+
identifier:
4+
filename: in(uriDecode($identifier, $charset))
5+
charset:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
file:/some/folder/some%20file.txt,some file.txt,UTF-8
2+
file:/some/folder/text%9Atext.txt,textštext.txt,windows-1252

0 commit comments

Comments
 (0)