示例#1
0
  /**
   * Writes the XML Declaration and the opening RDF tag to the print Writer. Encoding attribute is
   * specified as the encoding argument.
   *
   * @param out PrintWriter
   * @throws IOException
   */
  protected void writeHeader(OutputStreamWriter out) throws IOException {

    // validate
    if (out != null) {

      // wrapper for output stream writer (enable autoflushing)
      PrintWriter writer = new PrintWriter(out, true);

      // get encoding from the Encoding map
      String encoding = EncodingMap.getJava2IANAMapping(out.getEncoding());

      // only insert encoding if there is a value
      if (encoding != null) {

        // print opening tags <?xml version="1.0" encoding=*encoding*?>
        writer.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
      } else {

        // print opening tags <?xml version="1.0"?>
        writer.println("<?xml version=\"1.0\"?>");
      }

      // print the Entities
      this.writeXMLEntities(writer);

      // print the opening RDF tag (including namespaces)
      this.writeRDFHeader(writer);
    } else {

      throw new IllegalArgumentException("Cannot write to null Writer.");
    }
  }
示例#2
0
 /**
  * Create the directory path for the file to be generated, construct FileOutputStream and
  * OutputStreamWriter depending upon docencoding.
  *
  * @param path The directory path to be created for this file.
  * @param filename File Name to which the PrintWriter will do the Output.
  * @param docencoding Encoding to be used for this file.
  * @exception IOException Exception raised by the FileWriter is passed on to next level.
  * @exception UnSupportedEncodingException Exception raised by the OutputStreamWriter is passed on
  *     to next level.
  * @return Writer Writer for the file getting generated.
  * @see java.io.FileOutputStream
  * @see java.io.OutputStreamWriter
  */
 public static Writer genWriter(
     Configuration configuration, String path, String filename, String docencoding)
     throws IOException, UnsupportedEncodingException {
   FileOutputStream fos;
   if (path != null) {
     DirectoryManager.createDirectory(configuration, path);
     fos = new FileOutputStream(((path.length() > 0) ? path + File.separator : "") + filename);
   } else {
     fos = new FileOutputStream(filename);
   }
   if (docencoding == null) {
     OutputStreamWriter oswriter = new OutputStreamWriter(fos);
     docencoding = oswriter.getEncoding();
     return oswriter;
   } else {
     return new OutputStreamWriter(fos, docencoding);
   }
 }