828f11ed8529acf356aad05d15d9490834d31c7a
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPDocumentPartitioner.java
1 /**********************************************************************
2  Copyright (c) 2002  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://solareclipse.sourceforge.net/legal/cpl-v10.html
7
8  Contributors:
9  Igor Malinin - initial contribution
10
11  $Id: PHPDocumentPartitioner.java,v 1.4 2005-08-18 20:09:25 axelcl Exp $
12  **********************************************************************/
13 package net.sourceforge.phpeclipse.phpeditor.php;
14
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.ui.text.rules.FlatNode;
17 import net.sourceforge.phpeclipse.ui.text.rules.MultiViewPartitioner;
18 import net.sourceforge.phpeclipse.ui.text.rules.ViewNode;
19
20 import org.eclipse.jface.text.Assert;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentPartitioner;
23 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
24
25 /**
26  * 
27  * 
28  * @author Igor Malinin
29  */
30 public class PHPDocumentPartitioner extends MultiViewPartitioner {
31   public static final String PHP_TEMPLATE_DATA = "__php_template_data";
32
33   public static final String PHP_SCRIPT_CODE = "__php_script_code";
34
35   public static final String[] LEGAL_TYPES = {
36         PHP_TEMPLATE_DATA,
37         PHP_SCRIPT_CODE
38   };
39   
40   private IPartitionTokenScanner scriptScanner;
41
42   public PHPDocumentPartitioner(IPartitionTokenScanner scanner, IPartitionTokenScanner scriptScanner) {
43     super(scanner);
44
45     this.scriptScanner = scriptScanner;
46   }
47
48   protected FlatNode createNode(String type, int offset, int length) {
49     if (type.equals(PHPPartitionScanner.PHP_SCRIPTING_AREA)) {
50       if (DEBUG) {
51         Assert.isTrue(offset >= 0);
52       }
53       ViewNode node = new ViewNode(type);
54       node.offset = offset;
55       node.length = length;
56       return node;
57     }
58
59     return super.createNode(type, offset, length);
60   }
61
62   /*
63    * @see net.sf.solareclipse.text.rules.DocumentViewPartitioner#createPartitioner(String)
64    */
65   protected IDocumentPartitioner createPartitioner(String contentType) {
66     if (contentType == null) {
67       //                        return JavaTextTools.createHTMLPartitioner();
68       return PHPeclipsePlugin.getDefault().getJavaTextTools().getXMLTextTools().createPHPXMLPartitioner();
69     }
70
71     if (contentType.equals(PHPPartitionScanner.PHP_SCRIPTING_AREA)) {
72       return PHPeclipsePlugin.getDefault().getJavaTextTools().createPHPPartitioner();
73     }
74     return null;
75   }
76
77   /*
78    * @see net.sf.solareclipse.text.rules.DocumentViewPartitioner#getContentType(String, String)
79    */
80   protected String getContentType(String parent, String view) {
81     if (parent == null) {
82       if (view == IDocument.DEFAULT_CONTENT_TYPE) {
83         return PHP_TEMPLATE_DATA;
84       }
85     } else {
86       if (view == IDocument.DEFAULT_CONTENT_TYPE) {
87         return PHP_SCRIPT_CODE;
88       }
89     }
90
91     return super.getContentType(parent, view);
92   }
93
94         public String[] getLegalContentTypes() {
95                 return LEGAL_TYPES;
96         }
97 }