return;
}
try {
GadgetContext context = gadget.getContext();
MessageBundle bundle = messageBundleFactory.getBundle(gadget.getSpec(), context.getLocale(),
context.getIgnoreCache(), context.getContainer(), context.getView());
MessageELResolver messageELResolver = new MessageELResolver(expressions, bundle);
this.elContext = expressions.newELContext(messageELResolver,
new GadgetELResolver(gadget.getContext()));
this.elContext.putContext(GadgetContext.class, elContext);
Document document = mutableContent.getDocument();
processChildNodes(document);
Element head = (Element) DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head");
Element title = document.createElement("title");
ModulePrefs mps = gadget.getSpec().getModulePrefs();
String titleValue = "default title";
if (mps != null && mps.getTitle() != null && !mps.getTitle().isEmpty()){
titleValue = mps.getTitle();
}
title.appendChild(title.getOwnerDocument().createTextNode(titleValue));
// Insert new content before any of the existing children of the head element
Node firstHeadChild = head.getFirstChild();
head.insertBefore(title, firstHeadChild);
Element injectedStyle = document.createElement("style");
injectedStyle.setAttribute("type", "text/css");
head.insertBefore(injectedStyle, firstHeadChild);
// Inject default scrolling to the body
this.injectDefaultScrolling(injectedStyle);
// Only inject default styles if no doctype was specified.
if (document.getDoctype() == null) {
injectedStyle.appendChild(injectedStyle.getOwnerDocument().
createTextNode(DEFAULT_CSS));
}
// Override & insert DocType if Gadget is written for OpenSocial 2.0 or greater,
// if quirksmode is not set
if(gadget.getSpecificationVersion().isEqualOrGreaterThan("2.0.0")
&& !gadget.useQuirksMode()){
String container = gadget.getContext().getContainer();
String doctype_qname = defaultDoctypeQName;
String doctype_sysid = defaultDoctypeSysId;
String doctype_pubid = defaultDoctypePubId;
String value = containerConfig.getString(container, REWRITE_DOCTYPE_QNAME);
if(value != null){
doctype_qname = value;
}
value = containerConfig.getString(container, REWRITE_DOCTYPE_SYSID);
if(value != null){
doctype_sysid = value;
}
value = containerConfig.getString(container, REWRITE_DOCTYPE_PUBID);
if(value != null){
doctype_pubid = value;
}
//Don't inject DOCTYPE if QName is null
if(doctype_qname != null){
DocumentType docTypeNode = document.getImplementation()
.createDocumentType(doctype_qname, doctype_pubid, doctype_sysid);
if(document.getDoctype() != null){
document.removeChild(document.getDoctype());
}
document.insertBefore(docTypeNode, document.getFirstChild());
}
}
Element html= (Element)document.getElementsByTagName("html").item(0);
if(html != null){
Locale locale = gadget.getContext().getLocale();
if (locale != null) {
String locStr = locale.toString();
String locValue = locStr.replace("_", "-");
html.setAttribute("lang", locValue);
html.setAttribute("xml:lang", locValue);
}
}
injectBaseTag(gadget, head);
injectGadgetBeacon(gadget, head, firstHeadChild);
injectFeatureLibraries(gadget, head, firstHeadChild);
// This can be one script block.
Element mainScriptTag = document.createElement("script");
injectMessageBundles(bundle, mainScriptTag);
injectDefaultPrefs(gadget, mainScriptTag);
injectPreloads(gadget, mainScriptTag);
// We need to inject our script before any developer scripts.
head.insertBefore(mainScriptTag, firstHeadChild);
Element body = (Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "body");
body.setAttribute("dir", bundle.getLanguageDirection());
// With Caja enabled, onloads are triggered by features/caja/taming.js
if (!gadget.requiresCaja()) {
injectOnLoadHandlers(body);
}