private String read(int next) { String result = remainder().substring(0, next); parseIndex += next; return result; }
private int nextSemiOrComma() throws MalformedURLException { int semi = remainder().indexOf(';'); int comma = remainder().indexOf(','); if (semi<0 && comma<0) throw new MalformedURLException("Missing required ',' in data: URL"); if (semi<0) return comma; if (comma<0) return semi; return Math.min(semi, comma); }
private boolean skipOptional(String word) { if (remainder().startsWith(word)) { parseIndex += word.length(); return true; } return false; }
private void skipRequired(String word) throws MalformedURLException { if (!remainder().startsWith(word)) throw new MalformedURLException("Missing required '"+word+"' at position "+parseIndex+" of data: URL"); parseIndex += word.length(); }
private int nextUnescapedQuote() throws MalformedURLException { int i=0; String r = remainder(); boolean escaped = false; while (i<r.length()) { if (escaped) { escaped = false; } else { if (r.charAt(i)=='"') return i; if (r.charAt(i)=='\\') escaped = true; } i++; } throw new MalformedURLException("Unclosed double-quote in data: URL"); }
private void parseMediaType() throws MalformedURLException { if (remainder().startsWith(";") || remainder().startsWith(",")) return; int slash = remainder().indexOf("/"); if (slash==-1) throw new MalformedURLException("Missing required '/' in MIME type of data: URL"); String type = read(slash); skipRequired("/"); int next = nextSemiOrComma(); String subtype = read(next); mimeType = type+"/"+subtype; }
private void parseData() throws UnsupportedEncodingException, MalformedURLException { if (parameters.containsKey("base64")) { String base64value = parameters.get("base64"); if (base64value!=null) throw new MalformedURLException("base64 parameter must not take a value ("+base64value+") in data: URL"); data = Base64.decode(remainder()); } else { data = URLDecoder.decode(remainder(), getCharset()).getBytes(Charset.forName(getCharset())); } }
private void parseParameterOrParameterValues() throws MalformedURLException { while (true) { if (!remainder().startsWith(";")) return; parseIndex++; int eq = remainder().indexOf('='); String word, value; int nextSemiOrComma = nextSemiOrComma(); if (eq==-1 || eq>nextSemiOrComma) { word = read(nextSemiOrComma); value = null; } else { word = read(eq); if (remainder().startsWith("\"")) { // is quoted parseIndex++; int nextUnescapedQuote = nextUnescapedQuote(); value = "\"" + read(nextUnescapedQuote); } else { value = read(nextSemiOrComma()); } } parameters.put(word, value); } }
public synchronized DataUriSchemeParser parseChecked() throws UnsupportedEncodingException, MalformedURLException { if (isParsed) return this; skipOptional(PROTOCOL_PREFIX); if (allowSlashesAfterColon) while (skipOptional("/")) ; if (allowMissingComma && remainder().indexOf(',')==-1) { mimeType = DEFAULT_MIME_TYPE; parameters.put("charset", DEFAULT_CHARSET); } else { parseMediaType(); parseParameterOrParameterValues(); skipRequired(","); } parseData(); isParsed = true; return this; }