Add write to xml document (#9299)

* First commit

* Add xml.write

* Add comment

* Changelog.md

* Code review changes

* Code review changes

* Update import
This commit is contained in:
AdRiley 2024-03-06 17:13:28 +00:00 committed by GitHub
parent acf124e089
commit 3ebf1340e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View File

@ -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
@ -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

View File

@ -1,9 +1,9 @@
import project.Any.Any
import project.Data.Boolean.Boolean
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
@ -11,11 +11,16 @@ 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.Boolean import Boolean, False, True
from project.Data.Range.Extensions import all
from project.Data.Text.Extensions import all
from project.Metadata import make_single_choice, Widget
@ -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 False
## Gets the raw XML of the document.
@ -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 '<27>' (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 True).write path encoding on_existing_file on_problems
## GROUP Selections
Gets the child elements of an XML document.
@ -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 False
## Gets the raw XML of the contents of the element, not including the
outermost tag and attributes.

View File

@ -23,13 +23,14 @@ 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);
config.setParameter("format-pretty-print", prettyPrint);
serializer.setNewLine("\n");
return serializer.writeToString(element);
}