Changeset 25206 for main


Ignore:
Timestamp:
2012-03-12T10:51:09+13:00 (12 years ago)
Author:
sjm84
Message:

(Hopefully fixed config file issues for Greenstone 3 GLI

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java

    r24931 r25206  
    639639        if (format != null && format.hasChildNodes())
    640640        { // not an empty format statement
    641             String gsf_text = XMLTools.xmlNodeToStringWithoutIndenting(format);
     641            String gsf_text = XMLTools.xmlNodeToString(format);
    642642           
    643643            //We don't want the <format> elements in the string
     
    23662366    static public void save(File collect_config_xml_file, Document doc)
    23672367    {
    2368 
    23692368        Document collection_config_xml_document = convertInternalToCollectionConfig(doc);
    23702369        String[] nonEscapingTagNames = { StaticStrings.FORMAT_STR, StaticStrings.DISPLAYITEM_STR };
    23712370        XMLTools.writeXMLFile(collect_config_xml_file, collection_config_xml_document, nonEscapingTagNames);
    2372 
    23732371    }
    23742372
  • main/trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r25165 r25206  
    712712                    xml.append("\n");
    713713                }
    714                 else {
    715                   System.err.println(line);
     714                else
     715                {
     716                    System.err.println(line);
    716717                }
    717718            }
     
    791792    }
    792793
     794    static public void indentXML(Element elem, int depth)
     795    {
     796        Document doc = elem.getOwnerDocument();
     797
     798        String startIndentString = "\n";
     799        for (int i = 0; i < depth; i++)
     800        {
     801            startIndentString += "\t";
     802        }
     803        Node startTextNode = doc.createTextNode(startIndentString);
     804
     805        String endIndentString = "\n";
     806        for (int i = 0; i < depth - 1; i++)
     807        {
     808            endIndentString += "\t";
     809        }
     810        Node endTextNode = doc.createTextNode(endIndentString);
     811
     812        boolean found = false;
     813        Node child = elem.getFirstChild();
     814        while (child != null)
     815        {
     816            if (child.getNodeType() == Node.ELEMENT_NODE)
     817            {
     818                found = true;
     819                break;
     820            }
     821            child = child.getNextSibling();
     822        }
     823
     824        if (found)
     825        {
     826            elem.appendChild(endTextNode);
     827        }
     828
     829        child = elem.getFirstChild();
     830        while (child != null)
     831        {
     832            if (child.getNodeType() == Node.ELEMENT_NODE)
     833            {
     834                elem.insertBefore(startTextNode.cloneNode(false), child);
     835                indentXML((Element) child, depth + 1);
     836            }
     837            child = child.getNextSibling();
     838        }
     839    }
     840
    793841    /**
    794842     * Write an XML document to a given file with the text node of the specified
     
    797845    static public void writeXMLFile(File xml_file, Document document, String[] nonEscapingTagNames)
    798846    {
     847        indentXML(document.getDocumentElement(), 1);
    799848        try
    800849        {
     
    895944    {
    896945        StringBuffer sb = new StringBuffer("");
    897         xmlNodeToString(sb, e, 0);
     946        xmlNodeToString(sb, e, true, 2);
    898947        return sb.toString();
    899948    }
    900949
    901     private static void xmlNodeToString(StringBuffer sb, Node e, int depth)
     950    private static void xmlNodeToString(StringBuffer sb, Node e, boolean indent, int depth)
     951    {
     952        if (e.getNodeType() == Node.TEXT_NODE)
     953        {
     954            if (e.getNodeValue() != "")
     955            {
     956                String text = e.getNodeValue();
     957                text = text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("^[\\n\\r\\t\\s]*", "");
     958                sb.append(text);
     959            }
     960            return;
     961        }
     962
     963        if (e.getNodeType() == Node.COMMENT_NODE)
     964        {
     965            if (e.getNodeValue() != "")
     966            {
     967                sb.append("<!--" + e.getNodeValue() + "-->");
     968            }
     969            return;
     970        }
     971
     972        if (indent)
     973        {
     974            for (int i = 0; i < depth; i++)
     975            {
     976                sb.append('\t');
     977            }
     978        }
     979
     980        sb.append('<');
     981        sb.append(e.getNodeName());
     982        NamedNodeMap attrs = e.getAttributes();
     983        if (attrs != null)
     984        {
     985            for (int i = 0; i < attrs.getLength(); i++)
     986            {
     987                Node attr = attrs.item(i);
     988                sb.append(' ');
     989                sb.append(attr.getNodeName());
     990                sb.append("=\"");
     991                sb.append(attr.getNodeValue().replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
     992                sb.append('"');
     993            }
     994        }
     995        NodeList children = e.getChildNodes();
     996
     997        boolean hasElements = false;
     998        boolean indentSwapped = false;
     999        for (int i = 0; i < children.getLength(); i++)
     1000        {
     1001            if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
     1002            {
     1003                hasElements = true;
     1004            }
     1005            if (children.item(i).getNodeType() == Node.TEXT_NODE && indent)
     1006            {
     1007                if (children.item(i).getNodeValue().matches("[^\\s]*"))
     1008                {
     1009                    indentSwapped = true;
     1010                    indent = false;
     1011                }
     1012            }
     1013        }
     1014
     1015        if (children == null || children.getLength() == 0)
     1016        {
     1017            sb.append("/>");
     1018
     1019            if (indent)
     1020            {
     1021                sb.append("\n");
     1022            }
     1023        }
     1024        else
     1025        {
     1026            sb.append(">");
     1027            if (hasElements && indent)
     1028            {
     1029                sb.append("\n");
     1030            }
     1031
     1032            int len = children.getLength();
     1033            for (int i = 0; i < len; i++)
     1034            {
     1035                xmlNodeToString(sb, children.item(i), indent, depth + 1);
     1036            }
     1037
     1038            if (indent)
     1039            {
     1040                for (int i = 0; i < depth; i++)
     1041                {
     1042                    sb.append('\t');
     1043                }
     1044            }
     1045
     1046            sb.append("</" + e.getNodeName() + ">");
     1047
     1048            if ((hasElements && indent) || indentSwapped)
     1049            {
     1050                sb.append("\n");
     1051            }
     1052        }
     1053    }
     1054
     1055    public static String xmlNodeToStringWithoutIndenting(Node e)
     1056    {
     1057        StringBuffer sb = new StringBuffer("");
     1058        xmlNodeToStringWithoutNewline(sb, e, -1);
     1059        return sb.toString();
     1060    }
     1061
     1062    public static String xmlNodeToStringWithoutNewline(Node e)
     1063    {
     1064        StringBuffer sb = new StringBuffer("");
     1065        xmlNodeToStringWithoutNewline(sb, e, 0);
     1066        return sb.toString();
     1067    }
     1068
     1069    private static void xmlNodeToStringWithoutNewline(StringBuffer sb, Node e, int depth)
    9021070    {
    9031071
    9041072        for (int i = 0; i < depth; i++)
     1073        {
    9051074            sb.append(' ');
     1075        }
    9061076
    9071077        if (e.getNodeType() == Node.TEXT_NODE)
     
    9091079            if (e.getNodeValue() != "")
    9101080            {
    911                 sb.append(e.getNodeValue());
     1081                sb.append(e.getNodeValue().replaceAll("&", "&amp;").replaceAll("<", "&lt;").replace(">", "&gt;"));
    9121082            }
    9131083            return;
    9141084        }
    915        
     1085
    9161086        if (e.getNodeType() == Node.COMMENT_NODE)
    9171087        {
     
    9411111
    9421112        if (children == null || children.getLength() == 0)
    943             sb.append("/>\n");
     1113            sb.append("/>");
    9441114        else
    9451115        {
    9461116
    947             sb.append(">\n");
     1117            sb.append(">");
    9481118
    9491119            int len = children.getLength();
    9501120            for (int i = 0; i < len; i++)
    9511121            {
    952                 xmlNodeToString(sb, children.item(i), depth + 1);
     1122                if (depth >= 0)
     1123                {
     1124                    xmlNodeToStringWithoutNewline(sb, children.item(i), depth + 1);
     1125                }
     1126                else
     1127                {
     1128                    xmlNodeToStringWithoutNewline(sb, children.item(i), depth);
     1129                }
    9531130            }
    9541131
     
    9561133                sb.append(' ');
    9571134
    958             sb.append("</" + e.getNodeName() + ">\n");
    959         }
    960 
    961     }
    962 
    963     public static String xmlNodeToStringWithoutIndenting(Node e)
    964     {
    965         StringBuffer sb = new StringBuffer("");
    966         xmlNodeToStringWithoutNewline(sb, e, -1);
    967         return sb.toString();
    968     }
    969 
    970     public static String xmlNodeToStringWithoutNewline(Node e)
    971     {
    972         StringBuffer sb = new StringBuffer("");
    973         xmlNodeToStringWithoutNewline(sb, e, 0);
    974         return sb.toString();
    975     }
    976 
    977     private static void xmlNodeToStringWithoutNewline(StringBuffer sb, Node e, int depth)
    978     {
    979 
    980         for (int i = 0; i < depth; i++)
    981         {
    982             sb.append(' ');
    983         }
    984 
    985         if (e.getNodeType() == Node.TEXT_NODE)
    986         {
    987             if (e.getNodeValue() != "")
    988             {
    989                 sb.append(e.getNodeValue().replaceAll("&", "&amp;").replaceAll("<", "&lt;").replace(">", "&gt;"));
    990             }
    991             return;
    992         }
    993        
    994         if (e.getNodeType() == Node.COMMENT_NODE)
    995         {
    996             if (e.getNodeValue() != "")
    997             {
    998                 sb.append("<!--" + e.getNodeValue() + "-->");
    999             }
    1000             return;
    1001         }
    1002 
    1003         sb.append('<');
    1004         sb.append(e.getNodeName());
    1005         NamedNodeMap attrs = e.getAttributes();
    1006         if (attrs != null)
    1007         {
    1008             for (int i = 0; i < attrs.getLength(); i++)
    1009             {
    1010                 Node attr = attrs.item(i);
    1011                 sb.append(' ');
    1012                 sb.append(attr.getNodeName());
    1013                 sb.append("=\"");
    1014                 sb.append(attr.getNodeValue());
    1015                 sb.append('"');
    1016             }
    1017         }
    1018         NodeList children = e.getChildNodes();
    1019 
    1020         if (children == null || children.getLength() == 0)
    1021             sb.append("/>");
    1022         else
    1023         {
    1024 
    1025             sb.append(">");
    1026 
    1027             int len = children.getLength();
    1028             for (int i = 0; i < len; i++)
    1029             {
    1030                 if (depth >= 0)
    1031                 {
    1032                     xmlNodeToStringWithoutNewline(sb, children.item(i), depth + 1);
    1033                 }
    1034                 else
    1035                 {
    1036                     xmlNodeToStringWithoutNewline(sb, children.item(i), depth);
    1037                 }
    1038             }
    1039 
    1040             for (int i = 0; i < depth; i++)
    1041                 sb.append(' ');
    1042 
    10431135            sb.append("</" + e.getNodeName() + ">");
    10441136        }
Note: See TracChangeset for help on using the changeset viewer.