public void write(Document doc, OutputStream outputStream) throws IOException { OutputFormat format = new OutputFormat(doc); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(outputStream, format); serializer.asDOMSerializer(); serializer.serialize(doc); }
private void appendFormERDF(Writer writer) throws IOException { String name = context.getForm().getAttributes().get("name"); if (name == null) name = ""; String head = ""; if (context.getForm().getHead() != null) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(); serializer.setOutputByteStream(stream); serializer.asDOMSerializer(); serializer.setNamespaces(true); serializer.serialize(context.getForm().getHead()); head = StringEscapeUtils.escapeXml(stream.toString()); } catch (IOException e) { e.printStackTrace(); } } String nsDeclarations = ""; for (String key : context.getForm().getNSDeclarations().keySet()) { nsDeclarations += "[" + key + "," + context.getForm().getNSDeclarations().get(key) + "]"; } writer.append("<div id=\"" + context.getForm().getResourceId() + "\" class=\"-oryx-canvas\">"); appendOryxField(writer, "type", STENCILSET_URI + "#" + context.getForm().getStencilId()); appendXFormsField(writer, "id", ""); appendXFormsField(writer, "name", name); appendXFormsField(writer, "version", ""); appendXFormsField(writer, "head", head); appendXFormsField(writer, "nsdeclarations", nsDeclarations); appendOryxField(writer, "mode", "writable"); appendOryxField(writer, "mode", "fullscreen"); writer.append( "<a rel=\"oryx-stencilset\" href=\"/oryx/stencilsets/xforms/xforms.json\"/>"); // TODO: HACK // TO MAKE IT // WORK FOR // NOW for (String id : context.getResourceIds()) { writer.append("<a rel=\"oryx-render\" href=\"#" + id + "\"/>"); } writer.append("</div>"); }
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); try { if (config == null) { config = new PropertiesConfiguration("pnengine.properties"); } } catch (ConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String postVariable = config.getString("pnengine.post_variable"); String engineURL = config.getString("pnengine.url") + "/petrinets"; String engineURL_host = config.getString("pnengine.url"); String modelURL = config.getString("pnengine.default_model_url"); String formURL = null; String bindingsURL = null; String rdf = req.getParameter("data"); String diagramTitle = req.getParameter("title"); DocumentBuilder builder; BPMNDiagram diagram; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); Document document = builder.parse(new ByteArrayInputStream(rdf.getBytes())); BPMNRDFImporter importer = new BPMNRDFImporter(document); diagram = (BPMNDiagram) importer.loadBPMN(); String basefilename = String.valueOf(System.currentTimeMillis()); String tmpPNMLFile = this.getServletContext().getRealPath("/") + "tmp" + File.separator + basefilename + ".pnml"; BufferedWriter out1 = new BufferedWriter(new FileWriter(tmpPNMLFile)); // URL only for testing purposes... ExecConverter converter = new ExecConverter(diagram, modelURL, this.getServletContext().getRealPath("/")); converter.setBaseFileName( this.getServletContext().getRealPath("/") + "tmp" + File.separator + basefilename); PetriNet net = converter.convert(); ExecPetriNet execnet = (ExecPetriNet) net; Document pnmlDoc = builder.newDocument(); ExecPNPNMLExporter exp = new ExecPNPNMLExporter(); execnet.setName(diagramTitle); exp.savePetriNet(pnmlDoc, execnet); OutputFormat format = new OutputFormat(pnmlDoc); XMLSerializer serial = new XMLSerializer(out1, format); serial.asDOMSerializer(); serial.serialize(pnmlDoc.getDocumentElement()); out1.close(); StringWriter stringOut = new StringWriter(); XMLSerializer serial2 = new XMLSerializer(stringOut, format); serial2.asDOMSerializer(); serial2.serialize(pnmlDoc.getDocumentElement()); URL url_engine = new URL(engineURL); HttpURLConnection connection_engine = (HttpURLConnection) url_engine.openConnection(); connection_engine.setRequestMethod("POST"); String encoding = new sun.misc.BASE64Encoder().encode("testuser:"******"Authorization", "Basic " + encoding); connection_engine.setUseCaches(false); connection_engine.setDoInput(true); connection_engine.setDoOutput(true); String escaped_content = postVariable + "=" + URLEncoder.encode(stringOut.toString(), "UTF-8"); connection_engine.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection_engine.setRequestProperty( "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // connection_engine.setRequestProperty("Content-Length", // ""+escaped_content.getBytes().length); String errorMessage = null; try { connection_engine.getOutputStream().write(escaped_content.getBytes()); connection_engine.connect(); } catch (ConnectException e) { errorMessage = e.getMessage(); } // output link address res.getWriter().print("tmp/" + basefilename + ".pnml" + "\">View PNML</a><br/><br/>"); if (errorMessage == null && connection_engine.getResponseCode() == 200) { res.getWriter() .println( "Deployment to Engine <a href=\"" + engineURL_host + "/worklist\" target=\"_blank\">" + engineURL_host + "</a><br/><b>successful</b>!"); } else { res.getWriter() .println( "Deployment to Engine <a href=\"" + engineURL + "\" target=\"_blank\">" + engineURL + "</a><br/><b>failed</b> with message: \n" + errorMessage + "!"); } } catch (ParserConfigurationException e1) { res.getWriter().println(e1.getMessage()); } catch (SAXException e1) { res.getWriter().println(e1.getMessage()); } }