diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/xml/XMLNew.java b/src/main/java/ortus/boxlang/runtime/bifs/global/xml/XMLNew.java new file mode 100644 index 000000000..4511e1a10 --- /dev/null +++ b/src/main/java/ortus/boxlang/runtime/bifs/global/xml/XMLNew.java @@ -0,0 +1,57 @@ + +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ortus.boxlang.runtime.bifs.global.xml; + +import ortus.boxlang.runtime.bifs.BIF; +import ortus.boxlang.runtime.bifs.BoxBIF; +import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.scopes.ArgumentsScope; +import ortus.boxlang.runtime.scopes.Key; +import ortus.boxlang.runtime.types.Argument; +import ortus.boxlang.runtime.types.XML; + +@BoxBIF + +public class XMLNew extends BIF { + + /** + * Constructor + */ + public XMLNew() { + super(); + declaredArguments = new Argument[] { + new Argument( true, "boolean", Key.caseSensitive, false ) + }; + } + + /** + * Creates a new empty XML Object + * + * @param context The context in which the BIF is being invoked. + * @param arguments Argument scope for the BIF. + * + * @argument.caseSensitive Whether the identifiers in the XML document ( e.g. dot notation ) are case sensitive + */ + public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { + Boolean caseSensitive = arguments.getAsBoolean( Key.caseSensitive ); + return new XML( caseSensitive ); + } + +} diff --git a/src/main/java/ortus/boxlang/runtime/types/XML.java b/src/main/java/ortus/boxlang/runtime/types/XML.java index 528433f2e..f04b43f4c 100644 --- a/src/main/java/ortus/boxlang/runtime/types/XML.java +++ b/src/main/java/ortus/boxlang/runtime/types/XML.java @@ -79,6 +79,11 @@ public class XML implements Serializable, IStruct { */ public BoxMeta $bx; + /** + * The type of struct ( private so that the interface method `getType` will be used ) + */ + private final TYPES type; + /** * Function service */ @@ -105,6 +110,8 @@ public class XML implements Serializable, IStruct { */ public XML( String xmlData ) { + this.type = TYPES.DEFAULT; + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { @@ -126,7 +133,15 @@ public XML( String xmlData ) { * Create a new XML Document from the given string */ public XML( Node node ) { - this.node = node; + this.type = TYPES.DEFAULT; + this.node = node; + } + + /** + * Create a new XML Document from the given string + */ + public XML( Boolean caseSenstive ) { + this.type = caseSenstive ? TYPES.CASE_SENSITIVE : TYPES.DEFAULT; } /** @@ -152,12 +167,14 @@ public String getXMLText() { * @return the element children */ public List getXMLChildrenAsList() { - List children = new ArrayList(); - NodeList childNodes = node.getChildNodes(); - for ( int i = 0; i < childNodes.getLength(); i++ ) { - Node child = childNodes.item( i ); - if ( child.getNodeType() == Node.ELEMENT_NODE ) { - children.add( new XML( child ) ); + List children = new ArrayList(); + if ( node != null ) { + NodeList childNodes = node.getChildNodes(); + for ( int i = 0; i < childNodes.getLength(); i++ ) { + Node child = childNodes.item( i ); + if ( child.getNodeType() == Node.ELEMENT_NODE ) { + children.add( new XML( child ) ); + } } } return children; @@ -668,6 +685,11 @@ public String toStringWithCase() { return toString(); } + @Override + public String toString() { + return asString(); + } + @Override public IStruct.TYPES getType() { return IStruct.TYPES.DEFAULT; @@ -675,7 +697,7 @@ public IStruct.TYPES getType() { @Override public Boolean isCaseSensitive() { - return false; + return type.equals( TYPES.CASE_SENSITIVE ); } @Override diff --git a/src/test/java/ortus/boxlang/runtime/bifs/global/xml/XMLNewTest.java b/src/test/java/ortus/boxlang/runtime/bifs/global/xml/XMLNewTest.java new file mode 100644 index 000000000..e12165db9 --- /dev/null +++ b/src/test/java/ortus/boxlang/runtime/bifs/global/xml/XMLNewTest.java @@ -0,0 +1,71 @@ + +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ortus.boxlang.runtime.bifs.global.xml; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import ortus.boxlang.runtime.BoxRuntime; +import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.context.ScriptingRequestBoxContext; +import ortus.boxlang.runtime.dynamic.casters.XMLCaster; +import ortus.boxlang.runtime.scopes.IScope; +import ortus.boxlang.runtime.scopes.Key; +import ortus.boxlang.runtime.scopes.VariablesScope; +import ortus.boxlang.runtime.types.XML; + +public class XMLNewTest { + + static BoxRuntime instance; + IBoxContext context; + IScope variables; + static Key result = new Key( "result" ); + + @BeforeAll + public static void setUp() { + instance = BoxRuntime.getInstance( true ); + } + + @AfterAll + public static void teardown() { + } + + @BeforeEach + public void setupEach() { + context = new ScriptingRequestBoxContext( instance.getRuntimeContext() ); + variables = context.getScopeNearby( VariablesScope.name ); + } + + @DisplayName( "It tests the BIF XMLNew" ) + @Test + public void testBif() { + XML xmlObject = XMLCaster.cast( instance.executeStatement( "XMLNew()" ) ); + assertTrue( StringUtils.contains( xmlObject.toString(), "" ) ); + assertEquals( xmlObject.size(), 0 ); + } + +}