root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/www/CLServlet.java @ 197

リビジョン 197, 5.0 KB (コミッタ: atsuko, 10 年 前)

CLServlet がラベル+インスタンス数を出力するように変更

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.www;
8
9import java.io.IOException;
10import java.io.PrintWriter;
11import java.math.BigDecimal;
12//import java.math.BigDecimal;
13import javax.json.*;
14import javax.servlet.ServletException;
15import javax.servlet.annotation.WebServlet;
16import javax.servlet.http.HttpServlet;
17import javax.servlet.http.HttpServletRequest;
18import javax.servlet.http.HttpServletResponse;
19import javax.servlet.http.HttpSession;
20import org.biohackathon.SPARQLBuilder.OWL.*;
21
22/**
23 *
24 * @author atsuko
25 */
26@WebServlet(name = "CLServlet", urlPatterns = {"/clist"})
27public class CLServlet extends HttpServlet {
28
29    private static final String FILENAME = "cdata/";
30   
31    /**
32     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
33     * methods.
34     *
35     * @param request servlet request
36     * @param response servlet response
37     * @throws ServletException if a servlet-specific error occurs
38     * @throws IOException if an I/O error occurs
39     */
40    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
41            throws ServletException, IOException {
42        response.setContentType("text/html;charset=UTF-8");
43        try (PrintWriter out = response.getWriter()) {
44            /* TODO output your page here. You may use following sample code. */
45            out.println("<!DOCTYPE html>");
46            out.println("<html>");
47            out.println("<head>");
48            out.println("<title>Servlet CLServlet</title>");           
49            out.println("</head>");
50            out.println("<body>");
51            out.println("<h1>Servlet CLServlet at " + request.getContextPath() + "</h1>");
52            out.println("</body>");
53            out.println("</html>");
54        }
55    }
56
57    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
58    /**
59     * Handles the HTTP <code>GET</code> method.
60     *
61     * @param request servlet request
62     * @param response servlet response
63     * @throws ServletException if a servlet-specific error occurs
64     * @throws IOException if an I/O error occurs
65     */
66    @Override
67    protected void doGet(HttpServletRequest request, HttpServletResponse response)
68            throws ServletException, IOException {
69        //processRequest(request, response);
70        response.setContentType("application/json;charset=UTF-8");
71        PrintWriter out = response.getWriter();
72        String ep = request.getParameter("ep");
73        HttpSession session = request.getSession();
74        QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg");
75        if ( qpg == null ){
76        //QueryPathGenerator
77                   qpg = new QueryPathGenerator();
78        }
79        qpg.setSPARQLendpoint(ep);
80        SClass[] classes = qpg.getClasses(null);
81        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
82        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
83        for (int i = 0 ; i < classes.length; i++ ){
84            JsonObjectBuilder job = jbfactory.createObjectBuilder();
85            String uri = classes[i].getClassURI();
86            job.add("uri", uri);
87            Label[] labels = classes[i].getLabels();
88            String label = null;
89            for ( int j = 0 ; j < labels.length; j++ ){
90                if ( labels[j].getLanguage() == null ){
91                    label = labels[j].getLabel(); break;
92                }else if ( labels[j].getLanguage().equals("en") ){
93                    label = labels[j].getLabel(); break;
94                }
95            }
96            if ( label == null ){
97                String[] uris = uri.split("/");
98                String tmplabel = uris[uris.length-1];
99                String[] tmplabel2 = tmplabel.split("#");
100                label = tmplabel2[tmplabel2.length-1];
101            }
102           
103            StringBuilder displaybuilder = new StringBuilder(label);
104            displaybuilder.append(" (");
105            displaybuilder.append(classes[i].getNumOfInstances());
106            displaybuilder.append(")");
107            job.add("display", displaybuilder.toString());
108            jab.add(job);
109        }
110        JsonArray ja = jab.build();
111        out.print(ja);
112    }
113
114    /**
115     * Handles the HTTP <code>POST</code> method.
116     *
117     * @param request servlet request
118     * @param response servlet response
119     * @throws ServletException if a servlet-specific error occurs
120     * @throws IOException if an I/O error occurs
121     */
122    @Override
123    protected void doPost(HttpServletRequest request, HttpServletResponse response)
124            throws ServletException, IOException {
125        processRequest(request, response);
126    }
127
128    /**
129     * Returns a short description of the servlet.
130     *
131     * @return a String containing servlet description
132     */
133    @Override
134    public String getServletInfo() {
135        return "Short description";
136    }// </editor-fold>
137
138}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。