999fd087b207db1643424771e87639bbc12abd1f
[phpeclipse.git] /
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz 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  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: XMLDocumentProvider.java,v 1.3 2006-10-21 23:14:14 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.editor;
15
16 import java.net.MalformedURLException;
17
18 import net.sourceforge.phpeclipse.xml.core.internal.model.XMLDocument;
19 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IDocumentListener;
24 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
25
26 /**
27  * Document provider for XML files.
28  * 
29  * TODO Merge the encoding detection support from I18NDocumentProvider and
30  * AbstractDocumentProvider into this class
31  * 
32  * TODO This class currently doubles as a model manager which will need to be
33  * moved into core at some point, and would make this class pretty much useless
34  */
35 public class XMLDocumentProvider extends TextFileDocumentProvider {
36
37         // Inner Classes -----------------------------------------------------------
38
39         private class XMLFileInfo extends FileInfo {
40                 IXMLDocument xmlDocument;
41         }
42
43         // TestFileDocumentProvider Implementation ---------------------------------
44
45         /*
46          * @see TextFileDocumentProvider#createEmptyFileInfo()
47          */
48         protected FileInfo createEmptyFileInfo() {
49                 return new XMLFileInfo();
50         }
51
52         /*
53          * @see TextFileDocumentProvider#createFileInfo(Object)
54          */
55         protected FileInfo createFileInfo(Object element) throws CoreException {
56                 FileInfo fileInfo = super.createFileInfo(element);
57                 if (!(fileInfo instanceof XMLFileInfo)) {
58                         return null;
59                 }
60
61                 IDocument document = fileInfo.fTextFileBuffer.getDocument();
62
63                 String systemId = null;
64                 try {
65                         systemId = getSystemFile(fileInfo).toURL().toString();
66                 } catch (MalformedURLException e) {
67                 }
68
69                 IXMLDocument xmlDocument = createModel(document, systemId);
70                 if (xmlDocument instanceof IDocumentListener) {
71                         document.addDocumentListener((IDocumentListener) xmlDocument);
72                 }
73
74                 XMLFileInfo xmlFileInfo = (XMLFileInfo) fileInfo;
75                 xmlFileInfo.xmlDocument = xmlDocument;
76
77                 return xmlFileInfo;
78         }
79
80         /*
81          * @see TextFileDocumentProvider#disposeFileInfo(Object,
82          *      TextFileDocumentProvider.FileInfo)
83          */
84         protected void disposeFileInfo(Object element, FileInfo info) {
85                 if (info instanceof XMLFileInfo) {
86                         IDocument document = getDocument(element);
87                         if (document != null) {
88                                 IXMLDocument xmlDocument = ((XMLFileInfo) info).xmlDocument;
89                                 if (xmlDocument instanceof IDocumentListener) {
90                                         document
91                                                         .removeDocumentListener((IDocumentListener) xmlDocument);
92                                 }
93                         }
94                 }
95
96                 super.disposeFileInfo(element, info);
97         }
98
99         // Public Methods ----------------------------------------------------------
100
101         /**
102          * Creates the XML document model object corresponding to the specified
103          * document.
104          * 
105          * @param document
106          *            the document to parse
107          * @param systemId
108          *            the system ID of the document
109          * @return the document model object
110          */
111         public IXMLDocument createModel(IDocument document, String systemId) {
112                 return new XMLDocument(document, systemId);
113         }
114
115         /**
116          * Returns the XML document model associated with the specified element.
117          * 
118          * @param element
119          *            the element
120          * @return the document model associated with the element
121          */
122         public IXMLDocument getModel(Object element) {
123                 FileInfo info = getFileInfo(element);
124                 if (info instanceof XMLFileInfo) {
125                         XMLFileInfo xmlFileInfo = (XMLFileInfo) info;
126                         return xmlFileInfo.xmlDocument;
127                 }
128
129                 return null;
130         }
131 }