use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project intellij-community by JetBrains.
the class InspectionApplication method describeInspections.
private static void describeInspections(@NonNls String myOutputPath, final String name, final InspectionProfile profile) throws IOException {
final InspectionToolWrapper[] toolWrappers = profile.getInspectionTools(null);
final Map<String, Set<InspectionToolWrapper>> map = new HashMap<>();
for (InspectionToolWrapper toolWrapper : toolWrappers) {
final String groupName = toolWrapper.getGroupDisplayName();
Set<InspectionToolWrapper> groupInspections = map.get(groupName);
if (groupInspections == null) {
groupInspections = new HashSet<>();
map.put(groupName, groupInspections);
}
groupInspections.add(toolWrapper);
}
FileWriter fw = new FileWriter(myOutputPath);
try {
@NonNls final PrettyPrintWriter xmlWriter = new PrettyPrintWriter(fw);
xmlWriter.startNode(INSPECTIONS_NODE);
if (name != null) {
xmlWriter.addAttribute(PROFILE, name);
}
for (String groupName : map.keySet()) {
xmlWriter.startNode("group");
xmlWriter.addAttribute("name", groupName);
final Set<InspectionToolWrapper> entries = map.get(groupName);
for (InspectionToolWrapper toolWrapper : entries) {
xmlWriter.startNode("inspection");
final String shortName = toolWrapper.getShortName();
xmlWriter.addAttribute("shortName", shortName);
xmlWriter.addAttribute("displayName", toolWrapper.getDisplayName());
final boolean toolEnabled = profile.isToolEnabled(HighlightDisplayKey.find(shortName));
xmlWriter.addAttribute("enabled", Boolean.toString(toolEnabled));
final String description = toolWrapper.loadDescription();
if (description != null) {
xmlWriter.setValue(description);
} else {
LOG.error(shortName + " descriptionUrl==" + toolWrapper);
}
xmlWriter.endNode();
}
xmlWriter.endNode();
}
xmlWriter.endNode();
} finally {
fw.close();
}
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project intellij-community by JetBrains.
the class DuplocatorHashCallback method report.
@SuppressWarnings({ "HardCodedStringLiteral" })
public void report(String path, final Project project) throws IOException {
int[] hashCodes = myDuplicates.keys();
FileWriter fileWriter = null;
//fragments
try {
fileWriter = new FileWriter(path + File.separator + "fragments.xml");
PrettyPrintWriter writer = new PrettyPrintWriter(fileWriter);
writer.startNode("root");
for (int hash : hashCodes) {
List<List<PsiFragment>> dupList = myDuplicates.get(hash);
writer.startNode("hash");
writer.addAttribute("val", String.valueOf(hash));
for (final List<PsiFragment> psiFragments : dupList) {
writeFragments(psiFragments, writer, project, false);
}
writer.endNode();
}
//root node
writer.endNode();
writer.flush();
} finally {
if (fileWriter != null) {
fileWriter.close();
}
}
fileWriter = null;
//duplicates
try {
fileWriter = new FileWriter(path + File.separator + "duplicates.xml");
PrettyPrintWriter writer = new PrettyPrintWriter(fileWriter);
writer.startNode("root");
final DupInfo info = getInfo();
final int patterns = info.getPatterns();
for (int i = 0; i < patterns; i++) {
writer.startNode("duplicate");
writer.addAttribute("cost", String.valueOf(info.getPatternCost(i)));
writer.addAttribute("hash", String.valueOf(info.getHash(i)));
writeFragments(Arrays.asList(info.getFragmentOccurences(i)), writer, project, true);
writer.endNode();
}
//root node
writer.endNode();
writer.flush();
} finally {
if (fileWriter != null) {
fileWriter.close();
}
}
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter 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.io.xml.PrettyPrintWriter in project ddf by codice.
the class CswRecordConverter method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (StringUtils.isNotBlank(metacard.getMetadata())) {
// Check if the metadata is csw:Record
try {
StringReader xml = new StringReader(metacard.getMetadata());
XMLEventReader reader = factory.createXMLEventReader(xml);
boolean rootFound = false;
while (reader.hasNext() && !rootFound) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
rootFound = true;
QName name = event.asStartElement().getName();
if (StringUtils.equals(CswConstants.CSW_RECORD_LOCAL_NAME, name.getLocalPart()) && StringUtils.equals(CswConstants.CSW_OUTPUT_SCHEMA, name.getNamespaceURI())) {
return new BinaryContentImpl(IOUtils.toInputStream(metacard.getMetadata()), XML_MIME_TYPE);
}
}
}
} catch (Exception e) {
// Ignore and proceed with the transform.
}
}
StringWriter stringWriter = new StringWriter();
Boolean omitXmlDec = null;
if (arguments != null) {
omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
}
if (omitXmlDec == null || !omitXmlDec) {
stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
}
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
MarshallingContext context = new TreeMarshaller(writer, null, null);
context.put(CswConstants.WRITE_NAMESPACES, true);
copyArgumentsToContext(context, arguments);
this.marshal(metacard, writer, context);
BinaryContent transformedContent;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
transformedContent = new BinaryContentImpl(byteArrayInputStream, XML_MIME_TYPE);
return transformedContent;
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.
the class AbstractGmdTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
StringWriter stringWriter = new StringWriter();
Boolean omitXmlDec = null;
if (MapUtils.isNotEmpty(arguments)) {
omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
}
if (omitXmlDec == null || !omitXmlDec) {
stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
}
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
MarshallingContext context = new TreeMarshaller(writer, null, null);
copyArgumentsToContext(context, arguments);
converterSupplier.get().marshal(metacard, writer, context);
ByteArrayInputStream bais = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
return new BinaryContentImpl(bais, CswRecordConverter.XML_MIME_TYPE);
}
Aggregations