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;
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.transform.OutputKeys;
18 import javax.xml.transform.Result;
19 import javax.xml.transform.Source;
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerConfigurationException;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
27 import net.sourceforge.phpdt.monitor.core.MonitorCore;
28 import net.sourceforge.phpdt.monitor.ui.IContentViewer;
29 import net.sourceforge.phpdt.monitor.ui.internal.ContextIds;
31 import org.eclipse.jface.resource.JFaceResources;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.custom.StackLayout;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.help.WorkbenchHelp;
48 public class XMLViewer implements IContentViewer {
49 protected GridData data;
50 protected StackLayout layout;
51 protected Text messageText;
53 protected Composite rootComp;
54 protected Composite viewerComp;
55 protected Label messageLabel;
57 protected boolean xmlTagMissing = false;
58 protected boolean setEncoding = false;
59 protected boolean missingEncoding = false;
60 protected String originalEncoding;
63 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#dispose()
65 public void dispose() {
70 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#setContent()
72 public void setContent(byte[] b) {
75 out = MonitorCore.parse(b);
77 String lineSeparator = System.getProperty("line.separator");
78 int ls = lineSeparator.length();
79 if (out.length() > ls) {
80 while (out.substring(0, ls).indexOf(lineSeparator) >= 0)
81 out = out.substring(ls, out.length());
84 String out_temp = out.toLowerCase();
85 if (out_temp.indexOf("<?xml") < 0)
88 if (out.length() > 0) {
89 byte[] b1 = createDocument(out);
90 String finalMsg = new String (b1);
91 if (finalMsg.startsWith("Invalid XML")) {
93 messageText.setVisible(false);
94 layout.topControl = messageLabel;
95 messageLabel.setVisible(true);
96 messageLabel.setText("<Not a valid XML file>");
99 else if (xmlTagMissing && finalMsg.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
100 int x = finalMsg.indexOf("\n") + 1;
101 String Msg = finalMsg.substring(x);
104 messageText.setText(finalMsg);
107 else if (setEncoding) {
108 //change back to original Encoding
109 int begin = finalMsg.indexOf("UTF-8"); //location of opening "
110 int last = begin + 5; //location of closing "
111 String first_half = finalMsg.substring(0,begin);
112 String second_half = finalMsg.substring(last);
113 finalMsg = first_half + originalEncoding + second_half;
115 messageText.setText(finalMsg);
118 else if (missingEncoding) {
119 //remove encoding completely
120 int begin = finalMsg.indexOf("encoding=\"UTF-8\""); //location of opening "
121 int last = begin + 16; //location of closing "
122 String first_half = finalMsg.substring(0,begin);
123 String second_half = finalMsg.substring(last);
124 finalMsg = first_half + second_half;
126 messageText.setText(finalMsg);
129 messageText.setText(out);
133 * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#init(Composite)
135 public void init(Composite parent) {
138 viewerComp = new Composite(parent, SWT.NONE);
139 layout = new StackLayout();
140 layout.marginHeight = 0;
141 layout.marginWidth = 0;
142 viewerComp.setLayout(layout);
143 data = new GridData(GridData.FILL_BOTH);
144 viewerComp.setLayoutData(data);
146 messageText = new Text(viewerComp, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
147 Display display = viewerComp.getDisplay();
148 messageText.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
149 messageText.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
150 messageText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
151 messageText.setFont(JFaceResources.getTextFont());
152 messageText.setVisible(true);
153 WorkbenchHelp.setHelp(messageText, ContextIds.VIEW_RESPONSE);
155 messageLabel = new Label(viewerComp, SWT.NONE);
156 messageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
157 messageLabel.setVisible(false);
159 layout.topControl = messageText;
163 * @#createDocument(String)
165 protected byte[] createDocument(String str) {
166 byte[] parseArray = null;
167 Document document = null;
168 byte[] result = null;
171 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
173 factory.setAttribute("http://apache.org/xml/features/allow-java-encodings", new Boolean(true));
174 factory.setAttribute("http://apache.org/xml/features/continue-after-fatal-error", new Boolean(true));
175 } catch (Exception e) { }
176 DocumentBuilder parser = factory.newDocumentBuilder();
179 str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + str;
181 String str_temp = str.toLowerCase();
183 //if encoding present,then save original Encoding, change to UTF-8
184 if (str_temp.indexOf("encoding=") >= 0) {
186 String temp1 = str.substring(str_temp.indexOf("encoding="));
187 int beginIndex = temp1.indexOf("\"") + 1;
188 String temp2 = temp1.substring(beginIndex);
189 int endIndex = temp2.indexOf("\"");
190 originalEncoding = temp2.substring(0, endIndex);
193 //if no encoding at all,then no changes to be made
194 else if (str_temp.indexOf("encoding") < 0) {
196 missingEncoding = true;
199 parseArray = str.getBytes();
200 document = parser.parse(new InputSource(new ByteArrayInputStream(parseArray)));
201 result = getContents(document);
203 } catch (Exception e) {
204 result = "Invalid XML".getBytes();
209 protected byte[] getContents(Document document) throws IOException {
210 ByteArrayOutputStream out = new ByteArrayOutputStream();
211 Result result = new StreamResult(out);
212 Source source = new DOMSource(document);
214 Transformer transformer = TransformerFactory.newInstance().newTransformer();
215 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
216 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
217 transformer.transform(source, result);
219 catch (TransformerConfigurationException e) {
220 throw (IOException) (new IOException().initCause(e));
222 catch (TransformerException e) {
223 throw (IOException) (new IOException().initCause(e));
225 return out.toByteArray();