Search in sources :

Example 1 with PrettyPrintWriter

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();
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) FileWriter(java.io.FileWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter)

Example 2 with PrettyPrintWriter

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();
        }
    }
}
Also used : PsiFragment(com.intellij.dupLocator.util.PsiFragment) FileWriter(java.io.FileWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter)

Example 3 with PrettyPrintWriter

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");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) IOException(java.io.IOException) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) Budget(jgnash.engine.budget.Budget) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 4 with PrettyPrintWriter

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;
}
Also used : QName(javax.xml.namespace.QName) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) XStreamException(com.thoughtworks.xstream.XStreamException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext)

Example 5 with PrettyPrintWriter

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);
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder)

Aggregations

PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)23 XStream (com.thoughtworks.xstream.XStream)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)7 StringWriter (java.io.StringWriter)7 IOException (java.io.IOException)6 Writer (java.io.Writer)6 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)5 Metacard (ddf.catalog.data.Metacard)4 FileOutputStream (java.io.FileOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 QuickWriter (com.thoughtworks.xstream.core.util.QuickWriter)3 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 FileWriter (java.io.FileWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)2