Пример #1
0
 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);
 }
  public void buildSettings(
      Settings settings, File output, JProgressPane progress, String versionNumber)
      throws IOException {

    IIOMetadataNode settingsNode = new IIOMetadataNode("settings");

    // create settings
    progress.update("Saving window settings...", 20);
    settingsNode.appendChild(buildWindow(settings));
    progress.update("Saving file settings...", 40);
    settingsNode.appendChild(buildFiles(settings));
    progress.update("Saving options...", 60);
    settingsNode.appendChild(buildOptions(settings, versionNumber));
    progress.update("Saving display settings...", 80);
    settingsNode.appendChild(buildTableDisplay(settings));

    OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);
    of.setIndent(1);
    of.setIndenting(true);

    progress.update("Writing to file...", 90);

    FileOutputStream fos = new FileOutputStream(output);
    XMLSerializer serializer = new XMLSerializer(fos, of);
    serializer.serialize(settingsNode);
    fos.flush();
    fos.close();
  }
Пример #3
0
 @Override
 public ServerResponse getMenuXml() {
   ServerResponse response = new ServerResponse();
   try {
     File file = new File(MetadataFacade.METADATA_URL + "menu\\Menu.xml");
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(file);
     doc.getDocumentElement().normalize();
     OutputFormat format = new OutputFormat(doc);
     StringWriter stringOut = new StringWriter();
     XMLSerializer serial = new XMLSerializer(stringOut, format);
     serial.serialize(doc);
     response.setSeverity(ServerResponse.INFO);
     response.setResponseCode("Metadata.EntityLoadSuccessful");
     response.setData(stringOut.toString());
     LogUtil.log("metadata Menu loaded", Level.INFO, null);
   } catch (ParserConfigurationException e) {
     response.setSeverity(ServerResponse.ERROR);
     response.setResponseCode("Metadata.EntityLoadFailed");
     LogUtil.log("metadata Menu load failed", Level.SEVERE, e);
   } catch (SAXException e) {
     response.setSeverity(ServerResponse.ERROR);
     response.setResponseCode("Metadata.EntityLoadFailed");
     LogUtil.log("metadata Menu load failed", Level.SEVERE, e);
   } catch (IOException e) {
     response.setSeverity(ServerResponse.ERROR);
     response.setResponseCode("Metadata.EntityLoadFailed");
     LogUtil.log("metadata Menu load failed", Level.SEVERE, e);
   }
   return response;
 }
