Dan Caprioara created FOP-2892:
----------------------------------

             Summary: Font substitutions not working: the 
DefaultConfiguration.getChild() does not find elements as expected
                 Key: FOP-2892
                 URL: https://issues.apache.org/jira/browse/FOP-2892
             Project: FOP
          Issue Type: Bug
          Components: unqualified
    Affects Versions: 2.4
            Reporter: Dan Caprioara


Starting with FOP 2.4 the avalon framework is not used anymore. 

Instead, a {{Configuration}} implementation has been added to the FOP: 
{{org.apache.fop.configuration.DefaultConfiguration}}

The problem is that the {{getChild(String)}} method uses 
{{getElementsByTagName}} that returns all matching elements in document order, 
at any level. The old avalon implementation iterated only through the direct 
children. This creates some bugs.

Consider the following configuration:
{code:xml}

<fop version="1.0">

  <!-- 
....
   --->

  <renderers>
    <renderer mime="application/pdf">
      <fonts> 
        <auto-detect/>
      </fonts>
    </renderer>
  </renderers>

  <!-- A substitution can map a font family to another. -->
  <fonts>
    <substitutions>  
        <substitution>
            <from font-family='courierNew' font-style='normal' 
font-weight='400'/>   <to font-family='Courier New'/> 
         </substitution> 
     </substitutions>
  </fonts>

</fop>
{code}

The code from the {{FontManagerConfigurator}} 
{code}
DefaultConfiguration fontsCfg = (DefaultConfiguration)cfg.getChild("fonts", 
false);
{code}
now gets the first {{fonts}} element, the one containing the autodetect, 
instead of getting the direct {{fonts}} element, the one containing the 
substitutions.

The patch that solves this is:

{code}
Index: DefaultConfiguration.java
===================================================================
--- DefaultConfiguration.java   (revision 195722)
+++ DefaultConfiguration.java   (working copy)
@@ -108,7 +108,7 @@
 
     @Override
     public Configuration getChild(String key) {
-        NodeList nl = element.getElementsByTagName(key);
+        NodeList nl = element.getChildNodes();
         for (int i = 0; i < nl.getLength(); ++i) {
             Node n = nl.item(i);
             if (n.getNodeName().equals(key)) {
@@ -133,13 +133,17 @@
 
     @Override
     public Configuration[] getChildren(String key) {
-        NodeList nl = element.getElementsByTagName(key);
-        Configuration[] result = new Configuration[nl.getLength()];
+        ArrayList<Configuration> result = new ArrayList<>(1);
+
+        NodeList nl = element.getChildNodes();
         for (int i = 0; i < nl.getLength(); ++i) {
             Node n = nl.item(i);
-            result[i] = new DefaultConfiguration((Element) n);
+            if (n.getNodeName().equals(key)) {
+              result.add(new DefaultConfiguration((Element) n));
+            }
         }
-        return result;
+
+        return result.toArray(new Configuration[0]);
     }
 
     @Override
{code}





--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to