Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 1&1 Internet AG, https://github.com/1and1/ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package net.oneandone.jasmin.model;
 
 
 
 import java.io.Writer;
 import java.util.List;

Reference to a node, with minimize flag and type.
 
 public class References {
     public static final byte LF = 10;
     private static final Mapper SSASS;
 
     static {
          = new Mapper("net.oneandone.ssass.Mapper");
         .load();
     }
 
     public static References create(MimeType typeboolean minimizeNode node) {
         References result;
 
         result = new References(typeminimize);
         result.add(minimizenode);
         return result;
     }
 
     public final MimeType type;
     public final boolean overallMinimize;
     public final List<Booleanminimizes;
     public final List<Nodenodes;
 
     public References(MimeType typeboolean overallMinimize) {
         this. = type;
         this. = overallMinimize;
         this. = new ArrayList<>();
         this. = new ArrayList<>();
     }
 
     public void add(boolean minimizeNode node) {
         .add(minimize);
         .add(node);
     }

    
core method
 
     public void writeTo(Writer writerthrows IOException {
         switch () {
             case :
                 writeCssTo(writer);
                 break;
             case :
                 writeJsTo(writer);
                 break;
             default:
                 throw new IllegalStateException(.toString());
         }
     }

    
core method
 
     private void writeJsTo(Writer writerthrows IOException {
         Compiler compiler;
         CompilerOptions options;
         List<SourceFileexternals;
         List<SourceFilesources;
        Result result;
        boolean first;
        int i;
        Node node;
        compiler = new Compiler(new LoggerErrorManager());
        options = new CompilerOptions();
        options.setOutputCharset(.);
        sources = new ArrayList<>();
        externals = new ArrayList<>();
        for (i = 0; i < .size(); i++) {
            node = .get(i);
            sources.add(SourceFile.fromCode(location(node)
                    + /* to get unique names, which is checked by the compiler: */ "_" + ireadString(node)));
        }
        result = compiler.compile(externalssourcesoptions);
        if (!result.success) {
            if (result.errors.length < 1) {
                throw new IllegalStateException();
            }
            throw new IOException(result.errors[0]. + ":" + result.errors[0]. + ":" + result.errors[0].);
        }
        if () {
            writer.write(compiler.toSource());
        } else {
            first = true;
            for (SourceFile source : sources) {
                if (first) {
                    first = false;
                } else {
                    writer.write();
                }
                if (!) {
                    writer.write(.comment(source.getName()));
                }
                writer.write(source.getCode());
            }
        }
    }
    private static String readString(Node nodethrows IOException {
        try {
            return node.readString();
        } catch (CreateInputStreamException e) {
            if ((e.getCause() instanceof org.apache.http.NoHttpResponseException) && (node instanceof WebdavNode)) {
                try {
                    return node.readString();
                } catch (IOException e2) {
                    e.addSuppressed(e2);
                    throw e;
                }
            }
            throw e;
        }
    }

    
hook method
    protected void writeCssTo(Writer writerthrows IOException {
        writeCssTo(writernew Output(writer));
    }
    protected void writeCssTo(Writer writerOutput outputthrows IOException {
        Object[] results;
        Mapper mapper;
        Node node;
        mapper = .newInstance();
        mapper.setErrorHandler(new ExceptionErrorHandler());
        for (int i = 0; i < .size(); i++) {
            node = .get(i);
            if (i > 0) {
                writer.write();
            }
            if (!) {
                writer.write(.comment(location(node)));
            }
            results = mapper.run(node);
            if (results == null) {
                throw new IOException(node.toString() + ": css/sass error");
            }
            try {
                ((Stylesheetresults[0]).toCss(output);
            } catch (GenericException e) {
                throw new IOException(node.toString() + ": css generation failed: " + e.getMessage(), e);
            }
        }
    }
    public String readString() throws IOException {
        StringWriter result;
        result = new StringWriter();
        writeTo(result);
        return result.toString();
    }
    public byte[] readBytes() throws IOException {
        return readString().getBytes(.);
    }
    /* @return -1 for unknown */
    public long getLastModified() throws GetLastModifiedException {
        long result;
        result = .;
        for (Node node : ) {
            if (node instanceof WebdavNode) {
                // skip getLastModified - it's not supported by Webservice Stub Servlet
            } else {
                result = Math.max(result , node.getLastModified());
            }
        }
        return result == . ? -1 : result;
    }
    //--
    @Override
    public int hashCode() {
        return .hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        References references;
        if (obj instanceof References) {
            references = (Referencesobj;
            return  == references.type && .equals(references.minimizes) && .equals(references.nodes);
        }
        return false;
    }
    @Override
    public String toString() {
        StringBuilder builder;
        builder = new StringBuilder();
        for (int i = 0; i < .size(); i++) {
            builder.append(.get(i).getURI());
            builder.append(.get(i) ? "(min) " : " ");
        }
        return builder.toString();
    }
    //--
    // do not return full paths -- security!
    public static String location(Node node) {
        String name;
        int idx;
        if (node instanceof ZipNode) {
            name = ((ZipNodenode).getRoot().getZip().getName();
            idx = name.lastIndexOf('/');
            if (idx >= 0) {
                name = name.substring(idx + 1);
            }
            return "zip:" + name + "/" + node.getPath();
        } else if (node instanceof FileNode) {
            return "file:" + node.getRelative(node.getWorld().getWorking());
        } else if (node instanceof WebdavNode) {
            return "http:" + node.getName() + ((WebdavNodenode).getQuery();
        } else {
            return node.getName();
        }
    }
    //--
    public class LoggerErrorManager extends BasicErrorManager {
        public LoggerErrorManager() {
        }
        @Override
        public void println(CheckLevel levelJSError error) {
            switch (level) {
                case :
                    ..debug("error: " + error.toString());
                    break;
                case :
                    ..debug("warning: " + error.toString());
                    break;
            }
        }
        @Override
        protected void printSummary() {
        }
    }
New to GrepCode? Check out our FAQ X