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