Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write to xml document #9299

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@
- [Added `Data.download` and a few other changes.][9249]
- [Implement Data Links to Postgres (accessing a DB connection or a table
directly)][9269]
- [Added `Xml_Document.write`][9299]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand Down Expand Up @@ -905,6 +906,7 @@
[9233]: https://github.com/enso-org/enso/pull/9233
[9249]: https://github.com/enso-org/enso/pull/9249
[9269]: https://github.com/enso-org/enso/pull/9269
[9299]: https://github.com/enso-org/enso/pull/9299

#### Enso Compiler

Expand Down
52 changes: 50 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/XML.enso
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import project.Data.Json.Extensions
import project.Data.Json.JS_Object
import project.Data.Map.Map
import project.Data.Numbers.Integer
import project.Data.Text.Encoding.Encoding
import project.Data.Text.Text
import project.Data.Vector.Vector
import project.Error.Error
import project.Errors.Common.Index_Out_Of_Bounds
import project.Errors.File_Error.File_Error
import project.Errors.Illegal_State.Illegal_State
import project.Errors.No_Such_Key.No_Such_Key
import project.Errors.Problem_Behavior.Problem_Behavior
import project.Nothing.Nothing
import project.Panic.Panic
import project.System.File.File
import project.System.File.File_Access.File_Access
import project.System.File.Generic.Writable_File.Writable_File
import project.System.File.Existing_File_Behavior.Existing_File_Behavior
import project.System.Input_Stream.Input_Stream
import project.System.File.Write_Extensions
from project.Data.Range.Extensions import all
from project.Data.Text.Extensions import all
from project.Metadata import make_single_choice, Widget
Expand Down Expand Up @@ -191,7 +196,7 @@ type XML_Document
outer_xml : Text ! XML_Error
outer_xml self =
XML_Error.handle_java_exceptions <|
XML_Utils.outerXML self.java_document
XML_Utils.outerXML self.java_document Boolean.False
AdRiley marked this conversation as resolved.
Show resolved Hide resolved

## Gets the raw XML of the document.

Expand All @@ -205,6 +210,49 @@ type XML_Document
XML_Error.handle_java_exceptions <|
XML_Utils.innerXML self.java_document

## GROUP Output
ICON data_output
Writes (or appends) the xml to the specified file using the supplied
encoding. The behavior specified in the `existing_file` parameter will be
used if the file exists.

Appending will probably not work as expected for XML documents, as it will
append after the root element, which is not valid XML.

Arguments:
- path: The path to the target file.
- encoding: The encoding to use when writing the file.
- on_existing_file: Specifies how to proceed if the file already exists.
- include_xml_declaration: Specifies whether to include the XML declaration
in the output. (e.g. `<?xml version="1.0" encoding="UTF-8"?>`)
- on_problems: Specifies how to handle any encountered problems.

If a character cannot be converted to a byte, an `Encoding_Error` is raised.
If `on_problems` is set to `Report_Warning` or `Ignore`, it is replaced with
a substitute (either '�' (if Unicode) or '?' depending on the encoding).
Otherwise, the process is aborted.
If the path to the parent location cannot be found or the filename is
invalid, a `File_Error.Not_Found` is raised.
If another error occurs, such as access denied, an `File_Error.IO_Error` is
raised.
Otherwise, the file is created with the encoded xlm written to it.

The method returns a `File` object for the written file.

? Dry Run

If writing to Output context is not enabled (such as in "Design" mode),
then this function will write to a temporary file. This temporary file will
be automatically deleted on exit of the Enso process.

This allows for building the workflow without affecting the real files.
@encoding Encoding.default_widget
write : Writable_File -> Encoding -> Existing_File_Behavior -> Boolean -> File
write self path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
declaration = if include_xml_declaration then '<?xml version=\"1.0\" encoding=\"' + encoding.character_set + '\"?>\n' else ""
XML_Error.handle_java_exceptions <|
(declaration + XML_Utils.outerXML self.java_document Boolean.True).write path encoding on_existing_file on_problems

AdRiley marked this conversation as resolved.
Show resolved Hide resolved
## GROUP Selections
Gets the child elements of an XML document.

Expand Down Expand Up @@ -480,7 +528,7 @@ type XML_Element
outer_xml : Text ! XML_Error
outer_xml self =
XML_Error.handle_java_exceptions <|
XML_Utils.outerXML self.java_element
XML_Utils.outerXML self.java_element Boolean.False
AdRiley marked this conversation as resolved.
Show resolved Hide resolved

## Gets the raw XML of the contents of the element, not including the
outermost tag and attributes.
Expand Down
5 changes: 4 additions & 1 deletion std-bits/base/src/main/java/org/enso/base/XML_Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ public class XML_Utils {
* @throws IllegalAccessException if the DOM implementation class cannot be accessed.
* @throws InstantiationException if the DOM implementation class cannot be instantiated.
*/
public static String outerXML(Node element)
public static String outerXML(Node element, boolean prettyPrint)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
DOMImplementationLS dom =
(DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
LSSerializer serializer = dom.createLSSerializer();
DOMConfiguration config = serializer.getDomConfig();
config.setParameter("xml-declaration", false);
if (prettyPrint) {
config.setParameter("format-pretty-print", Boolean.TRUE);
AdRiley marked this conversation as resolved.
Show resolved Hide resolved
}
serializer.setNewLine("\n");
return serializer.writeToString(element);
}
Expand Down
Loading