use of javax.xml.transform.TransformerException in project felix by apache.
the class XSLTProcessor method processHttpException.
protected void processHttpException(HttpInputStream in, HttpOutputStream out, HttpException e) throws IOException {
out.setCode(e.getCode());
out.setHeader("Content-Type", "text/html");
out.sendHeaders();
// hardcoded dir :-P
Transformer transformer = createTransformer("error.xsl");
transformer.setURIResolver(this);
Document doc = e.getResponseDoc();
if (doc != null) {
try {
if (!in.getVariables().containsKey("locale")) {
transformer.setParameter("request.locale", locale.toString());
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc), new StreamResult(output));
output.writeTo(out);
} catch (TransformerException ex) {
XSLTProcessor.log(LogService.LOG_ERROR, "Exception during error output", ex);
}
}
}
use of javax.xml.transform.TransformerException in project felix by apache.
the class ComponentDescriptorIO method read.
public static List<ClassDescription> read(final InputStream file, final ClassLoader classLoader, final IssueLog iLog, final String location) throws SCRDescriptorException {
try {
final XmlHandler xmlHandler = new XmlHandler(classLoader, iLog, location);
IOUtils.parse(file, xmlHandler);
return xmlHandler.components;
} catch (final TransformerException e) {
throw new SCRDescriptorException("Unable to read xml", location, e);
}
}
use of javax.xml.transform.TransformerException in project felix by apache.
the class MetaTypeIO method write.
/**
* Generate the xml top level element and start streaming
* the meta data.
* @param metaData
* @param contentHandler
* @throws SAXException
*/
private static void write(final DescriptionContainer metaData, final List<ComponentContainer> components, final File file, final String localization) throws SCRDescriptorException {
final String namespace = detectMetatypeVersion(metaData);
try {
FileOutputStream fos = new FileOutputStream(file);
try {
final ContentHandler contentHandler = IOUtils.getSerializer(fos);
contentHandler.startDocument();
contentHandler.startPrefixMapping(PREFIX, namespace);
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, "localization", localization);
contentHandler.startElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME, ai);
IOUtils.newline(contentHandler);
for (final ComponentContainer comp : components) {
if (comp.getMetatypeContainer() != null) {
generateOCDXML(comp.getMetatypeContainer(), contentHandler);
generateDesignateXML(comp.getMetatypeContainer(), contentHandler);
}
}
// end wrapper element
contentHandler.endElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME);
IOUtils.newline(contentHandler);
contentHandler.endPrefixMapping(PREFIX);
contentHandler.endDocument();
} finally {
fos.close();
}
} catch (final IOException e) {
throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
} catch (final TransformerException e) {
throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
} catch (final SAXException e) {
throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
}
}
use of javax.xml.transform.TransformerException in project felix by apache.
the class ObrCleanRepo method writeToFile.
/**
* write a Node in a xml file.
*
* @param outputFilename URI to the output file
* @param treeToBeWrite Node root of the tree to be write in file
* @throws MojoExecutionException if the plugin failed
*/
private void writeToFile(URI outputFilename, Node treeToBeWrite) throws MojoExecutionException {
// init the transformer
Transformer transformer = null;
TransformerFactory tfabrique = TransformerFactory.newInstance();
try {
transformer = tfabrique.newTransformer();
} catch (TransformerConfigurationException e) {
getLog().error("Unable to write to file: " + outputFilename.toString());
throw new MojoExecutionException("Unable to write to file: " + outputFilename.toString() + " : " + e.getMessage());
}
Properties proprietes = new Properties();
proprietes.put("method", "xml");
proprietes.put("version", "1.0");
proprietes.put("encoding", "ISO-8859-1");
proprietes.put("standalone", "yes");
proprietes.put("indent", "yes");
proprietes.put("omit-xml-declaration", "no");
transformer.setOutputProperties(proprietes);
DOMSource input = new DOMSource(treeToBeWrite);
File fichier = null;
FileOutputStream flux = null;
try {
fichier = File.createTempFile("repository", ".xml");
flux = new FileOutputStream(fichier);
} catch (IOException e) {
getLog().error("Unable to write to file: " + fichier.getName());
throw new MojoExecutionException("Unable to write to file: " + fichier.getName() + " : " + e.getMessage());
}
Result output = new StreamResult(flux);
try {
transformer.transform(input, output);
} catch (TransformerException e) {
throw new MojoExecutionException("Unable to write to file: " + outputFilename.toString() + " : " + e.getMessage());
}
try {
flux.flush();
flux.close();
FileUtils.rename(fichier, new File(outputFilename));
} catch (IOException e) {
throw new MojoExecutionException("IOException when closing file : " + e.getMessage());
}
}
use of javax.xml.transform.TransformerException in project hale by halestudio.
the class InspireInstanceWriter method writeAdditionalElements.
/**
* @see StreamGmlWriter#writeAdditionalElements(XMLStreamWriter,
* TypeDefinition, IOReporter)
*/
@Override
protected void writeAdditionalElements(XMLStreamWriter writer, TypeDefinition containerDefinition, IOReporter reporter) throws XMLStreamException {
super.writeAdditionalElements(writer, containerDefinition, reporter);
// determine INSPIRE identifier and metadata names
Path<Definition<?>> localIdPath = new DefinitionAccessor(containerDefinition).findChildren("identifier").findChildren("Identifier").findChildren("localId").eval(false);
QName identifierName = localIdPath.getElements().get(1).getName();
Definition<?> internalIdentifierDef = localIdPath.getElements().get(2);
QName internalIdentifierName = internalIdentifierDef.getName();
QName localIdName = localIdPath.getElements().get(3).getName();
Path<Definition<?>> namespacePath = new DefinitionAccessor(internalIdentifierDef).findChildren("namespace").eval(false);
QName namespaceName = namespacePath.getElements().get(1).getName();
Path<Definition<?>> metadataPath = new DefinitionAccessor(containerDefinition).findChildren("metadata").eval(false);
QName metadataName = metadataPath.getElements().get(1).getName();
// write INSPIRE identifier
writer.writeStartElement(identifierName.getNamespaceURI(), identifierName.getLocalPart());
writer.writeStartElement(internalIdentifierName.getNamespaceURI(), internalIdentifierName.getLocalPart());
writer.writeStartElement(localIdName.getNamespaceURI(), localIdName.getLocalPart());
writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_LOCALID).as(String.class, ""));
writer.writeEndElement();
writer.writeStartElement(namespaceName.getNamespaceURI(), namespaceName.getLocalPart());
writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_NAMESPACE).as(String.class, ""));
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndElement();
// write metadata
writer.writeStartElement(metadataName.getNamespaceURI(), metadataName.getLocalPart());
// retrieve metadata element (if any)
Element metadataElement = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_DOM).as(Element.class);
// metadata from file (if any)
if (metadataElement == null) {
String metadataFile = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_FILE).as(String.class);
if (metadataFile != null && !metadataFile.isEmpty()) {
try (InputStream input = new BufferedInputStream(new FileInputStream(new File(metadataFile)))) {
metadataElement = findMetadata(input, reporter);
} catch (IOException e) {
reporter.warn(new IOMessageImpl("Could not load specified metadata file.", e));
}
}
}
if (metadataElement != null) {
try {
writeElement(metadataElement, writer);
} catch (TransformerException e) {
reporter.warn(new IOMessageImpl("Couldn't include specified metadata file.", e));
}
} else {
writer.writeAttribute(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "true");
}
writer.writeEndElement();
}
Aggregations