BrightSide Workbench Full Report + Source Code
SerializerMessage.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.jpa.serializer;
19 
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import org.jdom.Document;
24 import org.jdom.JDOMException;
25 import org.jdom.input.SAXBuilder;
26 import org.jdom.output.Format;
27 import org.jdom.output.XMLOutputter;
28 import org.turro.jpa.entity.XMLSerializer;
29 import org.turro.json.JsonSearch;
30 import org.turro.log.WebLoggers;
31 import org.turro.zkoss.dialog.Windows;
32 import org.turro.zkoss.label.PreLabel;
33 import org.zkoss.zk.ui.Page;
34 
39 public class SerializerMessage {
40 
41  private Object entity;
42  private String data;
43 
44  public SerializerMessage(Object entity) {
45  this.entity = entity;
46  }
47 
48  public SerializerMessage(String data) {
49  this.data = data;
50  }
51 
52  public void show(Page page) {
53  try {
54  data = entity == null ? data : new XMLSerializer(entity).serialize();
55  try {
56  if(data.startsWith("<")) {
57  SAXBuilder builder = new SAXBuilder();
58  Document doc = builder.build(new StringReader(data.replaceAll("&(?![a-z]+;)", "&amp;")));
59  StringWriter sw = new StringWriter();
60  Format fm = Format.getPrettyFormat();
61  XMLOutputter xo = new XMLOutputter(fm);
62  xo.output(doc, sw);
63  sw.close();
64  Windows.title("XML Entity")
65  .addComponent(new PreLabel(sw.toString()))
66  .width("70%")
67  .height("50%")
68  .sizeable()
69  .scrollable()
70  .closeable()
71  .show();
72  } else if(data.startsWith("{")) {
73  Windows.title("JSON Entity")
74  .addComponent(new PreLabel(JsonSearch.of(data).pretty()))
75  .width("70%")
76  .height("50%")
77  .sizeable()
78  .scrollable()
79  .closeable()
80  .show();
81  }
82  } catch (Exception ex) {
83  Windows.title("String Entity")
84  .addComponent(new PreLabel(data))
85  .width("70%")
86  .height("50%")
87  .sizeable()
88  .scrollable()
89  .closeable()
90  .show();
91  }
92  } catch(Exception ex) {
93  WebLoggers.severe(this).exception(ex).log();
94  }
95 
96  }
97 
98 }
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static Windows title(String title)
Definition: Windows.java:138
Windows addComponent(HtmlBasedComponent component)
Definition: Windows.java:97
Windows width(String width)
Definition: Windows.java:61
Windows height(String height)
Definition: Windows.java:66