Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / viewers / XMLViewer.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.viewers;
12  
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16
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;
28
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;
32
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;
44 /**
45  * XML Viewer.
46  */
47 public class XMLViewer implements IContentViewer {
48         protected GridData data;
49         protected StackLayout layout;
50         protected Text messageText;
51
52         protected Composite rootComp;
53         protected Composite viewerComp;
54         protected Label messageLabel;
55         
56         protected boolean xmlTagMissing = false;
57         protected boolean setEncoding = false;
58         protected boolean missingEncoding = false;
59         protected String originalEncoding;
60
61         /* (non-Javadoc)
62          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#dispose()
63          */
64         public void dispose() {
65                 viewerComp.dispose();
66         }
67         
68         /* (non-Javadoc)
69          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#setContent()
70          */
71         public void setContent(byte[] b) {
72                 String out = "";                
73                 if (b != null)
74                         out = MonitorCore.parse(b);
75                                 
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()); 
81                 }
82                 
83                 String out_temp = out.toLowerCase();
84                 if (out_temp.indexOf("<?xml") < 0) 
85                         xmlTagMissing = true;
86                 
87                 if (out.length() > 0) {         
88                         byte[] b1 = createDocument(out);
89                         String finalMsg = new String (b1);
90                         if (finalMsg.startsWith("Invalid XML")) {
91                                 //case: error parsing
92                                 messageText.setVisible(false);
93                                 layout.topControl = messageLabel;
94                                 messageLabel.setVisible(true);
95                                 messageLabel.setText("<Not a valid XML file>");
96                                 
97                         }
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);
101                                 finalMsg = Msg;
102                                 
103                                 messageText.setText(finalMsg);
104                         }
105                         
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; 
113                                 
114                                 messageText.setText(finalMsg);
115                         }
116                         
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;
124                                 
125                                 messageText.setText(finalMsg);  
126                         }                       
127                 } else
128                         messageText.setText(out);
129         }
130         
131         /* (non-Javadoc)
132          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#init(Composite)
133          */
134         public void init(Composite parent) {
135                 rootComp = parent;
136                 
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);
144         
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);
153                 
154                 messageLabel = new Label(viewerComp, SWT.NONE);
155                 messageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
156                 messageLabel.setVisible(false);
157                 
158                 layout.topControl = messageText;
159         }
160         
161         /* (non-Javadoc)
162          * @#createDocument(String)
163          */
164         protected byte[] createDocument(String str) {
165                 byte[] parseArray = null;
166                 Document document = null;
167                 byte[] result = null;   
168                 
169                 try {   
170                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
171                         try {
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();
176                         
177                         if (xmlTagMissing) {
178                                 str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + str;
179                         } else {
180                                 String str_temp = str.toLowerCase();
181                                 
182                                 //if encoding present,then save original Encoding, change to UTF-8
183                                 if (str_temp.indexOf("encoding=") >= 0) {
184                                         setEncoding = true;
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);
190                                 }       
191                                 
192                                 //if no encoding at all,then no changes to be made
193                                 else if (str_temp.indexOf("encoding") < 0) {
194                                         setEncoding = false;    
195                                         missingEncoding = true;         
196                                 }       
197                         }
198                         parseArray = str.getBytes();
199                         document = parser.parse(new InputSource(new ByteArrayInputStream(parseArray)));  
200                         result = getContents(document);
201                         
202                 } catch (Exception e) {
203                         result = "Invalid XML".getBytes();
204                 } 
205                 return result;
206         }
207
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);
212                 try {
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);            
217                 }
218                 catch (TransformerConfigurationException e) {
219                         throw (IOException) (new IOException().initCause(e));
220                 }
221                 catch (TransformerException e) {
222                         throw (IOException) (new IOException().initCause(e));
223                 }
224                 return out.toByteArray();
225         }
226 }