Were there any changes to gotoScreen(”screenName”) in Nifty 1.3? [Fixed]

I’m asking because now my “Option Screen” won’t load. I’m getting:



[java]

Sep 02, 2011 1:45:18 PM de.lessvoid.nifty.Nifty gotoScreenInternal

WARNING: screen [optionsScreen] not found

[/java]



When it’s obviously there…

[xml]

<screen id=“optionsScreen” controller=“com.madjack.games.sc.gui.nifty.Options”>

<layer id=“layer2” backgroundColor="#003f" childLayout=“center”>

[ STUFF ]

</layer>

</screen>

[/xml]



I have 2 screens defined in that XML… It was loading before (a long time ago, months I think, well before 1.3). I never noticed because I switched to something else and didn’t need to go there…



So, anyone could shed some light into this? I couldn’t find anything on the forums or on Nifty’s site so I’m wondering if it’s something on my side that I should be doing that has changed since it was working…



Thanks.

And now it seems validateXml(“xml/file”) will barf at nothing…



[java]

Sep 03, 2011 7:01:48 AM com.madjack.games.sc.gui.nifty.GuiManager loadStartMenu

SEVERE: null

org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element ‘interact’. One of ‘{“http://nifty-gui.sourceforge.net/nifty-1.3.xsd”:element}’ is expected.

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)

[…snipped…]

[/java]



when it’s clearly working when loaded… Oook. sigh

Seems there is some other quirk in your setup :wink:

Yeah, I should expect that kind of thing. Slightly getting used to it. :stuck_out_tongue:



What would be really nice is if we have a changelog with Nifty’s new version. But I couldn’t find that on sf. I posted on Void’s forums too, maybe I’ll get some answers.



While I wait for this I’m moving everything in that option screen to a new XML and see how that’ll go. :confused: A PITA though. :frowning:

I guess we’ll have to up to Nifty 1.3.1 before beta anyway…

Yeah.



The good news is that now that I’m almost done with the lens flare (good thing too before I got to the point to kill myself with a water gun), I’m moving back to pretty much where I was with Nifty. At that point I’ll finish the docs I’ve started (yeah, I know…). Void will be happy to know that. There were -no- changes in the Nifty’s wiki since I last edited it. :frowning:

The option screen loads fine when it’s in a different XML. groans

Sometimes I wish there was a DELETE ENTIRE THREAD button. :stuck_out_tongue:



It seems during testing I had inserted an invalid effect (actually the effect is valid, it’s just where it is that’s invalid, but nifty never complained about it) and effectively removing it made it work as it should.



So this is my bad.



But that brings an interesting point though. If there’s something invalid, shouldn’t Nifty simply discard it entirely? And by that I mean it shouldn’t take it into consideration at all. Or alternatively, it should simply “crash”, which would be my preferred choice.



In short this stopped the second screen to be “available”:



[xml]

<screen id=“firstScreen” …>

<layer …>

[…stuff…]

</layer>

<effect>

<onEndScreen name=“fade” start="#ff" end="#00" />

</effect>

</screen>



<screen id=“secondScreen” … >

[…etc…]

[/xml]

Isn’t this something that can be fixed by validating the XML first? There’s a DTD file (or similar) that can be used to check that all proper nifty tags are in place

Momoko_Fan said:
Isn't this something that can be fixed by validating the XML first? There's a DTD file (or similar) that can be used to check that all proper nifty tags are in place

The validator is the one that nifty uses.

[java]
nifty.validateXml("path/xml_file");
[/java]

If there is any error it throws an exception. But as I said, it says there's an error when it seems there isn't because the nifty screen works as it should. I think I remember void saying it was somehow broken... As for the XML itself, there are no errors in the editor, so... *shrug*

XML validation is using XSD (XML Schema Definition) but it is an optional step. Nifty will try to load invalid XML-files without complaining if it’s possible tho.



One should really take those validation exceptions serious :slight_smile:



The exception you’ve got from the nifty.validateXml() call:



