Commiting more changes to fix RSE issues with PHP projects.
[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.2 2006-10-21 23:13:43 pombredanne 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,
32                 IDocumentListener {
33         // Instance Variables ------------------------------------------------------
34
35         private IXMLElement root;
36
37         private String systemId;
38
39         private Object dirtyLock = new Object();
40
41         private boolean dirty = true;
42
43         // Constructors ------------------------------------------------------------
44
45         public XMLDocument(IDocument document, String systemId) {
46                 super(document, 0, document.getLength());
47                 this.systemId = systemId;
48         }
49
50         // IXMLDocument Implementation ---------------------------------------------
51
52         /*
53          * @see IXMLDocument#getRoot()
54          */
55         public IXMLElement getRoot() {
56                 return root;
57         }
58
59         /*
60          * @see net.sourceforge.phpeclipse.xml.core.model.IXMLDocument#getSystemId()
61          */
62         public String getSystemId() {
63                 return systemId;
64         }
65
66         /*
67          * @see IStyleSheet#reconcile(IProblemCollector)
68          */
69         public void reconcile(IProblemCollector problemCollector, IFile file) {
70                 synchronized (dirtyLock) {
71                         if (!dirty) {
72                                 return;
73                         }
74                         dirty = false;
75                 }
76
77                 synchronized (this) {
78                         boolean doParse = false;
79                         root = null;
80                         if (file != null) {
81                                 String filename = file.getFullPath().toString();
82                                 int len = filename.length();
83                                 if (len >= 4) {
84                                         if ((filename.charAt(len - 1) != 'l' && filename
85                                                         .charAt(len - 1) != 'L')
86                                                         || (filename.charAt(len - 2) != 'p' && filename
87                                                                         .charAt(len - 2) != 'P')
88                                                         || (filename.charAt(len - 3) != 't' && filename
89                                                                         .charAt(len - 3) != 'T')
90                                                         || (filename.charAt(len - 4) != '.')) {
91                                                 if ((filename.charAt(len - 1) != 'm' && filename
92                                                                 .charAt(len - 1) != 'M')
93                                                                 || (filename.charAt(len - 2) != 't' && filename
94                                                                                 .charAt(len - 2) != 'T')
95                                                                 || (filename.charAt(len - 3) != 'h' && filename
96                                                                                 .charAt(len - 3) != 'H')
97                                                                 || (filename.charAt(len - 4) != '.')) {
98                                                         if (len >= 5) {
99                                                                 if ((filename.charAt(len - 1) != 'l' && filename
100                                                                                 .charAt(len - 1) != 'L')
101                                                                                 || (filename.charAt(len - 2) != 'm' && filename
102                                                                                                 .charAt(len - 2) != 'M')
103                                                                                 || (filename.charAt(len - 3) != 't' && filename
104                                                                                                 .charAt(len - 3) != 'T')
105                                                                                 || (filename.charAt(len - 4) != 'h' && filename
106                                                                                                 .charAt(len - 4) != 'H')
107                                                                                 || (filename.charAt(len - 5) != '.')) {
108                                                                         doParse = true;
109                                                                 }
110                                                         }
111                                                 }
112                                         }
113                                 } else {
114                                         doParse = true;
115                                 }
116                         }
117                         if (doParse) {
118                                 IXMLParser parser = new XMLParser();
119                                 parser.setProblemCollector(problemCollector);
120                                 parser.setSource(getDocument());
121                                 parser.setSystemId(systemId);
122                                 IXMLDocument model = parser.parse();
123                                 if (model != null) {
124                                         root = model.getRoot();
125                                 }
126                         }
127                 }
128         }
129
130         // IDocumentListener Implementation ----------------------------------------
131
132         /*
133          * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
134          */
135         public void documentAboutToBeChanged(DocumentEvent event) {
136                 // do nothing
137         }
138
139         /*
140          * @see IDocumentListener#documentChanged(DocumentEvent)
141          */
142         public void documentChanged(DocumentEvent event) {
143                 synchronized (dirtyLock) {
144                         dirty = true;
145                 }
146         }
147
148         // Public Methods ----------------------------------------------------------
149
150         /**
151          * Sets the root element.
152          * 
153          * @param root
154          *            the root element to set
155          */
156         public void setRoot(IXMLElement root) {
157                 this.root = root;
158         }
159
160 }