All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sommeri.less4j.nodemime.NodeMime Maven / Gradle / Ivy

Go to download

Less language is an extension of css and less4j compiles it into regular css. It adds several dynamic features into css: variables, expressions, nested rules. Less4j is a port. The original compiler was written in JavaScript and is called less.js. The less language is mostly defined in less.js documentation/issues and by what less.js actually do. Links to less.js: * home page: http://lesscss.org/ * source code & issues: https://github.com/cloudhead/less.js

There is a newer version: 1.17.2
Show newest version
package com.github.sommeri.less4j.nodemime;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.github.sommeri.less4j.core.problems.BugHappened;

/**
 * Port of node.js mime package into java. It detects file mimetype from filename suffix. File content 
 * is ignored. 
 */
public class NodeMime {

  private static final String MIMETYPES_NODE_TYPES = "/mimetypes/node.types";
  private static final String MIMETYPES_MIME_TYPES = "/mimetypes/mime.types";
  private static final String FALLBACK_SUFFIX = "bin";
  private Map typeDatabase = null;

  public NodeMime() {
  }

  public String lookupMime(String path) {
    if (path==null)
      return defaultType();
    
    int indx = Math.max(Math.max(path.lastIndexOf("."), path.lastIndexOf("\\")), path.lastIndexOf("/"));
    String suffix = path.substring(indx + 1, path.length());
    return lookupBySuffix(suffix);
  }

  public String lookupCharset(String mimeType) {
    return lookupCharset(mimeType, null);
  }
  
  public String lookupCharset(String mimeType, String fallback) {
    if (mimeType==null)
      return fallback;
    
    return mimeType.startsWith("text") ? "UTF-8" : fallback;
  }
  
  private String lookupBySuffix(String suffix) {
    Map typeDatabase = getTypeDatabase();
    String result = typeDatabase.get(suffix);
    return result != null ? result : defaultType();
  }

  private String defaultType() {
    return typeDatabase.get(FALLBACK_SUFFIX);
  }

  private Map getTypeDatabase() {
    if (typeDatabase == null) {
      typeDatabase = new HashMap();
      load(MIMETYPES_MIME_TYPES);
      load(MIMETYPES_NODE_TYPES);
    }
    return typeDatabase;
  }

  private void load(String path) {
    InputStream stream = path.getClass().getResourceAsStream(path);
    if (stream == null)
      return;

    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (!line.startsWith("#"))
          loadLine(line);
      }
      reader.close();
    } catch (IOException e) {
      throw new BugHappened(e, null);
    }

  }

  private void loadLine(String line) {
    String[] split = line.split("\\s+");
    String mimetype = split[0];
    for (int i = 1; i < split.length; i++) {
      String suffix = split[i];
      typeDatabase.put(suffix, mimetype);
    }

  }

  public boolean isText(String mimetype) {
    if (mimetype==null)
      return false;
    
    return mimetype.contains("text");
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy