private String fileName;
+ private String charset;
+
/**
* Read in a file SCSS and parse it into a ScssStylesheet
*
}
/**
- * Main entry point for the SASS compiler. Takes in a file and builds upp a
+ * Main entry point for the SASS compiler. Takes in a file and builds up a
* ScssStylesheet tree out of it. Calling compile() on it will transform
* SASS into CSS. Calling toString() will print out the SCSS/CSS.
*
*/
public static ScssStylesheet get(String identifier) throws CSSException,
IOException {
+ return get(identifier, null);
+ }
+
+ /**
+ * Main entry point for the SASS compiler. Takes in a file and encoding then
+ * builds up a ScssStylesheet tree out of it. Calling compile() on it will
+ * transform SASS into CSS. Calling toString() will print out the SCSS/CSS.
+ *
+ * @param file
+ * @param encoding
+ * @return
+ * @throws CSSException
+ * @throws IOException
+ */
+ public static ScssStylesheet get(String identifier, String encoding)
+ throws CSSException, IOException {
+ /*
+ * The encoding to be used is passed through "encoding" parameter. the
+ * imported children scss node will have the same encoding as their
+ * parent, ultimately the root scss file. The root scss node has this
+ * "encoding" parameter to be null. Its encoding is determined by the
+ * @charset declaration, the default one is ASCII.
+ */
File file = new File(identifier);
file = file.getCanonicalFile();
if (source == null) {
return null;
}
+ source.setEncoding(encoding);
Parser parser = new Parser();
parser.setErrorHandler(new SCSSErrorHandler());
parser.setDocumentHandler(handler);
parser.parseStyleSheet(source);
+ stylesheet.setCharset(parser.getInputSource().getEncoding());
return stylesheet;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder("");
+ String delimeter = "\n\n";
+ // add charset declaration, if it is not default "ASCII".
+ if (!"ASCII".equals(getCharset())) {
+ string.append("@charset \"").append(getCharset()).append("\";")
+ .append(delimeter);
+ }
if (children.size() > 0) {
string.append(children.get(0).toString());
}
- String delimeter = "\n\n";
if (children.size() > 1) {
for (int i = 1; i < children.size(); i++) {
String childString = children.get(i).toString();
public static final void warning(String msg) {
Logger.getLogger(ScssStylesheet.class.getName()).warning(msg);
}
+
+ public String getCharset() {
+ return charset;
+ }
+
+ public void setCharset(String charset) {
+ this.charset = charset;
+ }
}
throw new CSSException(CSSException.SAC_NOT_SUPPORTED_ERR);
}
+ public InputSource getInputSource(){
+ return source;
+ }
+
/**
* Set the document handler for this parser
*/
}
}
}
- String encoding = "ASCII";
+ //use UTF-8 as the default encoding.
+ String encoding = source.getEncoding();
InputStream input = source.getByteStream();
- char c = ' ';
-
if (!input.markSupported()) {
input = new BufferedInputStream(input);
source.setByteStream(input);
+ input.mark(100);
}
- input.mark(100);
- c = (char) input.read();
-
- if (c == '@') {
- // hum, is it a charset ?
- int size = 100;
- byte[] buf = new byte[size];
- input.read(buf, 0, 7);
- String keyword = new String(buf, 0, 7);
- if (keyword.equals("charset")) {
- // Yes, this is the charset declaration !
-
- // here I don't use the right declaration : white space are ' '.
- while ((c = (char) input.read()) == ' ') {
- // find the first quote
- }
- char endChar = c;
- int i = 0;
+ if(encoding == null){
+ encoding = "ASCII";
- if ((endChar != '"') && (endChar != '\u005c'')) {
- // hum this is not a quote.
- throw new CSSException("invalid charset declaration");
- }
+ char c = ' ';
+
+ c = (char) input.read();
- while ((c = (char) input.read()) != endChar) {
- buf[i++] = (byte) c;
- if (i == size) {
- byte[] old = buf;
- buf = new byte[size + 100];
- System.arraycopy(old, 0, buf, 0, size);
- size += 100;
+ if (c == '@') {
+ // hum, is it a charset ?
+ int size = 100;
+ byte[] buf = new byte[size];
+ input.read(buf, 0, 7);
+ String keyword = new String(buf, 0, 7);
+ if (keyword.equals("charset")) {
+ // Yes, this is the charset declaration !
+
+ // here I don't use the right declaration : white space are ' '.
+ while ((c = (char) input.read()) == ' ') {
+ // find the first quote
}
- }
- while ((c = (char) input.read()) == ' ') {
- // find the next relevant character
- }
- if (c != ';') {
- // no semi colon at the end ?
- throw new CSSException("invalid charset declaration: "
+ char endChar = c;
+ int i = 0;
+
+ if ((endChar != '"') && (endChar != '\u005c'')) {
+ // hum this is not a quote.
+ throw new CSSException("invalid charset declaration");
+ }
+
+ while ((c = (char) input.read()) != endChar) {
+ buf[i++] = (byte) c;
+ if (i == size) {
+ byte[] old = buf;
+ buf = new byte[size + 100];
+ System.arraycopy(old, 0, buf, 0, size);
+ size += 100;
+ }
+ }
+ while ((c = (char) input.read()) == ' ') {
+ // find the next relevant character
+ }
+ if (c != ';') {
+ // no semi colon at the end ?
+ throw new CSSException("invalid charset declaration: "
+ "missing semi colon");
- }
- encoding = new String(buf, 0, i);
- if (source.getEncoding() != null) {
- // compare the two encoding informations.
- // For example, I don't accept to have ASCII and after UTF-8.
- // Is it really good ? That is the question.
- if (!encoding.equals(source.getEncoding())) {
- throw new CSSException("invalid encoding information.");
}
- }
- } // else no charset declaration available
+ encoding = new String(buf, 0, i);
+ if (source.getEncoding() != null) {
+ // compare the two encoding informations.
+ // For example, I don't accept to have ASCII and after UTF-8.
+ // Is it really good ? That is the question.
+ if (!encoding.equals(source.getEncoding())) {
+ throw new CSSException("invalid encoding information.");
+ }
+ }
+ } // else no charset declaration available
+ }
}
// ok set the real encoding of this source.
source.setEncoding(encoding);
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
- int numValue = Character.digit(c, 16);
- int count = 0;
- int p = 16;
-
- while (index + 1 < len && count < 6) {
- c = s.charAt(index+1);
-
- if (Character.digit(c, 16) != -1) {
- numValue = (numValue * 16) + Character.digit(c, 16);
- p *= 16;
- index++;
- } else {
- if (c == ' ') {
- // skip the latest white space
- index++;
- }
- break;
- }
+ buf.append('\u005c\u005c');
+ while (index < len) {
+ buf.append(s.charAt(index++));
}
- buf.append((char) numValue);
break;
case '\u005cn':
case '\u005cf':
finally { jj_save(12, xla); }
}
- private boolean jj_3R_196() {
- Token xsp;
- xsp = jj_scanpos;
- if (jj_3_3()) {
- jj_scanpos = xsp;
- if (jj_3R_239()) {
- jj_scanpos = xsp;
- if (jj_3R_240()) return true;
- }
- }
- return false;
- }
-
- private boolean jj_3_3() {
- if (jj_3R_166()) return true;
- return false;
- }
-
- private boolean jj_3_6() {
- if (jj_3R_169()) return true;
- return false;
- }
-
- private boolean jj_3_1() {
- if (jj_3R_165()) return true;
- return false;
- }
-
private boolean jj_3R_368() {
Token xsp;
xsp = jj_scanpos;
return false;
}
+ private boolean jj_3R_196() {
+ Token xsp;
+ xsp = jj_scanpos;
+ if (jj_3_3()) {
+ jj_scanpos = xsp;
+ if (jj_3R_239()) {
+ jj_scanpos = xsp;
+ if (jj_3R_240()) return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean jj_3_3() {
+ if (jj_3R_166()) return true;
+ return false;
+ }
+
+ private boolean jj_3_6() {
+ if (jj_3R_169()) return true;
+ return false;
+ }
+
+ private boolean jj_3_1() {
+ if (jj_3R_165()) return true;
+ return false;
+ }
+
/** Generated Token Manager. */
public ParserTokenManager token_source;
/** Current token. */
public void setLocale(Locale locale) throws CSSException {
throw new CSSException(CSSException.SAC_NOT_SUPPORTED_ERR);
}
+
+ public InputSource getInputSource(){
+ return source;
+ }
/**
* Set the document handler for this parser
}
}
}
- String encoding = "ASCII";
+ //use UTF-8 as the default encoding.
+ String encoding = source.getEncoding();
InputStream input = source.getByteStream();
- char c = ' ';
-
if (!input.markSupported()) {
- input = new BufferedInputStream(input);
- source.setByteStream(input);
- }
- input.mark(100);
- c = (char) input.read();
-
- if (c == '@') {
- // hum, is it a charset ?
- int size = 100;
- byte[] buf = new byte[size];
- input.read(buf, 0, 7);
- String keyword = new String(buf, 0, 7);
- if (keyword.equals("charset")) {
- // Yes, this is the charset declaration !
-
- // here I don't use the right declaration : white space are ' '.
- while ((c = (char) input.read()) == ' ') {
- // find the first quote
- }
- char endChar = c;
- int i = 0;
+ input = new BufferedInputStream(input);
+ source.setByteStream(input);
+ input.mark(100);
+ }
+ if(encoding == null){
+ encoding = "ASCII";
+
+ char c = ' ';
+
+ c = (char) input.read();
+
+ if (c == '@') {
+ // hum, is it a charset ?
+ int size = 100;
+ byte[] buf = new byte[size];
+ input.read(buf, 0, 7);
+ String keyword = new String(buf, 0, 7);
+ if (keyword.equals("charset")) {
+ // Yes, this is the charset declaration !
+
+ // here I don't use the right declaration : white space are ' '.
+ while ((c = (char) input.read()) == ' ') {
+ // find the first quote
+ }
+ char endChar = c;
+ int i = 0;
- if ((endChar != '"') && (endChar != '\'')) {
- // hum this is not a quote.
- throw new CSSException("invalid charset declaration");
- }
+ if ((endChar != '"') && (endChar != '\'')) {
+ // hum this is not a quote.
+ throw new CSSException("invalid charset declaration");
+ }
- while ((c = (char) input.read()) != endChar) {
- buf[i++] = (byte) c;
- if (i == size) {
- byte[] old = buf;
- buf = new byte[size + 100];
- System.arraycopy(old, 0, buf, 0, size);
- size += 100;
+ while ((c = (char) input.read()) != endChar) {
+ buf[i++] = (byte) c;
+ if (i == size) {
+ byte[] old = buf;
+ buf = new byte[size + 100];
+ System.arraycopy(old, 0, buf, 0, size);
+ size += 100;
+ }
}
- }
- while ((c = (char) input.read()) == ' ') {
- // find the next relevant character
- }
- if (c != ';') {
- // no semi colon at the end ?
- throw new CSSException("invalid charset declaration: "
+ while ((c = (char) input.read()) == ' ') {
+ // find the next relevant character
+ }
+ if (c != ';') {
+ // no semi colon at the end ?
+ throw new CSSException("invalid charset declaration: "
+ "missing semi colon");
- }
- encoding = new String(buf, 0, i);
- if (source.getEncoding() != null) {
- // compare the two encoding informations.
- // For example, I don't accept to have ASCII and after UTF-8.
- // Is it really good ? That is the question.
- if (!encoding.equals(source.getEncoding())) {
- throw new CSSException("invalid encoding information.");
}
- }
- } // else no charset declaration available
+ encoding = new String(buf, 0, i);
+ if (source.getEncoding() != null) {
+ // compare the two encoding informations.
+ // For example, I don't accept to have ASCII and after UTF-8.
+ // Is it really good ? That is the question.
+ if (!encoding.equals(source.getEncoding())) {
+ throw new CSSException("invalid encoding information.");
+ }
+ }
+ } // else no charset declaration available
+ }
}
// ok set the real encoding of this source.
source.setEncoding(encoding);
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
- int numValue = Character.digit(c, 16);
- int count = 0;
- int p = 16;
-
- while (index + 1 < len && count < 6) {
- c = s.charAt(index+1);
-
- if (Character.digit(c, 16) != -1) {
- numValue = (numValue * 16) + Character.digit(c, 16);
- p *= 16;
- index++;
- } else {
- if (c == ' ') {
- // skip the latest white space
- index++;
- }
- break;
- }
+ buf.append('\\');
+ while (index < len) {
+ buf.append(s.charAt(index++));
}
- buf.append((char) numValue);
break;
case '\n':
case '\f':
filePathBuilder.append(".scss");
}
- ScssStylesheet imported = ScssStylesheet
- .get(filePathBuilder.toString());
+ // set parent's charset to imported node.
+ ScssStylesheet imported = ScssStylesheet.get(
+ filePathBuilder.toString(), node.getCharset());
if (imported == null) {
imported = ScssStylesheet.get(importNode.getUri());
}
--- /dev/null
+@charset "UTF-8";
+.imported { content: "\1f4c5"; }
+.imported_raw_utf { content: "♥"; }
+.bar { content: "\1f4c5"; }
+.raw_utf { content: "📈"; }
\ No newline at end of file
--- /dev/null
+@charset "abc";
+.imported{content: '\1f4c5';}
+.imported_raw_utf{content: "♥";}
--- /dev/null
+@charset "UTF-8";
+@import "utf8-imported/to-be-imported-scss-file-contains-utf8";
+.bar {content: "\1f4c5";}
+.raw_utf {content: "📈";}
\ No newline at end of file