Package javax.xml.transform

Examples of javax.xml.transform.Transformer


        return new QName(namespceURI, localName);
    }

    public void generateXMLFile(Element element, Writer writer) {
        try {
            Transformer it = newTransformer();
           
            it.setOutputProperty(OutputKeys.METHOD, "xml");
            it.setOutputProperty(OutputKeys.INDENT, "yes");
            it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            it.transform(new DOMSource(element), new StreamResult(writer));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here


   * @throws TransformerConfigurationException
   */
  public Vector buildSite(String sBasePath, String sOutputPath, Properties oEnvironmentProps, Properties oUserProps)
      throws FileNotFoundException,IOException, DOMException, TransformerException, TransformerConfigurationException {

    Transformer oTransformer;
    StreamResult oStreamResult;
    StreamSource oStreamSrcXML;
    InputStream oXMLStream = null;
    String sMedia;
    Page oCurrentPage;
    long lElapsed = 0;

    final String sSep = System.getProperty("file.separator");

    if (DebugFile.trace) {
      lElapsed = System.currentTimeMillis();

      DebugFile.writeln("Begin PageSet.buildSite(" + sBasePath + "," + sOutputPath + "...)");
      DebugFile.incIdent();
    }

    oLastXcpt = null;

    if (!sBasePath.endsWith(sSep)) sBasePath += sSep;

    Vector vPages = pages();

    // Move to containers node
    if (DebugFile.trace)
      DebugFile.writeln("seekChildByName(,[Node], \"containers\")");

    Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers");

    if (oContainers==null) {
      if (DebugFile.trace)
        DebugFile.writeln("ERROR: <containers> node not found.");

      throw new DOMException(DOMException.NOT_FOUND_ERR, "<containers> node not found");
    }

    // Load XML data stream only once for all containers
    if (DebugFile.trace)
      DebugFile.writeln("oXMLStream = new FileInputStream(" + sURI + ")");

    // For each Page do XSL Transformation
    for (int c=0; c<vPages.size(); c++) {
      oCurrentPage = (Page) vPages.get(c);

      oXMLStream = new FileInputStream(sURI);
      oStreamSrcXML = new StreamSource(oXMLStream);

      // XSL Transformation
      try {
        if (DebugFile.trace)
          DebugFile.writeln("oTransformer = StylesheetCache.newTransformer(" + sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template() + ")");

        // Get a copy of Stylesheet from cache
        oTransformer = StylesheetCache.newTransformer(sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template());

        sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
        if (null==sMedia)
          sMedia = "html";
        else
          sMedia = sMedia.substring(sMedia.indexOf('/')+1);

        if (DebugFile.trace)
          DebugFile.writeln("Pages[" + String.valueOf(c) + "].filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")");

        oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia);

        if (DebugFile.trace)
          DebugFile.writeln("oStreamResult = new StreamResult(" + oCurrentPage.filePath() + ")");

        oStreamResult = new StreamResult(oCurrentPage.filePath());

        // Set environment parameters for stylesheet
        StylesheetCache.setParameters(oTransformer, oEnvironmentProps);

        // Set user defined parameters for stylesheet
        StylesheetCache.setParameters(oTransformer, oUserProps);

        // Realizar la transformación
        if (DebugFile.trace)
          DebugFile.writeln("oTransformer.transform(oStreamSrcXML, oStreamResult)");

        oTransformer.setParameter("param_page", ((Page)(vPages.get(c))).getTitle());
        oTransformer.transform(oStreamSrcXML, oStreamResult);

    if (!new File(oCurrentPage.filePath()).exists()) {
          if (DebugFile.trace) {
            DebugFile.writeln("FileNotFoundException: PageSet.buildSite() could not create file "+oCurrentPage.filePath());
            DebugFile.decIdent();
View Full Code Here

                               Properties oEnvironmentProps, Properties oUserProps)

    throws FileNotFoundException, IOException, DOMException, TransformerException,
           TransformerConfigurationException, MalformedURLException {

    Transformer oTransformer;
    StreamResult oStreamResult;
    StreamSource oStreamSrcXML;
    StringWriter oStrWritter;
    File oXMLFile,oXSLFile;
    InputStream oXMLStream = null;
    String sTransformed;
    StringBuffer oPostTransform;
    String sKey;
    String sMedia;
    String sXSLFile;
    Object sVal;
    Page oCurrentPage;

    int iCloseHead, iOpenBody, iCloseBody;
    int iReaded;
    char CharBuffer[] = new char[8192];
    String sCharBuffer;
    long lElapsed = 0;

    final String sSep = System.getProperty("file.separator");

    if (DebugFile.trace) {
      lElapsed = System.currentTimeMillis();

      DebugFile.writeln("Begin Pageset.buildPageForEdit(" + sBasePath + "," + sOutputPath + "," + sCtrlPath + "," + sMenuPath + ")");
      DebugFile.incIdent();
    }

    FileSystem oFS = new FileSystem();

    if (!sBasePath.endsWith(sSep)) sBasePath += sSep;

    String sWebServer = oEnvironmentProps.getProperty("webserver", "");

    if (DebugFile.trace && sWebServer.length()==0) DebugFile.writeln("WARNING: webserver property not set at EnvironmentProperties");

    if (!sWebServer.endsWith("/")) sWebServer+="/";


    // Posicionarse en el nodo de contenedores
    Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers");

    if (oContainers==null) {
      if (DebugFile.trace)
        DebugFile.writeln("ERROR: <containers> node not found.");

      throw new DOMException(DOMException.NOT_FOUND_ERR, "<containers> node not found");
    }

    // Cagar el stream de datos XML una sola vez
    if (DebugFile.trace)
      DebugFile.writeln("new FileInputStream(" + (sURI.startsWith("file://") ? sURI.substring(7) : sURI) + ")");

    // Para cada contenedor (página) realizar la transformación XSLT

      oCurrentPage = this.page(sPageGUID);

    oXMLFile = new File (sURI.startsWith("file://") ? sURI.substring(7) : sURI);
    if (!oXMLFile.exists()) {
        if (DebugFile.trace) DebugFile.decIdent();
      throw new FileNotFoundException("PageSet.buildPageForEdit() File not found "+sURI);
    }

      oXMLStream = new FileInputStream(oXMLFile);
      oStreamSrcXML = new StreamSource(oXMLStream);

      // Asignar cada stream de salida a su stream temporal
      oStrWritter = new StringWriter();
      oStreamResult = new StreamResult(oStrWritter);

      // Transformacion XSLT
      try {

        // Obtener la hoja de estilo desde el cache
        sXSLFile = sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template();
      oXSLFile = new File (sXSLFile);
      if (!oXSLFile.exists()) {
          if (DebugFile.trace) DebugFile.decIdent();
        throw new FileNotFoundException("PageSet.buildPageForEdit() File not found "+sXSLFile+" maybe there is a mismatch between the microsite name and the directory name where it is placed, or between the template name and the actual .xsl file name");
      }

        oTransformer = StylesheetCache.newTransformer(sXSLFile);

        sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE);

        if (DebugFile.trace) DebugFile.writeln(OutputKeys.MEDIA_TYPE + "=" + sMedia);

        if (null==sMedia)
          sMedia = "html";
        else
          sMedia = sMedia.substring(sMedia.indexOf('/')+1);

        if (null==oCurrentPage.getTitle())
          throw new NullPointerException("Page title is null");

        if (DebugFile.trace)
          DebugFile.writeln("Page.filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")");

        oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia);

        // Set environment parameters for stylesheet
        StylesheetCache.setParameters (oTransformer, oEnvironmentProps);

        // Set user defined parameters for stylesheet
        StylesheetCache.setParameters (oTransformer, oUserProps);

        // Paso el title de la pagina como parametro
        oTransformer.setParameter ("param_page", oCurrentPage.getTitle());

        // Realizar la transformación
        oTransformer.transform (oStreamSrcXML, oStreamResult);

      }
      catch (TransformerConfigurationException e) {
         oLastXcpt = e;
         sMedia = null;
View Full Code Here

                               Properties oEnvironmentProps, Properties oUserProps)

    throws IOException, DOMException, TransformerException,
           TransformerConfigurationException, MalformedURLException {

    Transformer oTransformer;
    StreamResult oStreamResult;
    StreamSource oStreamSrcXML;
    StringWriter oStrWritter;
    InputStream oXMLStream = null;
    String sTransformed;
    StringBuffer oPostTransform;
    String sKey;
    String sMedia;
    Object sVal;
    Page oCurrentPage;

    int iCloseHead, iOpenBody, iCloseBody;
    int iReaded;
    char CharBuffer[] = new char[8192];
    String sCharBuffer;
    long lElapsed = 0;

    final String sSep = System.getProperty("file.separator");

    if (DebugFile.trace) {
      lElapsed = System.currentTimeMillis();

      DebugFile.writeln("Begin Pageset.buildSiteForEdit(" + sBasePath + "," + sOutputPath + "," + sCtrlPath + "," + sMenuPath + ")");
      DebugFile.incIdent();
    }

    FileSystem oFS = new FileSystem();

    Vector vPages = pages();

    if (!sBasePath.endsWith(sSep)) sBasePath += sSep;

    String sWebServer = oEnvironmentProps.getProperty("webserver", "");

    if (DebugFile.trace && sWebServer.length()==0) DebugFile.writeln("WARNING: webserver property not set at EnvironmentProperties");

    if (!sWebServer.endsWith("/")) sWebServer+="/";


    // Posicionarse en el nodo de contenedores
    Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers");

    if (oContainers==null) {
      if (DebugFile.trace)
        DebugFile.writeln("ERROR: <containers> node not found.");

      throw new DOMException(DOMException.NOT_FOUND_ERR, "<containers> node not found");
    }

    // Cagar el stream de datos XML una sola vez
    if (DebugFile.trace)
      DebugFile.writeln("new FileInputStream(" + (sURI.startsWith("file://") ? sURI.substring(7) : sURI) + ")");


    // Para cada contenedor (página) realizar la transformación XSLT
    for (int c=0; c<vPages.size(); c++) {

      oCurrentPage = (Page) vPages.get(c);

      oXMLStream = new FileInputStream(sURI.startsWith("file://") ? sURI.substring(7) : sURI);
      oStreamSrcXML = new StreamSource(oXMLStream);

      // Asignar cada stream de salida a su stream temporal
      oStrWritter = new StringWriter();
      oStreamResult = new StreamResult(oStrWritter);

      // Transformacion XSLT
      try {

        // Obtener la hoja de estilo desde el cache
        oTransformer = StylesheetCache.newTransformer(sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template());

        sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE);

        if (DebugFile.trace) DebugFile.writeln(OutputKeys.MEDIA_TYPE + "=" + sMedia);

        if (null==sMedia)
          sMedia = "html";
        else
          sMedia = sMedia.substring(sMedia.indexOf('/')+1);

        if (null==oCurrentPage.getTitle())
          throw new NullPointerException("Page " + String.valueOf(c) + " title is null");

        if (DebugFile.trace)
          DebugFile.writeln("Page.filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")");

        oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia);

        // Set environment parameters for stylesheet
        StylesheetCache.setParameters (oTransformer, oEnvironmentProps);

        // Set user defined parameters for stylesheet
        StylesheetCache.setParameters (oTransformer, oUserProps);

        // Paso el title de la pagina como parametro
        oTransformer.setParameter ("param_page", ((Page)(vPages.get(c))).getTitle());

        // Realizar la transformación
        oTransformer.transform (oStreamSrcXML, oStreamResult);

      }
      catch (TransformerConfigurationException e) {
         oLastXcpt = e;
         sMedia = null;
View Full Code Here

      if(systemId != null)
          xslStreamSource = new StreamSource(new StringReader(xslString), systemId);
      else
          xslStreamSource = new StreamSource(new StringReader(xslString));

      Transformer transformer = transformerFactory.newTransformer(xslStreamSource);
      if(props != null) {
          Iterator iter = props.entrySet().iterator();
          while(iter.hasNext()) {
              Entry entry = (Entry)iter.next();
              transformer.setParameter((String)entry.getKey(), (String)entry.getValue());
          }
      }
      return transformer;
   }
View Full Code Here

  {
    String xmlData = null;

    if (doc != null)
    {
      Transformer serializer = getSerializer(systemID);
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();

      try
      {
        serializer.transform(new DOMSource(doc), new StreamResult(
            outStream));
        xmlData = outStream.toString(Constants.getXMLEncoding());
        outStream.close();

        // The serializer contains a bug and replaces DOS line breaks
View Full Code Here

   */
  static private Transformer getSerializer(String systemID) throws XException
  {
    setTransformerProperties();

    Transformer serializer;
    TransformerFactory tfactory = TransformerFactory.newInstance();
    // This creates a transformer that does a simple identity transform,
    // and thus can be used for all intents and purposes as a serializer.
    try
    {
      serializer = tfactory.newTransformer();
      serializer.setOutputProperty(OutputKeys.INDENT, "yes");
      serializer.setOutputProperty(OutputKeys.METHOD, "xml");
      serializer.setOutputProperty(OutputKeys.ENCODING, Constants
          .getXMLEncoding());
      if ((systemID != null) && (systemID.length() > 0))
      {
        serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
            systemID);
      }
    }
    catch (Throwable e)
    {
View Full Code Here

        return (Element)doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "usage")
            .item(0);
    }

    public void transform(InputStream stylesheet, OutputStream out) throws TransformerException {
        Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource(stylesheet));
        trans.transform(new DOMSource(doc), new StreamResult(out));
    }
View Full Code Here

        }

        // output the result document
        if (LOG.isLoggable(Level.FINE)) {
            try {
                Transformer serializer = TransformerFactory.newInstance()
                    .newTransformer(
                                    new StreamSource(Tool.class
                                        .getResourceAsStream("indent-no-xml-declaration.xsl")));

                serializer.transform(new DOMSource(resultDoc), new StreamResult(new PrintStream(System.out)));
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, "ERROR_SERIALIZE_COMMAND_MSG", ex);
            }
        }
View Full Code Here

   
   
    public static InputStream convertMessageToInputStream(Source src)
        throws IOException, TransformerConfigurationException, TransformerException {

        final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
       
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(baos);
        transformer.transform(src, result);
        LOG.finest("received message: " + new String(baos.toByteArray()));
       
        return new ByteArrayInputStream(baos.toByteArray());
    }
View Full Code Here

TOP

Related Classes of javax.xml.transform.Transformer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.