View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.presentation;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import olr.rdf.Statement;
24  import olr.vrp.Parser;
25  
26  import org.w3c.dom.html.HTMLTableElement;
27  import org.w3c.dom.html.HTMLTableRowElement;
28  
29  import VRP.Model.Literal;
30  import VRP.Model.Model;
31  import VRP.Model.Triple;
32  
33  import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
34  
35  /***
36   * @version $Id: Import.java,v 1.7 2004/08/02 19:26:39 roku Exp $
37   */
38  public class Import extends SecureHttpPresentation {
39      /*** 
40       * Initializes an instance of this class.
41       */
42      public Import() {
43      }
44  
45      public String handleDefault() throws HttpPresentationException {
46          return showPage();
47      }
48  
49      public String handleImport() throws HttpPresentationException {
50          String URI = getComms().request.getParameter("url");
51          if (URI != null) {
52              Parser parser = null;
53              try {
54                  parser = new Parser(false, false);
55                  Model m = parser.parse(URI);
56                  Triple triple;
57                  for (Iterator it = m.getStatements().iterator(); it.hasNext(); getSessionData()
58                          .getStatementPool().isImportedResource(triple.getSubject())) {
59                      triple = (Triple) it.next();
60                      Object object = triple.getObject();
61                      Statement statement = new Statement(triple.getSubject(), triple.getPredicate(),
62                              (object instanceof Literal) ? ((Literal) object).getvalue() : object
63                                      .toString());
64                      getSessionData().getStatementPool().addStatement(statement);
65                  }
66  
67                  String s = showPage(String.valueOf(String.valueOf(m.getStatements().size()))
68                          .concat(" statements sucessfully imported"), URI);
69                  return s;
70              } catch (Exception e) {
71                  final List messages = parser != null ? parser.getMessages() : new ArrayList();
72                  messages.add(e.getMessage());
73                  String s1 = showPage(messages, URI);
74                  return s1;
75              }
76          } else {
77              return showPage();
78          }
79      }
80  
81      public String showPage() {
82          return showPage("", null);
83      }
84  
85      public String showPage(String okText, String uri) {
86          ImportHTML page = new ImportHTML();
87          if (uri != null)
88              page.getElementUrl().setValue(uri);
89          if (okText != null)
90              page.setTextOkText(okText);
91          return page.toDocument();
92      }
93  
94      public String showPage(List messages, String uri) {
95          final ImportHTML page = new ImportHTML();
96          if (uri != null)
97              page.getElementUrl().setValue(uri);
98          try {
99              HTMLTableElement errorTable = page.getElementErrorTable();
100             HTMLTableRowElement errorRow = page.getElementErrorRow();
101             if (messages != null) {
102                 Iterator it = messages.iterator();
103                 do {
104                     if (!it.hasNext())
105                         break;
106                     String line = (String) it.next();
107                     if (line != null) {
108                         page.setTextErrorText(line);
109                         errorTable.appendChild(errorRow.cloneNode(true));
110                     }
111                 } while (true);
112             }
113             errorRow.getParentNode().removeChild(errorRow);
114         } catch (Exception e) {
115             e.printStackTrace();
116         }
117         return page.toDocument();
118     }
119 }