Is there a way to manage repetitive image path in fxml? - java

I'm wondering what is the better way to mangage path in my fxml file ?
For example, I have many fxml files with :
Is there a way to store /ressources/images/ somewhere and do something like this :
<Image url="#MY_DEFINE_PATH/success_128.png" />
where MY_DEFINE_PATH come from a global file somewhere in my project ?

You could embed your own DTD entity definition:
<?xml ... ?>
<!DOCTYPE GridPane [
<!ENTITY imgPath "/ressources/images/">
]>
<GridPane>
...
<Image url="&imgPath;success_128.png" />
You could also link to an external DTD, but then one should use an XML catalog (DTDs in local cache mapping from their URLs) to speed up processing of the XML.
Using an external DTD file
<!DOCTYPE GridPane [
<!ENTITY imgPath "/mypaths.dtd">
]>
mypaths.dtd:
<!ENTITY imgPath "/ressources/images/">

Related

How to define XML entities in Java using Dom4J?

I'm trying to add entity declarations to my XML document in Java using Dom4J 2.1.1, but can't figure out how to do so, or if it's even possible. Can anyone help please?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TEAMS_ASSET_FILE PUBLIC "-//TEAMS//DTD asset and link file//EN" "D:\Apps\data\Tasset.dtd" [
<!ENTITY asset0000001 SYSTEM "Z:\somepath\1234\myfile.pdf">
<!ENTITY asset0000002 SYSTEM "Z:\anotherpath\5678\another.pdf">
]>
<content>
...
</content>
see DocumentType-Interface [https://dom4j.github.io/javadoc/2.1.1/org/dom4j/DocumentType.html]. Get it by using
DocumentType docType = doc.getDocType();
and then add entities as InternalEntityDecl/ExternalEntityDecl with
setExternalDeclarations(java.util.List<Decl> declarations)
or
setInternalDeclarations(java.util.List<Decl> declarations)

paremetrize part of xml attribute value

I'm unmarshaling a couple of large XML files.
they have the common part and I decided to write the common parts in separate XML file and then include it using xi:include tag.
it looks like this:
<tag1>
<tag2>
</tag2>
<tag3>
</tag3>
<xi:include href = "long/common/part/of/partial/xml/file1"/>
<xi:include href = "long/common/part/of/partial/xml/file2"/>
</tag1>
at this moment I would like to parametrize the long/common/part.
I tried to define a variable using xsl:variable like this
<xsl:variable name="test">
"long/common/part/of/partial/xml/"
</xsl:variable>
but the assigning value to href was a problem, neither the
<xi:include href = "{$test}"/>
or
<xi:include href = <xsl:value-of select="test"/>
wasn't working.
Is there a way to assign value to XML attribute?
You're mixing XInclude, XSLT, and ad-hoc {$var} syntax (not part of XML) here. What you can do to parametrize a href value in XInclude elements is to use an entity reference (XML's and SGML's mechanism for text substitution variables among other things):
<xi:include href="&href-value;"/>
where href-value must be bound to the string long/common/part/of/partial/xml/file1 either programmatically, or (preferably) by declaring it in the prolog eg:
<!DOCTYPE tag1 [
<!ENTITY href-value "long/common/part/of/partial/xml/file1">
]>
<tag1>
<xi:include href = "&href-value;"/>
</tag1>
However, since now you're using entity references anyway, you can achieve just the same with just entities, and without XInclude at all:
<!DOCTYPE tag1 [
<!ENTITY common-part SYSTEM "long/common/part/of/partial/xml/file1">
]>
<tag1>
&common-part;
</tag1>
This pulls the content of long/common/part/of/partial/xml/file1 into the common-part entity, then references that value in content, with the XML parser treating the document as if the replacement value for common-part (eg. whatever is stored in long/common/part/of/partial/xml/file1) had been specified directly in the document.
Hope this isn't too confusing; there's a general explanation how entities in XML and SGML work in this answer

Parsing xml data

DOes anyone know how to resolve this exception??
java.io.FileNotFoundException: F:\eclipse\WS\l\dblp.dtd (The system cannot find the file specified)
Even though I have given the correct path still Im getting this exception.
heres my xml code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dblp SYSTEM "dblp.dtd">
<dblp>
<article mdate="2011-01-11" key="journals/acta/Saxena96">
<author>Sanjeev Saxena</author>
<title>Parallel Integer Sorting and Simulation Amongst CRCW Models. </title>
<pages>607-619</pages>
<year>1996</year>
<volume>33</volume>
<journal>Acta Inf.</journal>
<number>7</number>
<url>db/journals/acta/acta33.html#Saxena96</url>
<ee>http://dx.doi.org/10.1007/BF03036466</ee>
</article>
<article mdate="2011-01-11" key="journals/acta/Simon83">
<author>Hans-Ulrich Simon</author>
<title>Pattern Matching in Trees and Nets.</title>
<pages>227-248</pages>
<year>1983</year>
<volume>20</volume>
<journal>Acta Inf.</journal>
<url>db/journals/acta/acta20.html#Simon83</url>
<ee>http://dx.doi.org/10.1007/BF01257084</ee>
</article>
</dblp>
You are referencing "dblp.dtd" in your DOCTYPE - do you have that DTD file in the directory mentioned in the exception?
If not and you don't have the DTD, try removing the DOCTYPE line from the xml file, or overriding the entity resolution to tell it not to try loading it, as in this answer.

How to correct for no grammar constraints error

I have a simple XML document that is flagged (correctly) in Eclipse as having no grammar. I use the file to preload a database on initialisation. Is there a "generic" DTD or Schema that I could apply to this document (and similar - I have over 15 of them) to eliminate this warning and be more correct in my XML structure?
<AbnormalFlags>
<AbnormalFlag>
<code>H</code>
<description>High</description>
</AbnormalFlag>
<AbnormalFlag>
<code>L</code>
<description>Low</description>
</AbnormalFlag>
<AbnormalFlag>
<code>A</code>
<description>Abnormal</description>
</AbnormalFlag>
</AbnormalFlags>
Just add this on top of your xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>

How to use DTD ENTITY external reference in log4j.xml

I tried to use entities from external dtd file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>
log4j-entity.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY logHome "/root/crm_test/">
I tried to use the entity values in attribute values like this.
<param name="File" value="&logHome;log/info.log"/>
I get this errror:
The external entity reference "&logHome;" is not permitted in an attribute value.
How can I do this?
Note:
This thing works..
<!ENTITY logHome "/root/crm_test/">
You need to make the entity inside the internal subset a parameter entity and then reference it.
Change:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>
to:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHome SYSTEM "log4j-entity.dtd">
%logHome;
]>
The XML specs specifically forbid the use of external entities in attribute values. See here: http://www.w3.org/TR/2004/REC-xml-20040204/#forbidden
The following are forbidden, and constitute fatal errors: [...] a reference to an external entity in an attribute value.
So the answer is: XML won't let you do what you're trying to do. You might, however, get a similar effect if you ran your XML through an XSLT processor and applied transformations as needed.
There are a couple things wrong here.
You are using the entity name logHome for two different things (an external entity containing declarations, which should as Daniel Haley points out be a parameter entity) and an internal entity whose replacement text names a directory.
As a consequence, your reference to &logHome; in the attribute value is understood to be a reference to the resource whose URI is "log4j-entity.dtd".
The simplest way to achieve what you want would be to declare the logHome entity in the internal subset:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY logHome "/root/crm_test/">
]>
If you really want the declaration of logHome to be external, it might be less confusing to use a different name for the parameter entity:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHomeDeclaration SYSTEM "log4j-entity.dtd">
%logHomeDeclaration;
]>

Categories

Resources