/*
 * #%L
 * ACS AEM Commons Bundle
 * %%
 * Copyright (C) 2013 Adobe
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.adobe.acs.commons.rewriter.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.sling.rewriter.Generator;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

public final class XMLParserGenerator implements Generator {

    private final StringWriter writer;

    private final SAXParser saxParser;

    private ContentHandler contentHandler;

    public XMLParserGenerator() throws ParserConfigurationException, SAXException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);

        saxParser = factory.newSAXParser();
        this.writer = new StringWriter();
    }

    public void finished() throws IOException, SAXException {
        this.saxParser.parse(new ByteArrayInputStream(this.writer.toString().getBytes("UTF-8")),
                new ContentHandlerAdapter(contentHandler));
    }

    public PrintWriter getWriter() {
        return new PrintWriter(writer);
    }

    public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
        // nothing to do
    }


    public void setContentHandler(ContentHandler handler) {
        this.contentHandler = handler;
    }

    public void dispose() {
        // nothing to do
    }

}

@@ -688,45 +688,8 @@
 l;%0A%0A
-import java.io.ByteArrayInputStream;%0A
 impo
@@ -736,24 +736,53 @@
 rintWriter;%0A
+import java.io.StringReader;%0A
 import java.
@@ -1128,24 +1128,56 @@
 entHandler;%0A
+import org.xml.sax.InputSource;%0A
 import org.x
@@ -1296,24 +1296,68 @@
 er writer;%0A%0A
+    private final PrintWriter printWriter;%0A%0A
     private 
@@ -1703,24 +1703,76 @@
 ngWriter();%0A
+        this.printWriter = new PrintWriter(writer);%0A
     %7D%0A%0A    p
@@ -1845,151 +1845,371 @@
 his.
-saxParser.parse(new ByteArrayInputStream(this.writer.toString().getBytes(%22UTF-8%22)),%0A                new ContentHandlerAdapter(contentHandler));
+printWriter.flush();%0A        final ContentHandlerAdapter handler = new ContentHandlerAdapter(contentHandler);%0A        final String documentString = this.writer.toString();%0A        if (!documentString.isEmpty()) %7B%0A            final InputSource source = new InputSource(new StringReader(documentString));%0A            this.saxParser.parse(source, handler);%0A        %7D
 %0A   
@@ -2264,21 +2264,17 @@
  return 
-new P
+p
 rintWrit
@@ -2271,32 +2271,24 @@
  printWriter
-(writer)
 ;%0A    %7D%0A%0A   
@@ -2426,17 +2426,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

better handle empty string case