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

juzu.impl.plugin.amd.AmdMetaModelPlugin Maven / Gradle / Ivy

/*
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package juzu.impl.plugin.amd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import juzu.asset.AssetLocation;
import juzu.impl.compiler.ProcessingContext;
import juzu.impl.metamodel.AnnotationKey;
import juzu.impl.metamodel.AnnotationState;
import juzu.impl.plugin.application.metamodel.ApplicationMetaModel;
import juzu.impl.plugin.application.metamodel.ApplicationMetaModelPlugin;
import juzu.impl.plugin.asset.Asset;
import juzu.impl.plugin.asset.AssetsMetaModel;
import juzu.plugin.amd.Modules;

/**
 * @author Nguyen Thanh Hai
 * @version $Id$
 * 
 */
public class AmdMetaModelPlugin extends ApplicationMetaModelPlugin {

  /** . */
  private static final Asset REQUIRE_JS = new Asset(
      "juzu.amd",
      "script",
      "/juzu/impl/plugin/amd/require.js",
      null, Collections.emptyList(),
      AssetLocation.APPLICATION,
      null, null);

  public AmdMetaModelPlugin() {
    super("amd");
  }

  @Override
  public Set> init(ProcessingContext env) {
    return Collections.>singleton(Modules.class);
  }

  @Override
  public void processAnnotationAdded(ApplicationMetaModel metaModel, AnnotationKey key, AnnotationState added) {
    if (metaModel.getHandle().equals(key.getElement())) {
      List> value = (List>)added.get("value");
      Integer maxAge = (Integer)added.get("maxAge");

      //
      AssetsMetaModel assetsMetaModel = metaModel.getChild(AssetsMetaModel.KEY);
      assetsMetaModel.removeAssets("module");
      ArrayList list = new ArrayList();
      for (Map a : value) {
        HashMap asset = new HashMap(a);
        if (asset.get("location") == null) {
          asset.put("location", AssetLocation.APPLICATION.name());
        }
        if (asset.get("maxAge") == null && maxAge != null) {
          asset.put("maxAge", maxAge);
        }
        if (asset.get("id") == null) {
          asset.put("id", asset.get("value"));
        }
        List aliases = (List)a.get("aliases");
        String adapter = (String)a.get("adapter");
        list.add(new ModuleAsset(asset, adapter, aliases));
      }

      //
      for (Asset asset : list) {
        assetsMetaModel.addAsset(asset);
      }

      //
      assetsMetaModel.addAsset(REQUIRE_JS);
    }
  }

  @Override
  public void processAnnotationRemoved(ApplicationMetaModel metaModel, AnnotationKey key, AnnotationState removed) {
    if (metaModel.getHandle().equals(key.getElement())) {
      AssetsMetaModel assetsMetaModel = metaModel.getChild(AssetsMetaModel.KEY);
      assetsMetaModel.removeAssets("module");
      assetsMetaModel.removeAsset(REQUIRE_JS);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy