1

I'm trying to marshall an object and replace some invarvalid char's after that. In this processes, the completed xml is not getting generated. I can only see 1024 chars in all the generated files.

package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Scanner;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

public class MessageParserComponent {
    private static final Logger LOGGER = Logger.getLogger(MessageParserComponent.class);

           public File marshalIXml(final Object obj, final String xsdSchema,
        final String xmlFileName, final JAXBContext ctx) {
        File xml = new File(xmlFileName);

        try {
            xml.createNewFile();
            Marshaller marshaller = null;
            marshaller = ctx.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                "http://www.db.com/tf " + xsdSchema);
            marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
                new NamespacePrefixMapper() {
                    @Override
                    public String getPreferredPrefix(String arg0, String arg1,
                        boolean arg2) {
                        return "tf";
                    }
                });

            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                "http://www.db.com/tf " + xsdSchema);
            marshaller.setSchema(getSchema(xsdSchema));
            marshaller.marshal(obj, new StreamResult(xml));
            xml = replaceInvalidChar('\u0007', '\n', xml);
            xml = replaceInvalidString("ns2", "xsi", xml);
        } catch (IOException e) {
            LOGGER.error(e);
        } catch (JAXBException e) {
            LOGGER.error(e);
        } catch (SAXException e) {
            LOGGER.error(e);
        }
        return xml;
    }

  private Schema getSchema(String xsdSchema) throws SAXException {
        SchemaFactory fact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = fact.newSchema(this.getClass().getClassLoader()
                                           .getResource(xsdSchema));
        return schema;
    }

   private static File replaceInvalidString(String Target, String Dest,
        File Source) throws IOException {
        String xml_string;
        xml_string = new Scanner(Source).useDelimiter("\\Z").next();
        xml_string = xml_string.replace(Target, Dest);
        FileOutputStream fi = new FileOutputStream(Source);
        fi.write(xml_string.getBytes());
        return Source;
    }

    public static File replaceInvalidChar(char Target, char Dest, File Source)
        throws IOException {
        String xml_string;
        xml_string = new Scanner(Source).useDelimiter("\\Z").next();
        xml_string = xml_string.replace(Target, Dest);
        FileOutputStream fi = new FileOutputStream(Source);
        fi.write(xml_string.getBytes());
        return Source;
    }
}

Is there a limit for string replacement?
Am I creating the file in a wrong way?

Note:

I'm storing file in UNIX log folder
I have java 6, JAXB 2.2

Any sort of help is highly appreciated.

3
  • Maybe new Scanner(Source).useDelimiter("\\Z").next() returns only 1024 bytes of data. Do marshal into ByteArrayInputStream, then create String from it and call replace() method to do the replacement. Then save String into your file. Oct 23, 2013 at 18:11
  • Thank you @Stanislav Mamontov , But i feel it scans the input file and copies into string. Any way i'll check and get back to you.
    – Pavan Sky
    Oct 23, 2013 at 18:36
  • Saw the similar issue in other blogs, saying that Because a single read with “/z” as the delimiter should read everything until “end of input”, it’s tempting to just do a single read and leave it at that, as the examples listed above all do. In most cases that’s OK, but I’ve found at least one situation where reading to “end of input” doesn’t read the entire input – when the input is a SequenceInputStream, each of the constituent InputStreams appears to give a separate “end of input” of its own.
    – Pavan Sky
    Oct 23, 2013 at 19:44

3 Answers 3

0

Just check whether you annotated your object with jaxb annotations correctly. And why are you setting Marshaller properties? do you want to use external schema to marshall your object? instead why don't you let jaxb to take care of all those things (if your objects are available in same workspace).

This sample program may be helpful to you. http://www.mkyong.com/java/jaxb-hello-world-example/

and check this on too..

JAXB Marshaller : StringWriter output has a truncated tag value truncated-tag-value

1
  • We need a custom prefix for each element that other interface expects, which made to set the marshaller properties. Now comming to our issue, this is not because of marshaller, as @Stanislav Mamontov says, in different environments the scanner will behave in differnt ways, causing the xml which is completely generated to be truncated to 1024 char.
    – Pavan Sky
    Oct 23, 2013 at 19:42
0

When you open a FileOutputStream you are responsible for closing. You should change your code to include the close() call.

    FileOutputStream fi = new FileOutputStream(Source);
    fi.write(xml_string.getBytes());
    fi.close();
2
  • Thanks for the post reminder, i got it earlier itself and have closed the stream.
    – Pavan Sky
    Oct 23, 2013 at 19:47
  • @PavanSky - It should.
    – bdoughan
    Oct 23, 2013 at 20:07
0

The issue was with the scanner, which is trying to find the EOF and not spooling the entire file. This is not happening in local development environment. Where as in the UNIX server, where my application is deployed the "end of input" is different, which caused this issue.

File f1 = new File(path);
     StringBuilder f2 = new StringBuilder();
        Scanner scanner = new Scanner(f1).useDelimiter("\\Z");

        while (scanner.hasNext()) {
           f2.append(scanner.next());

This completed the trick. I do not know about the performance part of the scanner (taking only 1024 chars)when compared to other Buttered readers.

Any other solution which improves performance is highly appriciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.