intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.core / src / net / sourceforge / phpeclipse / xml / core / internal / model / XMLDocument.java
1 /*
2  * Copyright (c) 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
10  * 
11  * $Id: XMLDocument.java,v 1.1 2004-09-02 18:26:55 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.core.internal.model;
15
16 import net.sourceforge.phpeclipse.core.model.SourceReference;
17 import net.sourceforge.phpeclipse.xml.core.internal.parser.XMLParser;
18 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
19 import net.sourceforge.phpeclipse.xml.core.model.IXMLElement;
20 import net.sourceforge.phpeclipse.xml.core.parser.IProblemCollector;
21 import net.sourceforge.phpeclipse.xml.core.parser.IXMLParser;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.jface.text.DocumentEvent;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IDocumentListener;
27
28 /**
29  *  
30  */
31 public class XMLDocument extends SourceReference implements IXMLDocument, IDocumentListener {
32   // Instance Variables ------------------------------------------------------
33
34   private IXMLElement root;
35
36   private String systemId;
37
38   private Object dirtyLock = new Object();
39
40   private boolean dirty = true;
41
42   // Constructors ------------------------------------------------------------
43
44   public XMLDocument(IDocument document, String systemId) {
45     super(document, 0, document.getLength());
46     this.systemId = systemId;
47   }
48
49   // IXMLDocument Implementation ---------------------------------------------
50
51   /*
52    * @see IXMLDocument#getRoot()
53    */
54   public IXMLElement getRoot() {
55     return root;
56   }
57
58   /*
59    * @see net.sourceforge.phpeclipse.xml.core.model.IXMLDocument#getSystemId()
60    */
61   public String getSystemId() {
62     return systemId;
63   }
64
65   /*
66    * @see IStyleSheet#reconcile(IProblemCollector)
67    */
68   public void reconcile(IProblemCollector problemCollector, IFile file) {
69     synchronized (dirtyLock) {
70       if (!dirty) {
71         return;
72       }
73       dirty = false;
74     }
75
76     synchronized (this) {
77       boolean doParse = false;
78       root = null;
79       if (file != null) {
80         String filename = file.getLocation().toString();
81         int len = filename.length();
82         if (len >= 4) {
83           if ((filename.charAt(len - 1) != 'l' && filename.charAt(len - 1) != 'L')
84               || (filename.charAt(len - 2) != 'p' && filename.charAt(len - 2) != 'P')
85               || (filename.charAt(len - 3) != 't' && filename.charAt(len - 3) != 'T') 
86               || (filename.charAt(len - 4) != '.')) {
87             if ((filename.charAt(len - 1) != 'm' && filename.charAt(len - 1) != 'M')
88                 || (filename.charAt(len - 2) != 't' && filename.charAt(len - 2) != 'T')
89                 || (filename.charAt(len - 3) != 'h' && filename.charAt(len - 3) != 'H') 
90                 || (filename.charAt(len - 4) != '.')) {
91               if (len >= 5) {
92                 if ((filename.charAt(len - 1) != 'l' && filename.charAt(len - 1) != 'L')
93                     || (filename.charAt(len - 2) != 'm' && filename.charAt(len - 2) != 'M')
94                     || (filename.charAt(len - 3) != 't' && filename.charAt(len - 3) != 'T')
95                     || (filename.charAt(len - 4) != 'h' && filename.charAt(len - 4) != 'H') 
96                     || (filename.charAt(len - 5) != '.')) {
97                   doParse = true;
98                 }
99               }
100             }
101           }
102         } else {
103           doParse = true;
104         }
105       }
106       if (doParse) {
107         IXMLParser parser = new XMLParser();
108         parser.setProblemCollector(problemCollector);
109         parser.setSource(getDocument());
110         parser.setSystemId(systemId);
111         IXMLDocument model = parser.parse();
112         if (model != null) {
113           root = model.getRoot();
114         }
115       }
116     }
117   }
118
119   // IDocumentListener Implementation ----------------------------------------
120
121   /*
122    * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
123    */
124   public void documentAboutToBeChanged(DocumentEvent event) {
125     // do nothing
126   }
127
128   /*
129    * @see IDocumentListener#documentChanged(DocumentEvent)
130    */
131   public void documentChanged(DocumentEvent event) {
132     synchronized (dirtyLock) {
133       dirty = true;
134     }
135   }
136
137   // Public Methods ----------------------------------------------------------
138
139   /**
140    * Sets the root element.
141    * 
142    * @param root
143    *          the root element to set
144    */
145   public void setRoot(IXMLElement root) {
146     this.root = root;
147   }
148
149 }