List of usage examples for org.w3c.dom Document createCDATASection
public CDATASection createCDATASection(String data) throws DOMException;
CDATASection
node whose value is the specified string. From source file:se.unlogic.hierarchy.core.servlets.CoreServlet.java
private void addBackgroundModuleResponses(List<BackgroundModuleResponse> backgroundModuleResponses, Document doc, User user, HttpServletRequest req) { if (backgroundModuleResponses != null) { Element backgroundsModuleResponsesElement = doc.createElement("backgroundsModuleResponses"); doc.getFirstChild().appendChild(backgroundsModuleResponsesElement); Document debugDoc;// ww w .j a va 2 s . c om // Write xml to file if debug enabled if (backgroundModuleXMLDebug && !StringUtils.isEmpty(this.backgroundModuleXMLDebugFile)) { debugDoc = XMLUtils.createDomDocument(); debugDoc.appendChild(debugDoc.createElement("BackgroundModuleDebug")); } else { debugDoc = null; } for (BackgroundModuleResponse moduleResponse : backgroundModuleResponses) { if (isValidResponse(moduleResponse)) { if (moduleResponse.getResponseType() == ResponseType.HTML) { // Append module html response Element responseElement = doc.createElement("response"); backgroundsModuleResponsesElement.appendChild(responseElement); Element htmlElement = doc.createElement("HTML"); responseElement.appendChild(htmlElement); htmlElement.appendChild(doc.createCDATASection(moduleResponse.getHtml())); this.appendSlots(moduleResponse, doc, responseElement); } else if (moduleResponse.getResponseType() == ResponseType.XML_FOR_CORE_TRANSFORMATION) { // Append module response Element responseElement = doc.createElement("response"); backgroundsModuleResponsesElement.appendChild(responseElement); Element xmlElement = doc.createElement("XML"); responseElement.appendChild(xmlElement); xmlElement.appendChild(doc.adoptNode(moduleResponse.getElement())); this.appendSlots(moduleResponse, doc, responseElement); } else if (moduleResponse.getResponseType() == ResponseType.XML_FOR_SEPARATE_TRANSFORMATION) { if (moduleResponse.getTransformer() != null) { //Append XML to debug document if (debugDoc != null) { this.log.debug("Background XML debug mode enabled, appending XML from module " + moduleResponse.getModuleDescriptor() + " to XML debug document"); try { Element documentElement = (Element) debugDoc .importNode(moduleResponse.getDocument().getDocumentElement(), true); if (moduleResponse.getModuleDescriptor() != null) { documentElement.setAttribute("moduleID", moduleResponse.getModuleDescriptor().getModuleID() + ""); documentElement.setAttribute("name", moduleResponse.getModuleDescriptor().getName()); } debugDoc.getDocumentElement().appendChild(documentElement); } catch (Exception e) { this.log.error("Error appending XML from module " + moduleResponse.getModuleDescriptor() + " to XML debug document", e); } } // Transform output try { StringWriter stringWriter = new StringWriter(); this.log.debug("Background module XML transformation starting"); XMLTransformer.transformToWriter(moduleResponse.getTransformer(), moduleResponse.getDocument(), stringWriter, encoding); this.log.debug( "Background module XML transformation finished, appending result..."); // Append module response Element responseElement = doc.createElement("response"); backgroundsModuleResponsesElement.appendChild(responseElement); Element htmlElement = doc.createElement("HTML"); responseElement.appendChild(htmlElement); htmlElement.appendChild(doc.createCDATASection(stringWriter.toString())); this.appendSlots(moduleResponse, doc, responseElement); this.log.debug("Result appended"); } catch (Exception e) { this.log.error("Tranformation of background module response from module" + moduleResponse.getModuleDescriptor() + " failed while processing request from user " + user + " accessing from " + req.getRemoteAddr(), e); } } else { this.log.error( "Background module response for separate transformation without attached stylesheet returned by module " + moduleResponse.getModuleDescriptor() + " while processing request from user " + user + " accessing from " + req.getRemoteAddr()); } } } } //Write background module XML debug to file if (debugDoc != null && debugDoc.getDocumentElement().hasChildNodes()) { log.debug("Writing background module XML debug to file " + backgroundModuleXMLDebugFile); try { XMLUtils.writeXMLFile(debugDoc, this.applicationFileSystemPath + "WEB-INF/" + backgroundModuleXMLDebugFile, true, encoding); } catch (Exception e) { log.error("Error writing background module XML debug to file " + backgroundModuleXMLDebugFile, e); } } } }
From source file:sos.scheduler.cron.CronConverter.java
private Element createExtension(final Document pobjDoc) throws DOMException, Exception { Element objExtensions = pobjDoc.createElement("extensions"); Element objExtension = pobjDoc.createElementNS("www.sos-berlin.com/schema/joe", "extension"); Element objGenerator = pobjDoc.createElement("generator"); objExtension.appendChild(objGenerator); objGenerator.setAttribute("name", "CronConverter"); objGenerator.setAttribute("date", SOSDate.getCurrentTimeAsString()); objGenerator.setAttribute("vendor", "www.sos-berlin.com"); Element objComments = pobjDoc.createElement("comment"); Node objCommentNode = pobjDoc.createCDATASection(lastComment); objComments.appendChild(objCommentNode); objExtension.appendChild(objComments); Element objDocu = pobjDoc.createElement("docu"); Node objDocuNode = pobjDoc.createCDATASection(strCronLine); objDocu.appendChild(objDocuNode);//from w w w. j a v a2 s .com objGenerator.appendChild(objDocu); objExtensions.appendChild(objExtension); return objExtensions; }
From source file:sos.scheduler.cron.CronConverter.java
public Document createJobElement(String cronline, final HashMap<String, String> environmentVariables) throws Exception { try {/*from w w w. j ava 2s .co m*/ logger.info("processing line: " + cronline); Document eleJob = docBuilder.newDocument(); Element jobElement = eleJob.createElement(conTagJOB); Matcher cronRegExAliasMatcher = cronRegExAliasPattern.matcher(cronline); if (cronRegExAliasMatcher.matches()) { logger.debug("Current line matches pattern " + cronRegExAlias); cronline = convertAlias(cronRegExAliasMatcher); } Matcher cronRegExMatcher = cronRegExPattern.matcher(cronline); int commandIndex = 6; if (isSystemCronTab()) { commandIndex = 7; cronRegExMatcher = cronRegExSystemPattern.matcher(cronline); } if (!cronRegExMatcher.matches()) { throw new JobSchedulerException("Fail to parse cron line \"" + cronline + "\""); } String jobname = getJobName(cronRegExMatcher.group(commandIndex)); jobElement.setAttribute(conAttributeNAME, jobname); if (isCreateJobChainJobs()) { jobElement.setAttribute(conAttributeORDER, "yes"); } else { jobElement.setAttribute(conAttributeORDER, "no"); } jobElement.setAttribute(conAttributeTITLE, "Cron Job " + cronRegExMatcher.group(commandIndex).trim()); //jobElement.setAttribute("replace", "yes"); if (timeout != null && !timeout.equals("0")) { jobElement.setAttribute(conOptionTIMEOUT, timeout); } String schedulerUser = ""; String command = cronRegExMatcher.group(commandIndex); if (isSystemCronTab()) { schedulerUser = cronRegExMatcher.group(6); command = (changeUserCommand + " " + command).trim(); } jobElement.appendChild(createExtension(eleJob)); if (isCreateJobChainJobs() == false) { logger.debug("Creating params element"); Element paramsElement = eleJob.createElement("params"); logger.debug("Creating param element (command)"); Element paramCommandElement = eleJob.createElement("param"); paramCommandElement.setAttribute("name", "command"); paramCommandElement.setAttribute("value", command); paramsElement.appendChild(paramCommandElement); jobElement.appendChild(paramsElement); } logger.debug("Creating script element"); Element scriptElement = eleJob.createElement(conTagSCRIPT); scriptElement.setAttribute(conAttributeLANGUAGE, "shell"); String script = conNewline; if (schedulerUser.length() > 0) { script += "export SCHEDULER_CRONTAB_USER=" + schedulerUser + conNewline; } Iterator<String> envIter = environmentVariables.keySet().iterator(); // set environment variables on job while (envIter.hasNext()) { String envName = envIter.next().toString(); String envValue = environmentVariables.get(envName).toString(); script += envName + "=" + envValue + conNewline; script += "export " + envName; } script += "echo created by " + conClassName + ", at " + SOSDate.getCurrentTimeAsString() + conNewline; if (isCreateAMock() == true) { script += "echo mock-mode: " + command + conNewline; if (strMockCommand.length() > 0) { script += strMockCommand + conNewline; } script += "exit 0" + conNewline; } else { script += command; } Node scriptData = eleJob.createCDATASection(script); scriptElement.appendChild(scriptData); jobElement.appendChild(scriptElement); if (isCreateJobChainJobs() == false) { Element runTimeElement = eleJob.createElement(conTagRUN_TIME); createRunTime(cronRegExMatcher, runTimeElement); if (usedNewRunTime && oldRunTime) { // workaround while <month> Element is not available // can later be deleted (keep only else branch) usedNewRunTime = false; Document runTimeDocument = docBuilder.newDocument(); runTimeDocument.appendChild(runTimeDocument.importNode(runTimeElement, true)); StringWriter out = new StringWriter(); OutputFormat format = new OutputFormat(runTimeDocument); format.setIndenting(true); format.setIndent(2); format.setOmitXMLDeclaration(true); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(runTimeDocument); Comment runTimeComment = eleJob .createComment("This run_time is currently not supported:\n" + out.toString()); jobElement.appendChild(runTimeComment); } else { jobElement.appendChild(runTimeElement); } } eleJob.appendChild(jobElement); return eleJob; } catch (Exception e) { throw new JobSchedulerException("Error occured creating job from cron line: " + cronline, e); } }
From source file:uk.ac.cam.caret.sakai.rwiki.component.model.impl.RWikiEntityImpl.java
/** * {@inheritDoc}/*from ww w . j a va 2 s.c om*/ */ public Element toXml(Document doc, Stack stack) { if (rwo == null) throw new RuntimeException(" Cant serialise containers at the moment "); Element wikipage = doc.createElement(SchemaNames.EL_WIKIPAGE); if (stack.isEmpty()) { doc.appendChild(wikipage); } else { ((Element) stack.peek()).appendChild(wikipage); } stack.push(wikipage); wikipage.setAttribute(SchemaNames.ATTR_ID, rwo.getId()); wikipage.setAttribute(SchemaNames.ATTR_PAGE_NAME, rwo.getName()); wikipage.setAttribute(SchemaNames.ATTR_REVISION, String.valueOf(rwo.getRevision())); wikipage.setAttribute(SchemaNames.ATTR_USER, rwo.getUser()); wikipage.setAttribute(SchemaNames.ATTR_OWNER, rwo.getOwner()); // I would like to be able to render this, but we cant... because its a // pojo ! getProperties().toXml(doc, stack); Element content = doc.createElement(SchemaNames.EL_WIKICONTENT); stack.push(content); wikipage.appendChild(content); content.setAttribute("enc", "BASE64"); try { String b64Content = Base64.encode(rwo.getContent().getBytes("UTF-8")); CDATASection t = doc.createCDATASection(b64Content); stack.push(t); content.appendChild(t); stack.pop(); } catch (UnsupportedEncodingException usex) { // if UTF-8 isnt available, we are in big trouble ! throw new IllegalStateException("Cannot find Encoding UTF-8"); } stack.pop(); stack.pop(); return wikipage; }