use of com.thoughtworks.xstream.XStream in project jgnash by ccavanaugh.
the class BinaryContainer method writeBinary.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing Binary file");
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
out.writeObject(list);
out.flush();
}
// forcibly flush before letting go of the resources to help older windows systems write correctly
os.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing Binary file complete");
}
use of com.thoughtworks.xstream.XStream in project jgnash by ccavanaugh.
the class XMLContainer method writeXML.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeXML(final Collection<StoredObject> objects, final Path path) {
Logger logger = Logger.getLogger(XMLContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing XML file");
try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.write("<?fileFormat " + Engine.CURRENT_MAJOR_VERSION + "." + Engine.CURRENT_MINOR_VERSION + "?>\n");
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new KXml2Driver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(new PrettyPrintWriter(writer))) {
out.writeObject(list);
// forcibly flush before letting go of the resources to help older windows systems write correctly
out.flush();
} catch (final Exception e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing XML file complete");
}
use of com.thoughtworks.xstream.XStream in project jgnash by ccavanaugh.
the class XMLContainer method readXML.
void readXML() {
// A file lock will be held on Windows OS when reading
try (final Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
readWriteLock.writeLock().lock();
final XStream xstream = configureXStream(new XStream(new StoredObjectReflectionProvider(objects), new KXml2Driver()));
// Filters out any java.sql.Dates that sneaked in when saving from a relational database
// and forces to a LocalDate
// TODO: Remove at a later date
xstream.alias("sql-date", LocalDate.class);
try (final ObjectInputStream in = xstream.createObjectInputStream(reader)) {
in.readObject();
}
} catch (final IOException | ClassNotFoundException e) {
Logger.getLogger(XMLContainer.class.getName()).log(Level.SEVERE, null, e);
} finally {
if (!acquireFileLock()) {
// lock the file on open
Logger.getLogger(XMLContainer.class.getName()).severe("Could not acquire the file lock");
}
readWriteLock.writeLock().unlock();
}
}
use of com.thoughtworks.xstream.XStream in project bamboobsc by billchen198318.
the class ApiRouteBuilder method configure.
@Override
public void configure() throws Exception {
this.from("servlet:///api").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String format = StringUtils.defaultString(exchange.getIn().getHeader("format", String.class)).trim().toLowerCase();
String visionOid = StringUtils.defaultString(exchange.getIn().getHeader("visionOid", String.class)).trim().toLowerCase();
String startDate = StringUtils.defaultString(exchange.getIn().getHeader("startDate", String.class)).trim().toLowerCase();
String endDate = StringUtils.defaultString(exchange.getIn().getHeader("endDate", String.class)).trim().toLowerCase();
String startYearDate = StringUtils.defaultString(exchange.getIn().getHeader("startYearDate", String.class)).trim().toLowerCase();
String endYearDate = StringUtils.defaultString(exchange.getIn().getHeader("endYearDate", String.class)).trim().toLowerCase();
String frequency = StringUtils.defaultString(exchange.getIn().getHeader("frequency", String.class)).trim().toLowerCase();
String dataFor = StringUtils.defaultString(exchange.getIn().getHeader("dataFor", String.class)).trim().toLowerCase();
String measureDataOrganizationOid = StringUtils.defaultString(exchange.getIn().getHeader("measureDataOrganizationOid", String.class)).trim().toLowerCase();
String measureDataEmployeeOid = StringUtils.defaultString(exchange.getIn().getHeader("measureDataEmployeeOid", String.class)).trim().toLowerCase();
String contentFlag = StringUtils.defaultString(exchange.getIn().getHeader("contentFlag", String.class)).trim().toUpperCase();
String responseContent = "";
// 請參考 gsbsc-web applicationContext-STANDARD-CXF.xml
ApiWebService apiWebService = (ApiWebService) AppContext.getBean("bsc.webservice.ApiWebService");
BscApiServiceResponse responseObj = apiWebService.getScorecard1(visionOid, startDate, endDate, startYearDate, endYearDate, frequency, dataFor, measureDataOrganizationOid, measureDataEmployeeOid, contentFlag);
if ("xml".equals(format)) {
// xml
XStream xstream = new XStream();
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("response", BscApiServiceResponse.class);
responseContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xstream.toXML(responseObj);
} else {
// json
ObjectMapper mapper = new ObjectMapper();
responseContent = mapper.writeValueAsString(responseObj);
}
exchange.getOut().setBody(responseContent);
}
}).to("stream:out");
}
use of com.thoughtworks.xstream.XStream in project restfulie-java by caelum.
the class OpenSearchTest method setUp.
@Before
public void setUp() {
xstream = new XStream();
xstream.registerConverter(new DefaultUrlConverter(Restfulie.custom()));
xstream.registerConverter(new DefaultTagsConveter());
xstream.processAnnotations(SearchDescription.class);
}
Aggregations