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.1 2004-09-02 18:28:04 jsurfer Exp $
14 package net.sourceforge.phpeclipse.xml.ui.internal.editor;
16 import java.net.MalformedURLException;
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;
23 import net.sourceforge.phpeclipse.xml.core.internal.model.XMLDocument;
24 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
28 * Document provider for XML files.
30 * TODO Merge the encoding detection support from I18NDocumentProvider and
31 * AbstractDocumentProvider into this class
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
37 public class XMLDocumentProvider extends TextFileDocumentProvider {
39 // Inner Classes -----------------------------------------------------------
41 private class XMLFileInfo extends FileInfo {
42 IXMLDocument xmlDocument;
45 // TestFileDocumentProvider Implementation ---------------------------------
48 * @see TextFileDocumentProvider#createEmptyFileInfo()
50 protected FileInfo createEmptyFileInfo() {
51 return new XMLFileInfo();
55 * @see TextFileDocumentProvider#createFileInfo(Object)
57 protected FileInfo createFileInfo(Object element) throws CoreException {
58 FileInfo fileInfo = super.createFileInfo(element);
59 if (!(fileInfo instanceof XMLFileInfo)) {
63 IDocument document = fileInfo.fTextFileBuffer.getDocument();
65 String systemId = null;
67 systemId = getSystemFile(fileInfo).toURL().toString();
68 } catch (MalformedURLException e) {
71 IXMLDocument xmlDocument = createModel(document, systemId);
72 if (xmlDocument instanceof IDocumentListener) {
73 document.addDocumentListener((IDocumentListener) xmlDocument);
76 XMLFileInfo xmlFileInfo = (XMLFileInfo) fileInfo;
77 xmlFileInfo.xmlDocument = xmlDocument;
83 * @see TextFileDocumentProvider#disposeFileInfo(Object, TextFileDocumentProvider.FileInfo)
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);
97 super.disposeFileInfo(element, info);
100 // Public Methods ----------------------------------------------------------
103 * Creates the XML document model object corresponding to the specified
106 * @param document the document to parse
107 * @param systemId the system ID of the document
108 * @return the document model object
110 public IXMLDocument createModel(IDocument document, String systemId) {
111 return new XMLDocument(document, systemId);
115 * Returns the XML document model associated with the specified element.
117 * @param element the element
118 * @return the document model associated with the element
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;