readers.add(new StringReader("\n//Begin " + id));
readers.add(rd);
readers.add(new StringReader("\n//End " + id));
}
}
return new CompositeReader(readers);
} else {
return null;
}
} else {
ScriptResource resource = getResource(resourceId);
if (resource != null) {
List<Module> modules = new ArrayList<Module>(resource.getModules());
Collections.sort(modules, MODULE_COMPARATOR);
ArrayList<Reader> readers = new ArrayList<Reader>(modules.size() * 2);
StringBuilder buffer = new StringBuilder();
//
boolean isModule = FetchMode.ON_LOAD.equals(resource.getFetchMode());
if (resource.isNativeAmd()) {
/* nothing to do for an AMD module */
// buffer.append("/* native AMD module */\n");
} else if (isModule) {
Set<ResourceId> depResourceIds = resource.getDependencies();
int argCount = depResourceIds.size();
JSONArray deps = new JSONArray();
LinkedList<String> params = new LinkedList<String>();
List<String> argNames = new ArrayList<String>(argCount);
List<String> argValues = new ArrayList<String>(argCount);
for (ResourceId id : depResourceIds) {
ScriptResource dep = getResource(id);
if (dep != null) {
Set<DepInfo> depInfos = resource.getDepInfo(id);
for (DepInfo info : depInfos) {
String pluginRS = info.getPluginRS();
String alias = info.getAlias();
if (alias == null) {
alias = dep.getAlias();
}
deps.put(parsePluginRS(dep.getId().toString(), pluginRS));
params.add(encode(params, alias));
argNames.add(parsePluginRS(alias, pluginRS));
}
} else if (RESERVED_MODULE.contains(id.getName())) {
String reserved = id.getName();
deps.put(reserved);
params.add(reserved);
argNames.add(reserved);
}
}
argValues.addAll(params);
int reserveIdx = argValues.indexOf("require");
if (reserveIdx != -1) {
argValues.set(reserveIdx, "eXo.require");
}
//
buffer.append("\ndefine('").append(resourceId).append("', ");
buffer.append(deps);
buffer.append(", function(");
buffer.append(StringUtils.join(params, ","));
buffer.append(") {\nvar require = eXo.require, requirejs = eXo.require,define = eXo.define;");
buffer.append("\neXo.define.names=").append(new JSONArray(argNames)).append(";");
buffer.append("\neXo.define.deps=[").append(StringUtils.join(argValues, ",")).append("]").append(";");
buffer.append("\nreturn ");
}
//
for (Module js : modules) {
Reader jScript = getJavascript(js, locale);
if (jScript != null) {
readers.add(new StringReader(buffer.toString()));
buffer.setLength(0);
readers.add(new NormalizeJSReader(jScript));
}
}
if (resource.isNativeAmd()) {
/* nothing to do for an AMD module */
//buffer.append("\n");
} else if (isModule) {
buffer.append("\n});");
} else {
buffer.append("\nif (typeof define === 'function' && define.amd && !require.specified('")
.append(resource.getId()).append("')) {");
buffer.append("define('").append(resource.getId()).append("');}");
}
readers.add(new StringReader(buffer.toString()));
return new CompositeReader(readers);
} else {
return null;
}
}
}