Пример #4
0
 private void createFile(File file, Document doc) {
   try {
     FileOutputStream _fileOutput = new FileOutputStream(file);
     XMLSerializer _xmlS = new XMLSerializer(_fileOutput, new OutputFormat(doc, "UTF-8", true));
     _xmlS.serialize(doc);
     _fileOutput.close();
     JOptionPane.showMessageDialog(null, "Insert new customer into XML successfully!!!");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #5
0
 /**
  * 设置属性
  *
  * @param os
  * @return
  */
 private XMLSerializer getXMLSerializer(OutputStream os) {
   OutputFormat of = new OutputFormat();
   formatCDataTag();
   of.setCDataElements(cdataNode);
   of.setPreserveSpace(true);
   of.setIndenting(true);
   of.setOmitXMLDeclaration(true);
   XMLSerializer serializer = new XMLSerializer(of);
   serializer.setOutputByteStream(os);
   return serializer;
 }
Пример #6
0
 private void endpointDoesNotExist(HttpServletResponse response)
     throws IOException, ServletException {
   response.setStatus(HttpServletResponse.SC_NOT_FOUND);
   XMLSerializer serializer =
       new XMLSerializer(
           response.getWriter(),
           new OutputFormat(Method.XML, OutputFormat.Defaults.Encoding, true));
   try {
     serializer.serialize(createError("NOT_FOUND", "Specified endpoint does not exist."));
   } catch (ParserConfigurationException e) {
     throw new ServletException(e);
   }
 }
Пример #7
0
 public static String formatDoc(Document document) {
   try {
     OutputFormat format = new OutputFormat(document);
     format.setLineWidth(65);
     format.setIndenting(true);
     format.setIndent(2);
     Writer out = new StringWriter();
     XMLSerializer serializer = new XMLSerializer(out, format);
     serializer.serialize(document);
     System.out.println(out.toString());
     return out.toString();
   } catch (IOException e) {
     log.error("XmlWorks::formatDoc()", e);
     throw new RuntimeException(e);
   }
 }
Пример #8
0
  public static String toXML(Document document) throws TransformerException, IOException {

    try {
      OutputFormat format = new OutputFormat(document);
      format.setLineWidth(65);
      format.setIndenting(true);
      format.setIndent(2);
      Writer out = new StringWriter();
      XMLSerializer serializer = new XMLSerializer(out, format);
      serializer.serialize(document);

      return out.toString();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public String format(String unformattedXml) {
    try {
      final Document document = parseXmlFile(unformattedXml);

      OutputFormat format = new OutputFormat(document);
      format.setLineWidth(65);
      format.setIndenting(true);
      format.setIndent(2);
      Writer out = new StringWriter();
      XMLSerializer serializer = new XMLSerializer(out, format);
      serializer.serialize(document);

      return out.toString();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  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>");
  }
Пример #11
0
 /**
  * 转为xml串
  *
  * @param obj
  * @return
  */
 public String toXML(Object obj) {
   String result = null;
   try {
     JAXBContext context = JAXBContext.newInstance(obj.getClass());
     Marshaller m = context.createMarshaller();
     m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     m.setProperty(Marshaller.JAXB_FRAGMENT, true); // 去掉报文头
     OutputStream os = new ByteOutputStream();
     XMLSerializer serializer = getXMLSerializer(os);
     m.marshal(obj, serializer.asContentHandler());
     result = os.toString();
   } catch (Exception e) {
     e.printStackTrace();
   }
   Logger.info("response text:" + result);
   return result;
 }
Пример #12
0
  // <schema name='SOME_SCHEMA'>
  //     <table name='ACCOUNT'>
  //         <column name='ID' type='char' size='4' nullable='false'/>
  //         <column name='NAME' type='varchar' size='20' nullable='false'/>
  //         <column name='SOME_DATE' type='date' nullable='false'/>
  //         <column name='SOME_NUMBER' type='number' precision='12' scale='0' nullable='false'/>
  //         <column name='SOME_DECIMAL' type='number' precision='10' scale='2' nullable='false'/>
  //
  //         <primary-key>
  //             <column name='ID'/>
  //         </primary-key>
  //     </table>
  //
  //     <stored-proc name='DO_SOMETHING'>
  //         <parameter name='ID' type='char' size='4' nullable='false'/>
  //         <parameter name='NAME' type='varchar' size='20' nullable='false'/>
  //     </stored-proc>
  // </schema>
  private void outputSchemaXml(final Catalog catalog, final Schema schema) {
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    try {
      db = dbf.newDocumentBuilder();
    } catch (final ParserConfigurationException e) {
      ThreadContext.throwFault(e);
    }
    final Document dom = db.newDocument();

    final Element eRoot = dom.createElement("schema");
    eRoot.setAttribute("name", m_options.m_schemaName);
    dom.appendChild(eRoot);
    eRoot.appendChild(
        dom.createComment(
            String.format(
                " This file is generated by Breakpoint tool DbAnalyse from [%s] ",
                m_options.m_optionsFilename)));

    createTableElements(dom, catalog, schema, eRoot);
    createStoredProcedureElements(dom, catalog, eRoot);

    final OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);
    format.setIndent(4);
    format.setLineWidth(150);

    XMLSerializer serializer = null;
    try {
      serializer =
          new XMLSerializer(new FileOutputStream(new File(m_options.m_outputFilename)), format);
    } catch (final FileNotFoundException e) {
      ThreadContext.throwFault(e);
    }

    try {
      serializer.serialize(dom);
    } catch (final IOException e) {
      ThreadContext.throwFault(e);
    }
  }
  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());
    }
  }