1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.viewers;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.transform.OutputKeys;
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Source;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerConfigurationException;
24 import javax.xml.transform.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
29 import net.sourceforge.phpdt.monitor.core.MonitorCore;
30 import net.sourceforge.phpdt.monitor.ui.IContentViewer;
31 import net.sourceforge.phpdt.monitor.ui.internal.ContextIds;
33 import org.eclipse.jface.resource.JFaceResources;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.StackLayout;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.help.WorkbenchHelp;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
47 public class XMLViewer implements IContentViewer {
48 protected GridData data;
49 protected StackLayout layout;
50 protected Text messageText;
52 protected Composite rootComp;
53 protected Composite viewerComp;
54 protected Label messageLabel;
56 protected boolean xmlTagMissing = false;
57 protected boolean setEncoding = false;
58 protected boolean missingEncoding = false;
59 protected String originalEncoding;
62 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#dispose()
64 public void dispose() {
69 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#setContent()
71 public void setContent(byte[] b) {
74 out = MonitorCore.parse(b);
76 String lineSeparator = System.getProperty("line.separator");
77 int ls = lineSeparator.length();
78 if (out.length() > ls) {
79 while (out.substring(0, ls).indexOf(lineSeparator) >= 0)
80 out = out.substring(ls, out.length());
83 String out_temp = out.toLowerCase();
84 if (out_temp.indexOf("<?xml") < 0)
87 if (out.length() > 0) {
88 byte[] b1 = createDocument(out);
89 String finalMsg = new String (b1);
90 if (finalMsg.startsWith("Invalid XML")) {
92 messageText.setVisible(false);
93 layout.topControl = messageLabel;
94 messageLabel.setVisible(true);
95 messageLabel.setText("<Not a valid XML file>");
98 else if (xmlTagMissing && finalMsg.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
99 int x = finalMsg.indexOf("\n") + 1;
100 String Msg = finalMsg.substring(x);
103 messageText.setText(finalMsg);
106 else if (setEncoding) {
107 //change back to original Encoding
108 int begin = finalMsg.indexOf("UTF-8"); //location of opening "
109 int last = begin + 5; //location of closing "
110 String first_half = finalMsg.substring(0,begin);
111 String second_half = finalMsg.substring(last);
112 finalMsg = first_half + originalEncoding + second_half;
114 messageText.setText(finalMsg);
117 else if (missingEncoding) {
118 //remove encoding completely
119 int begin = finalMsg.indexOf("encoding=\"UTF-8\""); //location of opening "
120 int last = begin + 16; //location of closing "
121 String first_half = finalMsg.substring(0,begin);
122 String second_half = finalMsg.substring(last);
123 finalMsg = first_half + second_half;
125 messageText.setText(finalMsg);
128 messageText.setText(out);
132 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#init(Composite)
134 public void init(Composite parent) {
137 viewerComp = new Composite(parent, SWT.NONE);
138 layout = new StackLayout();
139 layout.marginHeight = 0;
140 layout.marginWidth = 0;
141 viewerComp.setLayout(layout);
142 data = new GridData(GridData.FILL_BOTH);
143 viewerComp.setLayoutData(data);
145 messageText = new Text(viewerComp, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
146 Display display = viewerComp.getDisplay();
147 messageText.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
148 messageText.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
149 messageText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
150 messageText.setFont(JFaceResources.getTextFont());
151 messageText.setVisible(true);
152 WorkbenchHelp.setHelp(messageText, ContextIds.VIEW_RESPONSE);
154 messageLabel = new Label(viewerComp, SWT.NONE);
155 messageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
156 messageLabel.setVisible(false);
158 layout.topControl = messageText;
162 * @#createDocument(String)
164 protected byte[] createDocument(String str) {
165 byte[] parseArray = null;
166 Document document = null;
167 byte[] result = null;
170 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
172 factory.setAttribute("http://apache.org/xml/features/allow-java-encodings", new Boolean(true));
173 factory.setAttribute("http://apache.org/xml/features/continue-after-fatal-error", new Boolean(true));
174 } catch (Exception e) { }
175 DocumentBuilder parser = factory.newDocumentBuilder();
178 str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + str;
180 String str_temp = str.toLowerCase();
182 //if encoding present,then save original Encoding, change to UTF-8
183 if (str_temp.indexOf("encoding=") >= 0) {
185 String temp1 = str.substring(str_temp.indexOf("encoding="));
186 int beginIndex = temp1.indexOf("\"") + 1;
187 String temp2 = temp1.substring(beginIndex);
188 int endIndex = temp2.indexOf("\"");
189 originalEncoding = temp2.substring(0, endIndex);
192 //if no encoding at all,then no changes to be made
193 else if (str_temp.indexOf("encoding") < 0) {
195 missingEncoding = true;
198 parseArray = str.getBytes();
199 document = parser.parse(new InputSource(new ByteArrayInputStream(parseArray)));
200 result = getContents(document);
202 } catch (Exception e) {
203 result = "Invalid XML".getBytes();
208 protected byte[] getContents(Document document) throws IOException {
209 ByteArrayOutputStream out = new ByteArrayOutputStream();
210 Result result = new StreamResult(out);
211 Source source = new DOMSource(document);
213 Transformer transformer = TransformerFactory.newInstance().newTransformer();
214 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
215 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
216 transformer.transform(source, result);
218 catch (TransformerConfigurationException e) {
219 throw (IOException) (new IOException().initCause(e));
221 catch (TransformerException e) {
222 throw (IOException) (new IOException().initCause(e));
224 return out.toByteArray();