[java]org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element ‘interact’. One of ‘{"<a href=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd":element” rel=“nofollow”>http://nifty-gui.sourceforge.net/nifty-1.3.xsd":element</a>}’ is expected.[/java]



is correct. It just says that you have an “interact” element somewhere where it expects a Nifty “element” tag.



However there are some errors that the XSD-Validation can’t catch. For instance custom effects can define their own effect parameter attributes which can’t be checked by the XSD.

@void256 wouldn’t it be possible to have some kind of switch or option or tag that would tell nifty “if you get an error/warning, just crash”?



I do understand the logic behind how it works right now, trying to load the XML file, discarding what it finds to be unsuitable, but honestly, give me a crash with what’s wrong in my file anytime than an Info.logger that could be very hard to find since nifty is pretty verbose.



Even with the logger at info and my problem above; Nifty never complained. It just loaded the screen happily but refused to load the aforementioned screen. It took me a while to find the problem and there was nothing in the log about it. With a crash, 2 mins and it’s fixed.



Anyway, that’s just my take on it.

I agree with @madjack on this. Nifty throws a lot of logging messages and finding a warning in there is like finding a needle in a haystack.

Honestly, if it were me doing Nifty, XML files had better be perfect. As far as I’m concerned, any program that relies on input files like XML should be very strict. If so much as a dot or a capital letter is wrong, it should just stop and tell you to fix it.



I just think Nifty is too forgiving. It’s not your job, or Nifty’s job, to repair the XML’s author errors.

Did you get an error/exception from the validateXml() call or did that one pass without any errors?



XML validation takes some time and is therefore an optional step, that’s the whole reasoning behind it. And since XSD is now supported in almost all XML editors you should have well formed and valid (according to the nifty.xsd) XML from the start, really.



Besides that I’m more than happy to apply a patch that adds more, maybe optional validation to the way XML is parsed using XPP…


Nifty throws a lot of logging messages and finding a warning in there is like finding a needle in a haystack.

I agree that Nifty is logging quite a lot but most of this should be able to shout down using standard JDK14 logging configuration. I usually log only warnings. I call something at the beginning of my application which works quite well:

[java]Logger.getLogger("de.lessvoid.nifty").setLevel(Level.WARNING);[/java]
void256 said:
Did you get an error/exception from the validateXml() call or did that one pass without any errors?

The validation is commented. I used to uncomment it when I had a weird result or a something was missing. But these days I don't bother because I have to comment out so many lines that it's just not worth it. I'll give you examples at the end.


And since XSD is now supported in almost all XML editors you should have well formed and valid (according to the nifty.xsd) XML from the start, really.

The editor in jMP is not giving any error, but it has some issues. So at this point when I get errors, I'm not sure if the editor can really help me or not.

Besides that I'm more than happy to apply a patch that adds more, maybe optional validation to the way XML is parsed using XPP...

I have to ask, why try to fix the user's input XML file? Why not simply crash and let user fix it?


I agree that Nifty is logging quite a lot but most of this should be able to shout down using standard JDK14 logging configuration. I usually log only warnings. I call something at the beginning of my application which works quite well:

[java]Logger.getLogger(&quot;de.lessvoid.nifty&quot;).setLevel(Level.WARNING);[/java]

Yeah, that's also a tool that can be used, but I don't want to have to add a line for each library or class I'm using, each with their different settings. I usually only enable Warning for all logger enabled libs.

As I said, the last problem didn't give a warning and that error stopped the screen following it to not work. It was quite a frustrating experience before I found the error. It was a simple thing (that I thought was valid, but wasn't).

---

Ok, here's the kind of "error" validateXml() gives me.
[java]
SEVERE: null
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'useStyles'. One of '{"http://nifty-gui.sourceforge.net/nifty-1.3.xsd":resourceBundle, "http://nifty-gui.sourceforge.net/nifty-1.3.xsd":style, "http://nifty-gui.sourceforge.net/nifty-1.3.xsd":controlDefinition, "http://nifty-gui.sourceforge.net/nifty-1.3.xsd":screen}' is expected.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:449)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3228)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1787)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:737)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:276)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:243)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:189)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:109)
at javax.xml.validation.Validator.validate(Validator.java:124)
at de.lessvoid.nifty.loaderv2.NiftyLoader.validate(NiftyLoader.java:85)
[/java]

The start of the screen's XML looks like this. I truncated it right before the first screen is declared.
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd">

<resourceBundle id="dialog" filename="com/madjack/games/sc/gui/nifty/dialog" />

<useStyles filename="nifty-default-styles.xml" />
<!-- Custom Styles and Controls -->
<useStyles filename="Interface/preview-panel.xml" />
<useStyles filename="Interface/preview-panel-style.xml" />
<useStyles filename="Interface/drag-panel-style.xml" />
<useStyles filename="Interface/gamePanel-style.xml" />
<useControls filename="Interface/gamePanel.xml" />
<!-- End of Custom Styles and Controls -->
<useControls filename="nifty-default-controls.xml" />

