1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.text.DefaultLineTracker;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentPartitioner;
23 import org.eclipse.jface.text.ILineTracker;
24 import org.eclipse.jface.text.rules.DefaultPartitioner;
25 import org.eclipse.ui.editors.text.FileDocumentProvider;
26 import org.eclipse.ui.part.FileEditorInput;
29 * The PHPDocumentProvider provides the IDocuments used by java editors.
32 public class PHPDocumentProvider extends FileDocumentProvider {
34 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
35 private final static String[] TYPES =
37 IPHPPartitionScannerConstants.PHP,
38 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
39 IPHPPartitionScannerConstants.HTML,
40 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
41 IPHPPartitionScannerConstants.JAVASCRIPT,
42 IPHPPartitionScannerConstants.CSS };
44 private static PHPPartitionScanner fgScanner = null;
46 public PHPDocumentProvider() {
51 * Method declared on AbstractDocumentProvider
53 protected IDocument createDocument(Object element) throws CoreException {
54 IDocument document = super.createDocument(element);
55 if (document != null) {
56 // int fileType = 0; // PHP
57 IDocumentPartitioner partitioner = null;
58 if (element instanceof FileEditorInput) {
59 IFile file = (IFile) ((FileEditorInput) element).getAdapter(IFile.class);
60 String filename = file.getLocation().toString();
61 String extension = filename.substring(filename.lastIndexOf("."), filename.length());
62 // System.out.println(extension);
63 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
65 partitioner = createHTMLPartitioner();
66 } else if (extension.equalsIgnoreCase(".xml")) {
68 partitioner = createXMLPartitioner();
69 } else if (extension.equalsIgnoreCase(".js")) {
71 partitioner = createJavaScriptPartitioner();
72 } else if (extension.equalsIgnoreCase(".css")) {
73 // cascading style sheets
74 partitioner = createCSSPartitioner();
75 } else if (extension.equalsIgnoreCase(".tpl")) {
77 partitioner = createSmartyPartitioner();
78 } else if (extension.equalsIgnoreCase(".inc")) {
79 // php include files ?
80 partitioner = createIncludePartitioner();
84 if (partitioner == null) {
85 partitioner = createPHPPartitioner();
87 document.setDocumentPartitioner(partitioner);
88 partitioner.connect(document);
94 * Return a partitioner for .php files.
96 private IDocumentPartitioner createPHPPartitioner() {
97 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
101 * Return a partitioner for .html files.
103 private IDocumentPartitioner createHTMLPartitioner() {
104 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
107 private IDocumentPartitioner createXMLPartitioner() {
108 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
111 private IDocumentPartitioner createJavaScriptPartitioner() {
112 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
115 private IDocumentPartitioner createCSSPartitioner() {
116 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
119 private IDocumentPartitioner createSmartyPartitioner() {
120 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
123 private IDocumentPartitioner createIncludePartitioner() {
124 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
127 * Return a scanner for creating php partitions.
129 private PHPPartitionScanner getPHPPartitionScanner() {
130 if (fgScanner == null)
131 fgScanner = new PHPPartitionScanner();
136 * Creates a line tracker working with the same line delimiters as the document
137 * of the given element. Assumes the element to be managed by this document provider.
139 * @param element the element serving as blue print
140 * @return a line tracker based on the same line delimiters as the element's document
142 public ILineTracker createLineTracker(Object element) {
143 return new DefaultLineTracker();