-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
8u152
-
generic
-
generic
FULL PRODUCT VERSION :
XCode
ADDITIONAL OS VERSION INFORMATION :
MacOC Sierra (latest)
A DESCRIPTION OF THE PROBLEM :
Hi,
(1) your http://java.sun.com/xml/jaxp/properties/schemaLanguage is corrupt;
(2) IMHO, your XSD-file handling is buggy: the XSD-file is put at a default location without the possibility of changing the location.
Best,
Simon
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public interface XML {
public static final String namespaceURL =
"http://www.ti.bfh.ch/fileadmin/modules/BTI7055-de.xml";
public default void writeXMLdoc (String xmldocfilepath, String xsdfilepath) {
try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
namespaceURL+" "+xsdfilepath);
marshaller.marshal(this, new File(xmldocfilepath));
//marshaller.marshal(this, System.out);
writeXSD(xsdfilepath);
} catch (Exception e) { e.printStackTrace(); }
}
public default void writeXSD (String xsdfilepath) {
class LocalFileSchemaResolver extends SchemaOutputResolver {
// by the way, this is an inner (local) class ;-)
private File f;
LocalFileSchemaResolver (File f) { this.f = f; }
public Result createOutput (String namespaceURI,
String fileName) throws IOException {
StreamResult sr = new StreamResult(
new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8"));
sr.setSystemId(f.toURI().toURL().toString());
return sr;
}
}
try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
context.generateSchema(new LocalFileSchemaResolver(new File(xsdfilepath)));
} catch (Exception e) { e.printStackTrace(); }
}
public static Object xmldoc2object (String xmldocfilepath, Class objectclass) {
Object object = null;
try {
JAXBContext context = JAXBContext.newInstance(objectclass);
Unmarshaller unmarshaller = context.createUnmarshaller();
object = objectclass.cast(unmarshaller.unmarshal(
new BufferedReader(
new InputStreamReader(
new FileInputStream(xmldocfilepath), "UTF-8"))));
} catch (Exception e) { e.printStackTrace(); }
return object;
}
}
=========================================================
package xml;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
public class XMLDOMParsing {
private Document xmldom = null;
public Document getDOM () { return xmldom; }
public XMLDOMParsing (String xmlfilepathstr) {
String xmldoc = file2str(xmlfilepathstr);
xmldom = XML2DOM(xmldoc);
}
private String file2str (String filepathstr) {
String str = "";
try {
str = new String(Files.readAllBytes(Paths.get(filepathstr)),
StandardCharsets.UTF_8);
} catch (Exception e) { e.printStackTrace(); }
return str;
}
private Document XML2DOM (String xmldoc) {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
// START: required for XML-documents with an attributed XML-schema
//final String JAXP_SCHEMA_LANGUAGE =
// "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
//final String W3C_XML_SCHEMA =
// "http://www.w3.org/2001/XMLSchema";
//dbf.setNamespaceAware(true);
//dbf.setValidating(true);
//dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
// END: required for XML-documents with an attributed XML-schema
Document xmldom = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new SimpleErrorHandler());
InputSource src = new InputSource(new StringReader(xmldoc));
src.setEncoding("UTF-8"); // StandardCharsets.UTF_8 doesn't work
xmldom = db.parse(src);
} catch (Exception e) { e.printStackTrace(); }
return xmldom;
}
private class SimpleErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
}
}
---------- END SOURCE ----------
XCode
ADDITIONAL OS VERSION INFORMATION :
MacOC Sierra (latest)
A DESCRIPTION OF THE PROBLEM :
Hi,
(1) your http://java.sun.com/xml/jaxp/properties/schemaLanguage is corrupt;
(2) IMHO, your XSD-file handling is buggy: the XSD-file is put at a default location without the possibility of changing the location.
Best,
Simon
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public interface XML {
public static final String namespaceURL =
"http://www.ti.bfh.ch/fileadmin/modules/BTI7055-de.xml";
public default void writeXMLdoc (String xmldocfilepath, String xsdfilepath) {
try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
namespaceURL+" "+xsdfilepath);
marshaller.marshal(this, new File(xmldocfilepath));
//marshaller.marshal(this, System.out);
writeXSD(xsdfilepath);
} catch (Exception e) { e.printStackTrace(); }
}
public default void writeXSD (String xsdfilepath) {
class LocalFileSchemaResolver extends SchemaOutputResolver {
// by the way, this is an inner (local) class ;-)
private File f;
LocalFileSchemaResolver (File f) { this.f = f; }
public Result createOutput (String namespaceURI,
String fileName) throws IOException {
StreamResult sr = new StreamResult(
new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8"));
sr.setSystemId(f.toURI().toURL().toString());
return sr;
}
}
try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
context.generateSchema(new LocalFileSchemaResolver(new File(xsdfilepath)));
} catch (Exception e) { e.printStackTrace(); }
}
public static Object xmldoc2object (String xmldocfilepath, Class objectclass) {
Object object = null;
try {
JAXBContext context = JAXBContext.newInstance(objectclass);
Unmarshaller unmarshaller = context.createUnmarshaller();
object = objectclass.cast(unmarshaller.unmarshal(
new BufferedReader(
new InputStreamReader(
new FileInputStream(xmldocfilepath), "UTF-8"))));
} catch (Exception e) { e.printStackTrace(); }
return object;
}
}
=========================================================
package xml;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
public class XMLDOMParsing {
private Document xmldom = null;
public Document getDOM () { return xmldom; }
public XMLDOMParsing (String xmlfilepathstr) {
String xmldoc = file2str(xmlfilepathstr);
xmldom = XML2DOM(xmldoc);
}
private String file2str (String filepathstr) {
String str = "";
try {
str = new String(Files.readAllBytes(Paths.get(filepathstr)),
StandardCharsets.UTF_8);
} catch (Exception e) { e.printStackTrace(); }
return str;
}
private Document XML2DOM (String xmldoc) {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
// START: required for XML-documents with an attributed XML-schema
//final String JAXP_SCHEMA_LANGUAGE =
// "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
//final String W3C_XML_SCHEMA =
// "http://www.w3.org/2001/XMLSchema";
//dbf.setNamespaceAware(true);
//dbf.setValidating(true);
//dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
// END: required for XML-documents with an attributed XML-schema
Document xmldom = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new SimpleErrorHandler());
InputSource src = new InputSource(new StringReader(xmldoc));
src.setEncoding("UTF-8"); // StandardCharsets.UTF_8 doesn't work
xmldom = db.parse(src);
} catch (Exception e) { e.printStackTrace(); }
return xmldom;
}
private class SimpleErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
}
}
---------- END SOURCE ----------