use of javax.xml.transform.stream.StreamSource in project qi4j-sdk by Qi4j.
the class QuikitServlet method init.
@Override
public void init(ServletConfig config) throws ServletException {
try {
mountPoints = new TreeMap<String, Page>();
documentFactory = DocumentBuilderFactory.newInstance();
documentFactory.setNamespaceAware(true);
ClassLoader cl = getClass().getClassLoader();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Source[] schemaSources = new Source[2];
schemaSources[0] = new StreamSource(cl.getResourceAsStream("xhtml1-strict.xsd"));
schemaSources[1] = new StreamSource(cl.getResourceAsStream("xml.xsd"));
Schema schema = schemaFactory.newSchema(schemaSources);
documentFactory.setSchema(schema);
ApplicationAssembler assembler = createApplicationAssembler(config);
Energy4Java qi4j = new Energy4Java();
application = qi4j.newApplication(assembler);
application.activate();
Module module = application.findModule("WebLayer", "PagesModule");
finder = module;
if (application.mode() == Application.Mode.development) {
DataInitializer initializer = module.newTransient(DataInitializer.class);
initializer.initialize();
}
Iterable<ServiceReference<Page>> iterable = finder.findServices(Page.class);
for (ServiceReference<Page> page : iterable) {
PageMetaInfo pageMetaInfo = page.metaInfo(PageMetaInfo.class);
String mountPoint = pageMetaInfo.mountPoint();
mountPoints.put(mountPoint, page.get());
}
} catch (Exception e) {
throw new ServletException("Can not initialize Qi4j.", e);
}
}
use of javax.xml.transform.stream.StreamSource in project Mycat-Server by MyCATApache.
the class XmlProcessBase method baseParseXmlToBean.
/**
* 默认转换将指定的xml转化为
* 方法描述
* @param inputStream
* @param fileName
* @return
* @throws JAXBException
* @throws XMLStreamException
* @创建日期 2016年9月16日
*/
public Object baseParseXmlToBean(String fileName) throws JAXBException, XMLStreamException {
// 搜索当前转化的文件
InputStream inputStream = XmlProcessBase.class.getResourceAsStream(fileName);
// 如果能够搜索到文件
if (inputStream != null) {
// 进行文件反序列化信息
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xmlRead = xif.createXMLStreamReader(new StreamSource(inputStream));
return unmarshaller.unmarshal(xmlRead);
}
return null;
}
use of javax.xml.transform.stream.StreamSource in project OpenAttestation by OpenAttestation.
the class ReadXmlTest method createValidator.
@BeforeClass
public static void createValidator() throws Exception {
InputStream xsd = ReadXmlTest.class.getResourceAsStream("/jaxb/mtwilson-tag-selection/mtwilson-tag-selection.xsd");
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsd));
validator = schema.newValidator();
} catch (Exception e) {
throw e;
}
}
use of javax.xml.transform.stream.StreamSource in project OpenAttestation by OpenAttestation.
the class ConverterUtil method formateXMLString.
public static String formateXMLString(String inputXML) {
StreamResult xmlOutput = null;
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StringWriter stringWriter = new StringWriter();
xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
} catch (Exception e) {
// simple exception handling, please review it
throw new RuntimeException(e);
}
return xmlOutput.getWriter().toString();
}
use of javax.xml.transform.stream.StreamSource in project OpenAttestation by OpenAttestation.
the class JAXB method read.
/**
* Does not allow XML External Entity (XXE) injection CWE-611
* http://cwe.mitre.org/data/definitions/611.html
*
* @param <T>
* @param document
* @param valueType
* @return
* @throws IOException
* @throws JAXBException
*/
public <T> T read(String document, Class<T> valueType) throws IOException, JAXBException, XMLStreamException {
JAXBContext jc = getContextForType(valueType);
// CWE-611 restrict XML external entity references
XMLInputFactory xif = XMLInputFactory.newFactory();
// if true allows sender to include external files via entity declaration in the DTD, which is a security vulnerability
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
// if true allows sender to declare a DTD, and the DTD spec has security vulnerabilities so a reference implementation cannot be secure
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
// if true allows sender to encode > < " & and ' but not custom-defined entity references because we disable dtd support ; http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new StringReader(document)));
Unmarshaller u = jc.createUnmarshaller();
JAXBElement<T> doc = u.unmarshal(xsr, valueType);
return doc.getValue();
}
Aggregations