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
9 * Christopher Lenz - initial API and implementation
11 * $Id: XMLDocumentProvider.java,v 1.3 2006-10-21 23:14:14 pombredanne Exp $
14 package net.sourceforge.phpeclipse.xml.ui.internal.editor;
16 import java.net.MalformedURLException;
18 import net.sourceforge.phpeclipse.xml.core.internal.model.XMLDocument;
19 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
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;
27 * Document provider for XML files.
29 * TODO Merge the encoding detection support from I18NDocumentProvider and
30 * AbstractDocumentProvider into this class
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
35 public class XMLDocumentProvider extends TextFileDocumentProvider {
37 // Inner Classes -----------------------------------------------------------
39 private class XMLFileInfo extends FileInfo {
40 IXMLDocument xmlDocument;
43 // TestFileDocumentProvider Implementation ---------------------------------
46 * @see TextFileDocumentProvider#createEmptyFileInfo()
48 protected FileInfo createEmptyFileInfo() {
49 return new XMLFileInfo();
53 * @see TextFileDocumentProvider#createFileInfo(Object)
55 protected FileInfo createFileInfo(Object element) throws CoreException {
56 FileInfo fileInfo = super.createFileInfo(element);
57 if (!(fileInfo instanceof XMLFileInfo)) {
61 IDocument document = fileInfo.fTextFileBuffer.getDocument();
63 String systemId = null;
65 systemId = getSystemFile(fileInfo).toURL().toString();
66 } catch (MalformedURLException e) {
69 IXMLDocument xmlDocument = createModel(document, systemId);
70 if (xmlDocument instanceof IDocumentListener) {
71 document.addDocumentListener((IDocumentListener) xmlDocument);
74 XMLFileInfo xmlFileInfo = (XMLFileInfo) fileInfo;
75 xmlFileInfo.xmlDocument = xmlDocument;
81 * @see TextFileDocumentProvider#disposeFileInfo(Object,
82 * TextFileDocumentProvider.FileInfo)
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) {
91 .removeDocumentListener((IDocumentListener) xmlDocument);
96 super.disposeFileInfo(element, info);
99 // Public Methods ----------------------------------------------------------
102 * Creates the XML document model object corresponding to the specified
106 * the document to parse
108 * the system ID of the document
109 * @return the document model object
111 public IXMLDocument createModel(IDocument document, String systemId) {
112 return new XMLDocument(document, systemId);
116 * Returns the XML document model associated with the specified element.
120 * @return the document model associated with the element
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;