Skip to content

Commit

Permalink
Add MediaType.raw()
Browse files Browse the repository at this point in the history
  • Loading branch information
ato committed Jun 28, 2023
1 parent f2f8d38 commit bd907ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
31 changes: 20 additions & 11 deletions src/org/netpreserve/jwarc/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// line 1 "MediaType.rl"
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2018 National Library of Australia and the jwarc contributors
* Copyright (C) 2018-2023 National Library of Australia and the jwarc contributors
*/

// recompile: ragel -J MediaType.rl -o MediaType.java
Expand Down Expand Up @@ -168,10 +168,11 @@ private static byte[] init__media_type_eof_actions_0()
public static MediaType HTTP_REQUEST = MediaType.parse("application/http;msgtype=request");
public static MediaType HTTP_RESPONSE = MediaType.parse("application/http;msgtype=response");
public static MediaType OCTET_STREAM = MediaType.parse("application/octet-stream");
public static MediaType PLAIN_TEXT = MediaType.parse("text/plain");
public static MediaType PLAIN_TEXT = MediaType.parse("text/plain");
public static MediaType WARC_FIELDS = MediaType.parse("application/warc-fields");
public static final MediaType WWW_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded");

private final String raw;
private final String type;
private final String subtype;
private final Map<String,String> parameters;
Expand All @@ -194,14 +195,14 @@ public static MediaType parse(String string) {
StringBuilder buf = new StringBuilder();


// line 197 "MediaType.java"
// line 199 "MediaType.java"
{
cs = media_type_start;
}

// line 95 "MediaType.rl"
// line 97 "MediaType.rl"

// line 204 "MediaType.java"
// line 206 "MediaType.java"
{
int _klen;
int _trans = 0;
Expand Down Expand Up @@ -341,7 +342,7 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] )
// line 48 "MediaType.rl"
{ subtypeEnd = p; }
break;
// line 344 "MediaType.java"
// line 346 "MediaType.java"
}
}
}
Expand Down Expand Up @@ -390,7 +391,7 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] )
// line 48 "MediaType.rl"
{ subtypeEnd = p; }
break;
// line 393 "MediaType.java"
// line 395 "MediaType.java"
}
}
}
Expand All @@ -400,20 +401,28 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] )
break; }
}

// line 96 "MediaType.rl"
// line 98 "MediaType.rl"

String type = string.substring(0, typeEnd);
String subtype = string.substring(typeEnd + 1, subtypeEnd);
Map<String,String> parameters = Collections.unmodifiableMap(map);
return new MediaType(type, subtype, parameters);
return new MediaType(string, type, subtype, parameters);
}

private MediaType(String type, String subtype, Map<String,String> parameters) {
private MediaType(String raw, String type, String subtype, Map<String,String> parameters) {
this.raw = raw;
this.type = type;
this.subtype = subtype;
this.parameters = parameters;
}

/**
* The original unparsed media type string.
*/
public String raw() {
return raw;
}

public String type() {
return type;
}
Expand Down Expand Up @@ -478,7 +487,7 @@ public String toString() {
* The base type and subtype without any parameters.
*/
public MediaType base() {
return new MediaType(type, subtype, Collections.emptyMap());
return new MediaType(null, type, subtype, Collections.emptyMap());
}

private static boolean validToken(String s) {
Expand Down
18 changes: 14 additions & 4 deletions src/org/netpreserve/jwarc/MediaType.rl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2018 National Library of Australia and the jwarc contributors
* Copyright (C) 2018-2023 National Library of Australia and the jwarc contributors
*/

// recompile: ragel -J MediaType.rl -o MediaType.java
Expand Down Expand Up @@ -67,9 +67,11 @@ public class MediaType extends MessageParser {
public static MediaType HTTP_REQUEST = MediaType.parse("application/http;msgtype=request");
public static MediaType HTTP_RESPONSE = MediaType.parse("application/http;msgtype=response");
public static MediaType OCTET_STREAM = MediaType.parse("application/octet-stream");
public static MediaType PLAIN_TEXT = MediaType.parse("text/plain");
public static MediaType WARC_FIELDS = MediaType.parse("application/warc-fields");
public static final MediaType WWW_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded");

private final String raw;
private final String type;
private final String subtype;
private final Map<String,String> parameters;
Expand Down Expand Up @@ -97,15 +99,23 @@ public class MediaType extends MessageParser {
String type = string.substring(0, typeEnd);
String subtype = string.substring(typeEnd + 1, subtypeEnd);
Map<String,String> parameters = Collections.unmodifiableMap(map);
return new MediaType(type, subtype, parameters);
return new MediaType(string, type, subtype, parameters);
}

private MediaType(String type, String subtype, Map<String,String> parameters) {
private MediaType(String raw, String type, String subtype, Map<String,String> parameters) {
this.raw = raw;
this.type = type;
this.subtype = subtype;
this.parameters = parameters;
}

/**
* The original unparsed media type string.
*/
public String raw() {
return raw;
}

public String type() {
return type;
}
Expand Down Expand Up @@ -170,7 +180,7 @@ public class MediaType extends MessageParser {
* The base type and subtype without any parameters.
*/
public MediaType base() {
return new MediaType(type, subtype, Collections.emptyMap());
return new MediaType(null, type, subtype, Collections.emptyMap());
}

private static boolean validToken(String s) {
Expand Down

0 comments on commit bd907ce

Please sign in to comment.