Refactory
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / xml / ui / text / XMLTextTools.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU 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  *     Igor Malinin - initial contribution
10  *
11  * $Id: XMLTextTools.java,v 1.4 2006-10-21 23:14:13 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.text;
15
16 import java.util.Map;
17
18 import net.sourceforge.phpeclipse.ui.text.AbstractTextTools;
19 import net.sourceforge.phpeclipse.xml.ui.internal.text.DeclScanner;
20 import net.sourceforge.phpeclipse.xml.ui.internal.text.PHPXMLPartitionScanner;
21 import net.sourceforge.phpeclipse.xml.ui.internal.text.SingleTokenScanner;
22 import net.sourceforge.phpeclipse.xml.ui.internal.text.TextScanner;
23 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLCDATAScanner;
24 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLPartitionScanner;
25 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLTagScanner;
26
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.text.IDocumentPartitioner;
29 //import org.eclipse.jface.text.rules.DefaultPartitioner;
30 import org.eclipse.jface.text.rules.FastPartitioner;
31 import org.eclipse.jface.text.rules.ITokenScanner;
32 import org.eclipse.jface.text.rules.RuleBasedScanner;
33
34 /**
35  * 
36  * 
37  * @author Igor Malinin
38  */
39 public class XMLTextTools extends AbstractTextTools {
40         /** Text Attributes for XML editors */
41         public static final String[] TOKENS = { IXMLSyntaxConstants.XML_DEFAULT,
42                         IXMLSyntaxConstants.XML_TAG, IXMLSyntaxConstants.XML_ATT_NAME,
43                         IXMLSyntaxConstants.XML_ATT_VALUE, IXMLSyntaxConstants.XML_ENTITY,
44                         IXMLSyntaxConstants.XML_PI, IXMLSyntaxConstants.XML_CDATA,
45                         IXMLSyntaxConstants.XML_COMMENT, IXMLSyntaxConstants.XML_SMARTY,
46                         IXMLSyntaxConstants.XML_DECL, };
47
48         /** Content types for XML editors */
49         public static final String[] TYPES = { XMLPartitionScanner.XML_PI,
50                         XMLPartitionScanner.XML_COMMENT, XMLPartitionScanner.XML_DECL,
51                         XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_ATTRIBUTE,
52                         XMLPartitionScanner.XML_CDATA, XMLPartitionScanner.DTD_INTERNAL,
53                         XMLPartitionScanner.DTD_INTERNAL_PI,
54                         XMLPartitionScanner.DTD_INTERNAL_COMMENT,
55                         XMLPartitionScanner.DTD_INTERNAL_DECL, };
56
57         /** The XML partitions scanner */
58         private XMLPartitionScanner xmlPartitionScanner;
59
60         private PHPXMLPartitionScanner phpXMLPartitionScanner;
61
62         /** The XML text scanner */
63         private TextScanner xmlTextScanner;
64
65         /** The DTD text scanner */
66         private TextScanner dtdTextScanner;
67
68         /** The XML tags scanner */
69         private XMLTagScanner xmlTagScanner;
70
71         /** The XML attributes scanner */
72         private TextScanner xmlAttributeScanner;
73
74         /** The XML CDATA sections scanner */
75         private XMLCDATAScanner xmlCDATAScanner;
76
77         /** The XML processing instructions scanner */
78         private SingleTokenScanner xmlPIScanner;
79
80         /** The XML comments scanner */
81         private SingleTokenScanner xmlCommentScanner;
82
83         /** The XML declarations scanner */
84         private DeclScanner xmlDeclScanner;
85
86         public XMLTextTools(IPreferenceStore store) {
87                 this(store, TOKENS);
88         }
89
90         /**
91          * Creates a new XML text tools collection.
92          */
93         public XMLTextTools(IPreferenceStore store, String[] strTokens) {
94                 super(store, strTokens);
95
96                 xmlPartitionScanner = new XMLPartitionScanner(false);
97                 phpXMLPartitionScanner = new PHPXMLPartitionScanner(false);
98                 Map tokens = getTokens();
99
100                 xmlTextScanner = new TextScanner(tokens, '&',
101                                 IXMLSyntaxConstants.XML_DEFAULT);
102
103                 dtdTextScanner = new TextScanner(tokens, '%',
104                                 IXMLSyntaxConstants.XML_DEFAULT);
105
106                 xmlPIScanner = new SingleTokenScanner(tokens,
107                                 IXMLSyntaxConstants.XML_PI);
108
109                 xmlCommentScanner = new SingleTokenScanner(tokens,
110                                 IXMLSyntaxConstants.XML_COMMENT);
111
112                 xmlDeclScanner = new DeclScanner(tokens);
113
114                 xmlTagScanner = new XMLTagScanner(tokens);
115
116                 xmlAttributeScanner = new TextScanner(tokens, '&',
117                                 IXMLSyntaxConstants.XML_ATT_VALUE);
118
119                 xmlCDATAScanner = new XMLCDATAScanner(tokens);
120         }
121
122         /**
123          * 
124          */
125         public IDocumentPartitioner createXMLPartitioner() {
126                 return new /*DefaultPartitioner*/FastPartitioner(xmlPartitionScanner, TYPES);
127         }
128
129         public IDocumentPartitioner createPHPXMLPartitioner() {
130                 return new /*DefaultPartitioner*/FastPartitioner(phpXMLPartitionScanner, TYPES);
131         }
132
133         /**
134          * 
135          */
136         // public IPartitionTokenScanner getXMLPartitionScanner() {
137         // return xmlPartitionScanner;
138         // }
139         /**
140          * Returns a scanner which is configured to scan XML text.
141          * 
142          * @return an XML text scanner
143          */
144         public RuleBasedScanner getXMLTextScanner() {
145                 return xmlTextScanner;
146         }
147
148         /**
149          * Returns a scanner which is configured to scan DTD text.
150          * 
151          * @return an DTD text scanner
152          */
153         public RuleBasedScanner getDTDTextScanner() {
154                 return dtdTextScanner;
155         }
156
157         /**
158          * Returns a scanner which is configured to scan XML tags.
159          * 
160          * @return an XML tag scanner
161          */
162         public RuleBasedScanner getXMLTagScanner() {
163                 return xmlTagScanner;
164         }
165
166         /**
167          * Returns a scanner which is configured to scan XML tag attributes.
168          * 
169          * @return an XML tag attribute scanner
170          */
171         public RuleBasedScanner getXMLAttributeScanner() {
172                 return xmlAttributeScanner;
173         }
174
175         /**
176          * Returns a scanner which is configured to scan XML CDATA sections.
177          * 
178          * @return an XML CDATA section scanner
179          */
180         public ITokenScanner getXMLCDATAScanner() {
181                 return xmlCDATAScanner;
182         }
183
184         /**
185          * Returns a scanner which is configured to scan XML processing
186          * instructions.
187          * 
188          * @return an XML processing instruction scanner
189          */
190         public RuleBasedScanner getXMLPIScanner() {
191                 return xmlPIScanner;
192         }
193
194         /**
195          * Returns a scanner which is configured to scan XML comments.
196          * 
197          * @return an XML comment scanner
198          */
199         public RuleBasedScanner getXMLCommentScanner() {
200                 return xmlCommentScanner;
201         }
202
203         /**
204          * Returns a scanner which is configured to scan XML declarations.
205          * 
206          * @return an XML declaration scanner
207          */
208         public RuleBasedScanner getXMLDeclScanner() {
209                 return xmlDeclScanner;
210         }
211 }