intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.core / src / net / sourceforge / phpeclipse / css / core / internal / text / CssTextUtils.java
1 /*
2  * Copyright (c) 2003-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 and implementation
10  * 
11  * $Id: CssTextUtils.java,v 1.1 2004-09-02 18:07:13 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.core.internal.text;
15
16 import java.io.IOException;
17
18 import net.sourceforge.phpeclipse.css.core.CssCore;
19
20 import org.eclipse.jface.text.IDocument;
21
22 /**
23  * Various utility methods for handling CSS source.
24  * 
25  * TODO Probably refactor so this class isn't needed
26  */
27 public final class CssTextUtils {
28
29         // Constructors ------------------------------------------------------------
30
31         /**
32          * Hidden.
33          */
34         private CssTextUtils() {
35                 // Hidden
36         }
37
38         // Public Methods ----------------------------------------------------------
39
40         public static int findMatchingClosingPeer(IDocument document, int offset,
41                 char openingPeer) {
42                 try {
43                         CssCodeReader reader = new CssCodeReader(document, offset,
44                                 document.getLength(), CssCodeReader.FORWARD);
45                         char closingChar = getPeerCharacter(openingPeer);
46                         int stack = 1;
47                         int c = reader.read();
48                         while (c != -1) {
49                                 if ((c == openingPeer) && (c != closingChar)) {
50                                         stack++;
51                                 } else if (c == closingChar) {
52                                         stack--;
53                                 }
54                                 if (stack == 0) {
55                                         return reader.getOffset();
56                                 }
57                                 c = reader.read();
58                         }
59                 } catch (IOException e) {
60                         CssCore.log(
61                                 "Failed to find matching closing peer for '" + //$NON-NLS-1$
62                                 openingPeer + '\'', e);
63                 }
64                 return -1;
65         }
66
67         public static int findMatchingOpeningPeer(IDocument document, int offset,
68                 char closingPeer) {
69                 try {
70                         CssCodeReader reader = new CssCodeReader(document, offset + 1,
71                                 document.getLength(), CssCodeReader.BACKWARD);
72                         char openingChar = getPeerCharacter(closingPeer);
73                         int stack = 1;
74                         int c = reader.read();
75                         while (c != -1) {
76                                 if ((c == closingPeer) && (c != openingChar)) {
77                                         stack++;
78                                 } else if (c == openingChar) {
79                                         stack--;
80                                 }
81                                 if (stack == 0) {
82                                         return reader.getOffset();
83                                 }
84                                 c = reader.read();
85                         }
86                 } catch (IOException e) {
87                         CssCore.log(
88                                 "Failed to find matching opening peer for '" + //$NON-NLS-1$
89                                 closingPeer + '\'', e);
90                 }
91                 return  -1;
92         }
93
94         public static char getPeerCharacter(char c) {
95                 switch (c) {
96                         case '{': return '}';
97                         case '}': return '{';
98                         case '[': return ']';
99                         case ']': return '[';
100                         case '(': return ')';
101                         case ')': return '(';
102                         case '"': return '"';
103                         case '\'': return '\'';
104                         default: return 0;
105                 }
106         }
107
108         public static boolean isCssIdentifierPart(char c) {
109                 return ((c == '_') || Character.isLetterOrDigit(c) || (c == '-'));
110         }
111
112         public static boolean isCssIdentifierStart(char c) {
113                 return ((c == '_') || Character.isLetter(c));
114         }
115
116         public static boolean isCssNumberPart(char c) {
117                 return ((c == '.') || Character.isDigit(c));
118         }
119
120         public static boolean isCssNumberStart(char c) {
121                 return ((c == '-') || (c == '.') || Character.isDigit(c));
122         }
123
124         public static boolean isCssWhitespace(char c) {
125                 return Character.isWhitespace(c);
126         }
127
128 }