use of org.apache.ofbiz.base.util.collections.MapStack in project ofbiz-framework by apache.
the class RenderContentAndSubContent method getWriter.
@Override
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final Environment env = Environment.getCurrentEnvironment();
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
final Map<String, Object> envMap = FreeMarkerWorker.createEnvironmentMap(env);
final MapStack<String> templateRoot = MapStack.create();
((MapStack) templateRoot).push(envMap);
if (Debug.verboseOn()) {
Debug.logVerbose("in RenderContentAndSubContent, contentId(0):" + templateRoot.get("contentId"), module);
}
FreeMarkerWorker.getSiteParameters(request, templateRoot);
FreeMarkerWorker.overrideWithArgs(templateRoot, args);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
renderSubContent();
}
public void renderSubContent() throws IOException {
String mimeTypeId = (String) templateRoot.get("mimeTypeId");
Object localeObject = templateRoot.get("locale");
Locale locale = null;
if (localeObject == null) {
locale = UtilHttp.getLocale(request);
} else {
locale = UtilMisc.ensureLocale(localeObject);
}
if (Debug.verboseOn()) {
Debug.logVerbose("in RenderContentAndSubContent, contentId(2):" + templateRoot.get("contentId"), module);
}
if (Debug.verboseOn()) {
Debug.logVerbose("in RenderContentAndSubContent, subContentId(2):" + templateRoot.get("subContentId"), module);
}
try {
String contentId = (String) templateRoot.get("contentId");
String mapKey = (String) templateRoot.get("mapKey");
String contentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
if (UtilValidate.isNotEmpty(mapKey) || UtilValidate.isNotEmpty(contentAssocTypeId)) {
String txt = ContentWorker.renderSubContentAsText(dispatcher, contentId, mapKey, templateRoot, locale, mimeTypeId, true);
out.write(txt);
} else if (contentId != null) {
ContentWorker.renderContentAsText(dispatcher, contentId, out, templateRoot, locale, mimeTypeId, null, null, true);
}
} catch (GeneralException e) {
String errMsg = "Error rendering thisContentId:" + (String) templateRoot.get("contentId") + " msg:" + e.toString();
Debug.logError(e, errMsg, module);
// just log a message and don't return anything: throw new IOException();
}
}
};
}
use of org.apache.ofbiz.base.util.collections.MapStack in project ofbiz-framework by apache.
the class RenderContentTransform method getWriter.
@Override
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final Environment env = Environment.getCurrentEnvironment();
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
final Map<String, Object> templateRoot = MapStack.create(FreeMarkerWorker.createEnvironmentMap(env));
((MapStack) templateRoot).push(args);
final String xmlEscape = (String) templateRoot.get("xmlEscape");
final String thisContentId = (String) templateRoot.get("contentId");
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
renderSubContent();
}
public void renderSubContent() throws IOException {
String mimeTypeId = (String) templateRoot.get("mimeTypeId");
Object localeObject = templateRoot.get("locale");
Locale locale = null;
if (localeObject == null) {
locale = UtilHttp.getLocale(request);
} else {
locale = UtilMisc.ensureLocale(localeObject);
}
String editRequestName = (String) templateRoot.get("editRequestName");
if (UtilValidate.isNotEmpty(editRequestName)) {
String editStyle = getEditStyle();
openEditWrap(out, editStyle);
}
try {
String txt = null;
String mapKey = (String) templateRoot.get("mapKey");
if (UtilValidate.isEmpty(mapKey)) {
txt = ContentWorker.renderContentAsText(dispatcher, thisContentId, templateRoot, locale, mimeTypeId, true);
} else {
txt = ContentWorker.renderSubContentAsText(dispatcher, thisContentId, mapKey, templateRoot, locale, mimeTypeId, true);
}
if ("true".equals(xmlEscape)) {
txt = UtilFormatOut.encodeXmlValue(txt);
}
out.write(txt);
} catch (GeneralException e) {
String errMsg = "Error rendering thisContentId:" + thisContentId + " msg:" + e.toString();
Debug.logError(e, errMsg, module);
// just log a message and don't return anything: throw new IOException();
}
if (UtilValidate.isNotEmpty(editRequestName)) {
closeEditWrap(out, editRequestName);
}
}
public void openEditWrap(Writer out, String editStyle) throws IOException {
String divStr = "<div class=\"" + editStyle + "\">";
out.write(divStr);
}
public void closeEditWrap(Writer out, String editRequestName) throws IOException {
String fullRequest = editRequestName;
String delim = "?";
if (UtilValidate.isNotEmpty(thisContentId)) {
fullRequest += delim + "contentId=" + thisContentId;
delim = "&";
}
out.write("<a href=\"");
ServletContext servletContext = request.getSession().getServletContext();
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
out.append(rh.makeLink(request, response, "/" + fullRequest, false, false, true));
out.write("\">Edit</a>");
out.write("</div>");
}
public String getEditStyle() {
String editStyle = (String) templateRoot.get("editStyle");
if (UtilValidate.isEmpty(editStyle)) {
editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
}
if (UtilValidate.isEmpty(editStyle)) {
editStyle = "buttontext";
}
return editStyle;
}
};
}
Aggregations