input = args[0];
output = args[1];
}
- File inputFile = new File(input);
- ScssStylesheet scss = ScssStylesheet.get(inputFile);
+
+ // ScssStylesheet.setStylesheetResolvers(new VaadinResolver());
+
+ ScssStylesheet scss = ScssStylesheet.get(input);
scss.compile();
if (output == null) {
System.out.println(scss.toString());
String scssPath = cssPath.replace(".css", ".scss");
File scssFile = new File(scssPath);
if (scssFile.exists()) {
- ScssStylesheet scss = ScssStylesheet.get(new File(cssPath));
+ ScssStylesheet scss = ScssStylesheet.get(cssPath);
try {
scss.compile();
} catch (Exception e) {
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import org.w3c.css.sac.CSSException;
+import org.w3c.css.sac.InputSource;
import com.vaadin.sass.handler.SCSSDocumentHandler;
import com.vaadin.sass.handler.SCSSDocumentHandlerImpl;
import com.vaadin.sass.parser.Parser;
+import com.vaadin.sass.resolver.ScssStylesheetResolver;
+import com.vaadin.sass.resolver.VaadinResolver;
import com.vaadin.sass.tree.Node;
import com.vaadin.sass.visitor.BlockVisitor;
import com.vaadin.sass.visitor.ExtendVisitor;
* @throws CSSException
* @throws IOException
*/
- public static ScssStylesheet get(File file) throws CSSException,
+ public static ScssStylesheet get(String identifier) throws CSSException,
IOException {
- Parser parser = new Parser();
- SCSSDocumentHandler handler = new SCSSDocumentHandlerImpl();
+ File file = new File(identifier);
file = file.getCanonicalFile();
- handler.getStyleSheet().setFileName(file.getAbsoluteFile().getParent());
+
+ SCSSDocumentHandler handler = new SCSSDocumentHandlerImpl();
+ ScssStylesheet stylesheet = handler.getStyleSheet();
+
+ InputSource source = stylesheet.resolveStylesheet(identifier);
+ if (source == null) {
+ return null;
+ }
+
+ Parser parser = new Parser();
parser.setDocumentHandler(handler);
- parser.parseStyleSheet(file.getAbsolutePath());
- return handler.getStyleSheet();
+ parser.parseStyleSheet(source);
+
+ return stylesheet;
+ }
+
+ private static ScssStylesheetResolver[] resolvers = null;
+
+ public static void setStylesheetResolvers(
+ ScssStylesheetResolver... styleSheetResolvers) {
+ resolvers = Arrays.copyOf(styleSheetResolvers,
+ styleSheetResolvers.length);
+ }
+
+ public InputSource resolveStylesheet(String identifier) {
+ if (resolvers == null) {
+ setStylesheetResolvers(new VaadinResolver());
+ }
+
+ for (ScssStylesheetResolver resolver : resolvers) {
+ InputSource source = resolver.resolve(identifier);
+ if (source != null) {
+ File f = new File(source.getURI());
+ setFileName(f.getParent());
+ return source;
+ }
+ }
+
+ return null;
}
/**
--- /dev/null
+package com.vaadin.sass.resolver;
+
+import java.io.InputStream;
+
+import org.w3c.css.sac.InputSource;
+
+public class ClassloaderResolver implements ScssStylesheetResolver {
+
+ @Override
+ public InputSource resolve(String identifier) {
+ // identifier should not have .scss, fileName should
+ String ext = ".scss";
+ if (identifier.endsWith(".css")) {
+ ext = ".css";
+ }
+ String fileName = identifier;
+ if (identifier.endsWith(ext)) {
+ identifier = identifier.substring(0,
+ identifier.length() - ext.length());
+ } else {
+ fileName = fileName + ext;
+ }
+
+ // Can the classloader find it?
+ InputStream is = getClass().getClassLoader().getResourceAsStream(
+ fileName);
+ if (is != null) {
+ InputSource source = new InputSource();
+ source.setByteStream(is);
+ source.setURI(fileName);
+ return source;
+
+ } else {
+ return null;
+ }
+
+ }
+
+}
--- /dev/null
+package com.vaadin.sass.resolver;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import org.w3c.css.sac.InputSource;
+
+public class FilesystemResolver implements ScssStylesheetResolver {
+
+ @Override
+ public InputSource resolve(String identifier) {
+ // identifier should not have .scss, fileName should
+ String ext = ".scss";
+ if (identifier.endsWith(".css")) {
+ ext = ".css";
+ }
+ String fileName = identifier;
+ if (identifier.endsWith(ext)) {
+ identifier = identifier.substring(0,
+ identifier.length() - ext.length());
+ } else {
+ fileName = fileName + ext;
+ }
+
+ try {
+ InputStream is = new FileInputStream(fileName);
+ InputSource source = new InputSource();
+ source.setByteStream(is);
+ source.setURI(fileName);
+ return source;
+
+ } catch (FileNotFoundException e) {
+ // not found, try something else
+ return null;
+ }
+
+ }
+
+}
--- /dev/null
+package com.vaadin.sass.resolver;
+
+import org.w3c.css.sac.InputSource;
+
+public interface ScssStylesheetResolver {
+ /**
+ * Called with the "identifier" of a stylesheet that the resolver should
+ * try to find. The identifier is basically a filename, like "runo.scss"
+ * or "addon/styles.scss", but might exclude ".scss". The resolver must
+ * {@link InputSource#setURI(String)} to the final location where the
+ * stylesheet was found, e.g "runo.scss" might result in a URI like
+ * "VAADIN/themes/runo/runo.scss".
+ *
+ * @param identifier
+ * used fo find stylesheet
+ * @return InputSource for stylesheet (with URI set) or null if not
+ * found
+ */
+ public InputSource resolve(String identifier);
+}
\ No newline at end of file
--- /dev/null
+package com.vaadin.sass.resolver;
+
+import org.w3c.css.sac.InputSource;
+
+public class VaadinResolver implements ScssStylesheetResolver {
+
+ @Override
+ public InputSource resolve(String identifier) {
+ String ext = ".scss";
+ if (identifier.endsWith(".css")) {
+ ext = ".css";
+ }
+
+ // 'normalize' identifier to use in themeFile
+ String fileName = identifier;
+ if (identifier.endsWith(ext)) {
+ identifier = identifier.substring(0,
+ identifier.length() - ext.length());
+ }
+ // also look here
+ String themeFile = "VAADIN/themes/" + identifier + "/" + fileName;
+
+ // first plain file
+ ScssStylesheetResolver resolver = new FilesystemResolver();
+ InputSource source = resolver.resolve(fileName);
+
+ if (source == null) {
+ // then file in theme
+ source = resolver.resolve(themeFile);
+ }
+
+ if (source == null) {
+ // then plain via classloader
+ resolver = new ClassloaderResolver();
+ source = resolver.resolve(identifier);
+
+ }
+ if (source == null) {
+ // then try theme via classloader
+ source = resolver.resolve(themeFile);
+ }
+
+ return source;
+ }
+
+}
package com.vaadin.sass.visitor;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
if (child instanceof ImportNode) {
ImportNode importNode = (ImportNode) child;
if (!importNode.isPureCssImport()) {
- StringBuilder filePathBuilder = new StringBuilder(
- node.getFileName());
- filePathBuilder.append(File.separatorChar).append(
- importNode.getUri());
- if (!filePathBuilder.toString().endsWith(".scss")) {
- filePathBuilder.append(".scss");
- }
+
try {
- ScssStylesheet imported = ScssStylesheet.get(new File(
- filePathBuilder.toString()));
+
+ StringBuilder filePathBuilder = new StringBuilder(
+ node.getFileName());
+ filePathBuilder.append(File.separatorChar).append(
+ importNode.getUri());
+ if (!filePathBuilder.toString().endsWith(".scss")) {
+ filePathBuilder.append(".scss");
+ }
+
+ ScssStylesheet imported = ScssStylesheet
+ .get(filePathBuilder.toString());
+ if (imported == null) {
+ imported = ScssStylesheet.get(importNode.getUri());
+ }
+ if (imported == null) {
+ throw new FileNotFoundException(importNode.getUri()
+ + " (parent: " + node.getFileName() + ")");
+ }
+
traverse(imported);
String prefix = getUrlPrefix(importNode.getUri());
if (prefix != null) {
--- /dev/null
+Implement a sane test case.
\ No newline at end of file
+++ /dev/null
-Implement a sane test case.
\ No newline at end of file
public ScssStylesheet getStyleSheet(String filename)
throws URISyntaxException, CSSException, IOException {
File file = getFile(filename);
- stylesheet = ScssStylesheet.get(file);
+ stylesheet = ScssStylesheet.get(file.getAbsolutePath());
return stylesheet;
}