Esempio n. 1
0
  /**
   * @param queryString a query string of the form n1=v1&n2=v2&... to decode. May be null.
   * @param acceptAmp -> "&" if true, "&" if false
   * @return a Map of String[] indexed by name, an empty Map if the query string was null
   */
  public static Map<String, String[]> decodeQueryString(
      final CharSequence queryString, final boolean acceptAmp) {

    final Map<String, String[]> result = new TreeMap<String, String[]>();
    if (queryString != null) {
      final Matcher matcher =
          acceptAmp ? PATTERN_AMP.matcher(queryString) : PATTERN_NO_AMP.matcher(queryString);
      int matcherEnd = 0;
      while (matcher.find()) {
        matcherEnd = matcher.end();
        try {
          // Group 0 is the whole match, e.g. a=b, while group 1 is the first group
          // denoted ( with parens ) in the expression.  Hence we start with group 1.
          final String name =
              URLDecoder.decode(matcher.group(1), NetUtils.STANDARD_PARAMETER_ENCODING);
          final String value =
              URLDecoder.decode(matcher.group(2), NetUtils.STANDARD_PARAMETER_ENCODING);

          StringUtils.addValueToStringArrayMap(result, name, value);
        } catch (UnsupportedEncodingException e) {
          // Should not happen as we are using a required encoding
          throw new OXFException(e);
        }
      }
      if (queryString.length() != matcherEnd) {
        // There was garbage at the end of the query.
        throw new OXFException("Malformed URL: " + queryString);
      }
    }
    return result;
  }
 private void writeSpecifiedTextInline(final CharSequence text, int depth) throws IOException {
   final int textLength = text.length();
   int i = writeSpecifiedLine(text, 0);
   if (i < textLength) {
     final int subsequentLineDepth = depth + 1;
     do {
       while (Segment.isWhiteSpace(text.charAt(i)))
         if (++i >= textLength) return; // trim whitespace.
       writeEssentialNewLine();
       writeIndent(subsequentLineDepth);
       i = writeSpecifiedLine(text, i);
     } while (i < textLength);
   }
 }
 private int writeSpecifiedLine(final CharSequence text, int i) throws IOException {
   // Writes the first line from the specified text starting from the specified position.
   // The line break characters are not written.
   // Returns the position following the first line break character(s), or text.length() if the
   // text contains no line breaks.
   final int textLength = text.length();
   while (true) {
     final char ch = text.charAt(i);
     if (ch == '\r') {
       final int nexti = i + 1;
       if (nexti < textLength && text.charAt(nexti) == '\n') return i + 2;
     }
     if (ch == '\n') return i + 1;
     writer.write(ch);
     if (++i >= textLength) return i;
   }
 }