~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/response/PHPResponseWriter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.response;
19
 
 
20
 
import java.io.Writer;
21
 
import java.io.IOException;
22
 
 
23
 
import org.apache.solr.common.util.NamedList;
24
 
import org.apache.solr.request.SolrQueryRequest;
25
 
 
26
 
public class PHPResponseWriter implements QueryResponseWriter {
27
 
  static String CONTENT_TYPE_PHP_UTF8="text/x-php;charset=UTF-8";
28
 
 
29
 
  public void init(NamedList n) {
30
 
    /* NOOP */
31
 
  }
32
 
  
33
 
 public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
34
 
    PHPWriter w = new PHPWriter(writer, req, rsp);
35
 
    try {
36
 
      w.writeResponse();
37
 
    } finally {
38
 
      w.close();
39
 
    }
40
 
  }
41
 
 
42
 
  public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
43
 
    return CONTENT_TYPE_TEXT_UTF8;
44
 
  }
45
 
}
46
 
 
47
 
class PHPWriter extends JSONWriter {
48
 
  public PHPWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) {
49
 
    super(writer, req, rsp);
50
 
  }
51
 
  
52
 
  @Override
53
 
  public void writeNamedList(String name, NamedList val) throws IOException {
54
 
    writeNamedListAsMapMangled(name,val);
55
 
  }
56
 
 
57
 
  @Override
58
 
  public void writeMapOpener(int size) throws IOException {
59
 
    writer.write("array(");
60
 
  }
61
 
 
62
 
  @Override
63
 
  public void writeMapCloser() throws IOException {
64
 
    writer.write(')');
65
 
  }
66
 
 
67
 
  @Override
68
 
  public void writeArrayOpener(int size) throws IOException {
69
 
    writer.write("array(");
70
 
  }
71
 
 
72
 
  @Override
73
 
  public void writeArrayCloser() throws IOException {
74
 
    writer.write(')');
75
 
  }
76
 
 
77
 
  @Override
78
 
  public void writeNull(String name) throws IOException {
79
 
    writer.write("null");
80
 
  }
81
 
 
82
 
  @Override
83
 
  protected void writeKey(String fname, boolean needsEscaping) throws IOException {
84
 
    writeStr(null, fname, needsEscaping);
85
 
    writer.write('=');
86
 
    writer.write('>');
87
 
  }
88
 
 
89
 
  @Override
90
 
  public void writeStr(String name, String val, boolean needsEscaping) throws IOException {
91
 
    if (needsEscaping) {
92
 
      writer.write('\'');
93
 
      for (int i=0; i<val.length(); i++) {
94
 
        char ch = val.charAt(i);
95
 
        switch (ch) {
96
 
          case '\'':
97
 
          case '\\': writer.write('\\'); writer.write(ch); break;
98
 
          default:
99
 
            writer.write(ch);
100
 
        }
101
 
      }
102
 
      writer.write('\'');
103
 
    } else {
104
 
      writer.write('\'');
105
 
      writer.write(val);
106
 
      writer.write('\'');
107
 
    }
108
 
  }
109
 
}