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