<controlDefinition name="draggablePanel" style="drag-panel-style" childRootId="#panel-content" controller="com.madjack.games.sc.gui.nifty.GameWindow"
revert="false" drop="false" handle="#panel-frame">
<interact onClick="dragStart()" onClickMouseMove="drag()" onRelease="dragStop()" />
<panel id="#panel-frame" style="#frame" childLayout="vertical" >
<panel id="#panel-bar" style="#bar" childLayout="horizontal">
<panel id="#panel-minimize-button" style="#minimize-button">
<interact onClick="minimizeWindow()" />
</panel>
</panel>
<panel id="#panel-content" style="#content" />
</panel>
</controlDefinition>

<!-- first screen starts here -->
[/xml]

My logic dictates that if validateXml() crashes like the above that means there's an error and nifty shouldn't load at all but crash. Since Nifty doesn't crash and the screen works at 100%, then validateXml() is faulty, or, as I said, Nifty is too forgiving. I don't know which it is in this case.

Also, if I want to correctly use validateXml only on the screen elements I won't be able to do so because the screen relies on those "useStyle" and "useControls" tags to render the screen. So it's a vicious circle. To do the verification, I'd have to have a redo that screen only using the base styles and controls and that's not something I'm willing to do.

When validateXml() crashes, your XML is invalid and you should stop your application. That’s all.



fromXml() will not validate the XML again because validation takes time. The logic is, that when you run your game in development mode you check the XML with validateXml() and take the additional loading time into account to be safe. When you finally ship your game and you are sure that all of your XML files are valid you would not call validateXml() and therefore benefit from a faster way of loading the XML files because the validation step is skipped.



Nifty does not to try to correct the XML it just expects the XML to be valid. If you feed the fromXml() method invalid XML data unexpected things are indeed happening but that’s just a side effect of not checking the XML twice.



If it makes you happy, then I can add some kind of flag to Niftys fromXml() methods that will simply call validateXml() internally. Then it will crash automatically like you’ve expected. Would that help to close this thread? o_O



The validation exception you’ve posted above is correct, since the order of elements really is important in the XML. According to the XSD:



[xml] <xs:complexType name=“niftyType”>

<xs:sequence>

<xs:element name=“useStyles” type=“useStylesType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“useControls” type=“useControlsType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“registerSound” type=“registerSoundType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“registerEffect” type=“registerEffectType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“registerMusic” type=“registerMusicType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“registerMouseCursor” type=“registerMouseCursorType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“resourceBundle” type=“resourceBundleType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“style” type=“styleType” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“controlDefinition” type=“controlDefinition” maxOccurs=“unbounded” minOccurs=“0” />

<xs:element name=“screen” type=“screenType” maxOccurs=“unbounded” minOccurs=“1” />

<xs:element name=“popup” type=“popupType” maxOccurs=“unbounded” minOccurs=“0” />

</xs:sequence>

</xs:complexType>[/xml]



the “useStyles” needs to be before “resourceBundle”. If you read the Exception it actually hints that some other elements are required instead of the “useStyles”.



Of course we could argue again if this is a reasonable thing to do or if Nifty should accept these elements no matter how they are ordered in the XML but at the moment this is simply the way it is… Sorry about that.

I think you’ve misunderstood my usage of validateXml(). I do not use it unless I’m getting weird or incorrect results and that was before my screen definitions were getting “complicated”. I’ll humbly admit that 90% of the time the expected result and what I get on the screen is pretty much identical. When I did use it, I didn’t even “play” the game, all I wanted was a result (either things are good or not) then I commented it out again.


the “useStyles” needs to be before “resourceBundle”. If you read the Exception it actually hints that some other elements are required instead of the “useStyles”.

Actually, the way I read the exception was: "One of those following elements (in the list) need to be inside useStyle." which didn't make any sense to me. Now I get the message.

As for the "resourceBundle" my logic into putting it first was: "What if one of those styles or controls had a default text linked to a 'translation'"? Thus its placement before anything else. Now that I know where it should properly be placed, it's all good.

Also @void256 please don't take any of these criticism personal. They are not attacks or whatever. :)

Have you read the post I made about onLostFocus? I would be really nice to be able to do validation with it as an "interact".
The thread is here.

I’ve read that thread. If you see that as a nice thing to have why don’t you create a feature request for that on the sf.net tracker? It would help a lot to keep track of things.

void256 said:
why don't you create a feature request for that on the sf.net tracker? It would help a lot to keep track of things.

Done. Created a feature request. (sf.net seems to have some problems... waiting for it to acknowledge the submit).

I don't think you want us to add feature requests for every little things we can think of, that's why I didn't create one in the first place as I was waiting for your input on this.