use of javax.xml.transform.sax.TransformerHandler in project robovm by robovm.
the class TransformerFactoryImpl method newTransformerHandler.
/**
* Get a TransformerHandler object that can process SAX
* ContentHandler events into a Result, based on the Templates argument.
*
* @param templates The source of the transformation instructions.
*
* @return TransformerHandler ready to transform SAX events.
* @throws TransformerConfigurationException
*/
public TransformerHandler newTransformerHandler(Templates templates) throws TransformerConfigurationException {
try {
TransformerImpl transformer = (TransformerImpl) templates.newTransformer();
transformer.setURIResolver(m_uriResolver);
TransformerHandler th = (TransformerHandler) transformer.getInputContentHandler(true);
return th;
} catch (TransformerConfigurationException ex) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(ex);
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
}
throw ex;
}
}
use of javax.xml.transform.sax.TransformerHandler in project intellij-community by JetBrains.
the class GeneralToSMTRunnerEventsConvertorTest method testPreserveFullOutputAfterImport.
public void testPreserveFullOutputAfterImport() throws Exception {
mySuite.addChild(mySimpleTest);
for (int i = 0; i < 550; i++) {
String message = "line" + i + "\n";
mySimpleTest.addLast(printer -> printer.print(message, ConsoleViewContentType.NORMAL_OUTPUT));
}
mySimpleTest.setFinished();
mySuite.setFinished();
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = transformerFactory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
File output = FileUtil.createTempFile("output", "");
try {
FileUtilRt.createParentDirs(output);
handler.setResult(new StreamResult(new FileWriter(output)));
MockRuntimeConfiguration configuration = new MockRuntimeConfiguration(getProject());
TestResultsXmlFormatter.execute(mySuite, configuration, new SMTRunnerConsoleProperties(configuration, "framework", new DefaultRunExecutor()), handler);
String savedText = FileUtil.loadFile(output);
assertTrue(savedText.split("\n").length > 550);
myEventsProcessor.onStartTesting();
ImportedToGeneralTestEventsConverter.parseTestResults(() -> new StringReader(savedText), myEventsProcessor);
myEventsProcessor.onFinishTesting();
List<? extends SMTestProxy> children = myResultsViewer.getTestsRootNode().getChildren();
assertSize(1, children);
SMTestProxy testProxy = children.get(0);
MockPrinter mockPrinter = new MockPrinter();
testProxy.printOn(mockPrinter);
assertSize(550, mockPrinter.getAllOut().split("\n"));
} finally {
FileUtil.delete(output);
}
}
use of javax.xml.transform.sax.TransformerHandler in project adempiere by adempiere.
the class AbstractElementHandler method record_log.
/**
* Write results to log and records in history table
*
* @param success
* @param tableName
* @param objectType
* @param objectID
* @param objectStatus
* @throws SAXException
*
*/
public int record_log(Properties ctx, int success, String objectName, String objectType, int objectID, int objectIDBackup, String objectStatus, String tableName, int AD_Table_ID) throws SAXException {
StringBuffer recordLayout = new StringBuffer();
int id = 0;
TransformerHandler hd_document = getLogDocument(ctx);
AttributesImpl attsOut = new AttributesImpl();
String result = success == 1 ? "Success" : "Failure";
//hd_documemt.startElement("","","Successful",attsOut);
recordLayout.append("Type:").append(objectType).append(" - Name:").append(objectName).append(" - ID:").append(objectID).append(" - Action:").append(objectStatus).append(" - " + result);
hd_document.startElement("", "", result, attsOut);
hd_document.characters(recordLayout.toString().toCharArray(), 0, recordLayout.length());
hd_document.endElement("", "", result);
X_AD_Package_Imp_Detail detail = new X_AD_Package_Imp_Detail(ctx, 0, getTrxName(ctx));
detail.setAD_Package_Imp_ID(getPackageImpId(ctx));
detail.setAD_Org_ID(Env.getAD_Org_ID(ctx));
detail.setType(objectType);
detail.setName(objectName);
detail.setAction(objectStatus);
detail.setSuccess(result);
detail.setAD_Original_ID(objectID);
detail.setAD_Backup_ID(objectIDBackup);
detail.setTableName(tableName);
detail.setAD_Table_ID(AD_Table_ID);
if (!detail.save(getTrxName(ctx)))
log.info("Insert to import detail failed");
id = detail.get_ID();
return id;
}
use of javax.xml.transform.sax.TransformerHandler in project jackrabbit by apache.
the class EventJournalResourceImpl method spool.
@Override
public void spool(OutputContext outputContext) throws IOException {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
try {
outputContext.setContentType("application/atom+xml; charset=UTF-8");
outputContext.setProperty("Vary", "If-None-Match");
// TODO: Content-Encoding: gzip
// find out where to start
long prevts = -1;
String inm = request.getHeader("If-None-Match");
if (inm != null) {
// TODO: proper parsing when comma-delimited
inm = inm.trim();
if (inm.startsWith("\"") && inm.endsWith("\"")) {
String tmp = inm.substring(1, inm.length() - 1);
try {
prevts = Long.parseLong(tmp, 16);
journal.skipTo(prevts);
} catch (NumberFormatException ex) {
// broken etag
}
}
}
boolean hasPersistEvents = false;
if (outputContext.hasStream()) {
long lastts = -1;
long now = System.currentTimeMillis();
boolean done = false;
// collect events
List<Event> events = new ArrayList<Event>(MAXEV);
while (!done && journal.hasNext()) {
Event e = journal.nextEvent();
hasPersistEvents |= e.getType() == Event.PERSIST;
if (e.getDate() != lastts) {
// consider stopping
if (events.size() > MAXEV) {
done = true;
}
if (e.getDate() > now + MAXWAIT) {
done = true;
}
}
if (!done && (prevts == -1 || e.getDate() >= prevts)) {
events.add(e);
}
lastts = e.getDate();
}
if (lastts >= 0) {
// construct ETag from newest event
outputContext.setETag("\"" + Long.toHexString(lastts) + "\"");
}
OutputStream os = outputContext.getOutputStream();
StreamResult streamResult = new StreamResult(os);
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler th = tf.newTransformerHandler();
Transformer s = th.getTransformer();
s.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
s.setOutputProperty(OutputKeys.INDENT, "yes");
s.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
th.setResult(streamResult);
th.startDocument();
th.startElement(ATOMNS, FEED, FEED, NOATTRS);
writeAtomElement(th, TITLE, "EventJournal for " + getLocator().getWorkspaceName());
th.startElement(ATOMNS, AUTHOR, AUTHOR, NOATTRS);
writeAtomElement(th, NAME, "Jackrabbit Event Journal Feed Generator");
th.endElement(ATOMNS, AUTHOR, AUTHOR);
String id = getFullUri(request);
writeAtomElement(th, ID, id);
AttributesImpl linkattrs = new AttributesImpl();
linkattrs.addAttribute(null, "self", "self", "CDATA", id);
writeAtomElement(th, LINK, linkattrs, null);
cal.setTimeInMillis(lastts >= 0 ? lastts : now);
String upd = ISO8601.format(cal);
writeAtomElement(th, UPDATED, upd);
String lastDateString = "";
long lastTimeStamp = 0;
long index = 0;
AttributesImpl contentatt = new AttributesImpl();
contentatt.addAttribute(null, "type", "type", "CDATA", EVENTMEDIATYPE);
while (!events.isEmpty()) {
List<Event> bundle = null;
String path = null;
String op;
if (hasPersistEvents) {
bundle = new ArrayList<Event>();
Event e = null;
op = "operations";
do {
e = events.remove(0);
bundle.add(e);
// compute common path
if (path == null) {
path = e.getPath();
} else {
if (e.getPath() != null && e.getPath().length() < path.length()) {
path = e.getPath();
}
}
} while (e.getType() != Event.PERSIST && !events.isEmpty());
} else {
// no persist events
Event e = events.remove(0);
bundle = Collections.singletonList(e);
path = e.getPath();
op = EventUtil.getEventName(e.getType());
}
Event firstEvent = bundle.get(0);
String entryupd = lastDateString;
if (lastTimeStamp != firstEvent.getDate()) {
cal.setTimeInMillis(firstEvent.getDate());
entryupd = ISO8601.format(cal);
index = 0;
} else {
index += 1;
}
th.startElement(ATOMNS, ENTRY, ENTRY, NOATTRS);
String entrytitle = op + (path != null ? (": " + path) : "");
writeAtomElement(th, TITLE, entrytitle);
String entryid = id + "?type=journal&ts=" + Long.toHexString(firstEvent.getDate()) + "-" + index;
writeAtomElement(th, ID, entryid);
String author = firstEvent.getUserID() == null || firstEvent.getUserID().length() == 0 ? null : firstEvent.getUserID();
if (author != null) {
th.startElement(ATOMNS, AUTHOR, AUTHOR, NOATTRS);
writeAtomElement(th, NAME, author);
th.endElement(ATOMNS, AUTHOR, AUTHOR);
}
writeAtomElement(th, UPDATED, entryupd);
th.startElement(ATOMNS, CONTENT, CONTENT, contentatt);
for (Event e : bundle) {
// serialize the event
th.startElement(EVNS, E_EVENT, E_EVENT, NOATTRS);
// DAV:href
if (e.getPath() != null) {
boolean isCollection = (e.getType() == Event.NODE_ADDED || e.getType() == Event.NODE_REMOVED);
String href = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), e.getPath(), false).getHref(isCollection);
th.startElement(DavConstants.NAMESPACE.getURI(), DavConstants.XML_HREF, DavConstants.XML_HREF, NOATTRS);
th.characters(href.toCharArray(), 0, href.length());
th.endElement(DavConstants.NAMESPACE.getURI(), DavConstants.XML_HREF, DavConstants.XML_HREF);
}
// event type
String evname = EventUtil.getEventName(e.getType());
th.startElement(EVNS, E_EVENTTYPE, E_EVENTTYPE, NOATTRS);
th.startElement(EVNS, evname, evname, NOATTRS);
th.endElement(EVNS, evname, evname);
th.endElement(EVNS, E_EVENTTYPE, E_EVENTTYPE);
// date
writeObsElement(th, E_EVENTDATE, Long.toString(e.getDate()));
// user data
if (e.getUserData() != null && e.getUserData().length() > 0) {
writeObsElement(th, E_EVENTUSERDATA, firstEvent.getUserData());
}
// try to compute nodetype information
if (e instanceof AdditionalEventInfo) {
try {
Name pnt = ((AdditionalEventInfo) e).getPrimaryNodeTypeName();
if (pnt != null) {
writeObsElement(th, E_EVENTPRIMARNODETYPE, pnt.toString());
}
Set<Name> mixins = ((AdditionalEventInfo) e).getMixinTypeNames();
if (mixins != null) {
for (Name mixin : mixins) {
writeObsElement(th, E_EVENTMIXINNODETYPE, mixin.toString());
}
}
} catch (UnsupportedRepositoryOperationException ex) {
// optional
}
}
// identifier
if (e.getIdentifier() != null) {
writeObsElement(th, E_EVENTIDENTIFIER, e.getIdentifier());
}
// info
if (!e.getInfo().isEmpty()) {
th.startElement(EVNS, E_EVENTINFO, E_EVENTINFO, NOATTRS);
Map<?, ?> m = e.getInfo();
for (Map.Entry<?, ?> entry : m.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
String t = value != null ? value.toString() : null;
writeElement(th, null, key, NOATTRS, t);
}
th.endElement(EVNS, E_EVENTINFO, E_EVENTINFO);
}
th.endElement(EVNS, E_EVENT, E_EVENT);
lastTimeStamp = e.getDate();
lastDateString = entryupd;
}
th.endElement(ATOMNS, CONTENT, CONTENT);
th.endElement(ATOMNS, ENTRY, ENTRY);
}
th.endElement(ATOMNS, FEED, FEED);
th.endDocument();
os.flush();
}
} catch (Exception ex) {
throw new IOException("error generating feed: " + ex.getMessage());
}
}
use of javax.xml.transform.sax.TransformerHandler in project jackrabbit by apache.
the class ExportDocViewTest method doTestExportDocView.
/**
* Tests session.exportDocView with the different argument possibilities.
* The flag withHandler decides if the method requiring a ContentHandler as
* argument is called. The class org.apache.xml.serialize.XMLSerializer is
* taken as ContentHandler in this case. In both cases ( export with a
* ContentHandler and export with Stream) the test node is exported to the
* file defined in the setUp. This exported file is parsed using
* javax.xml.transform package and the receiving document is compared with
* the test node and its properties and child nodes in the repository.
*
* @param withHandler boolean, decides to call method requiring a
* ContentHandler as argument
* @param skipBinary
* @param noRecurse
*/
public void doTestExportDocView(boolean withHandler, boolean skipBinary, boolean noRecurse) throws RepositoryException, IOException, SAXException, TransformerException {
this.skipBinary = skipBinary;
this.noRecurse = noRecurse;
this.withHandler = withHandler;
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
try {
if (withHandler) {
SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = stf.newTransformerHandler();
th.setResult(new StreamResult(os));
session.exportDocumentView(testPath, th, skipBinary, noRecurse);
} else {
session.exportDocumentView(testPath, os, skipBinary, noRecurse);
}
} finally {
os.close();
}
// build the DOM tree
InputStream in = new BufferedInputStream(new FileInputStream(file));
doc = readDocument(in);
compareTree();
}
Aggregations