Changeset 8695


Ignore:
Timestamp:
2004-11-29T17:08:52+13:00 (19 years ago)
Author:
mdewsnip
Message:

Now remove invalid characters from metadata values when they are entered. This prevents the problem of writing out XML files with invalid characters, which are barfed on when reading them back in.

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

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/gui/EnrichPane.java

    r8635 r8695  
    6464import org.greenstone.gatherer.util.TreeSynchronizer;
    6565import org.greenstone.gatherer.util.Utility;
    66 
    67 
    68 /** Provides a view of controls for the editing of metadata. It makes use of several other important components such as a GTree, GTable and MetadataValueTree. While much of the gui content is left to these components, the EnrichPane is resposible for actioning metadata edit requests, listing for mouse clicks within its scope and other data functionality (such as providing a list of the selected files).
     66import org.greenstone.gatherer.util.XMLTools;
     67
     68
     69/** Provides a view of controls for the editing of metadata.
    6970 */
    7071public class EnrichPane
     
    10941095    public String getSelectedValue()
    10951096    {
    1096         return metadata_value_text_field.getText().replaceAll("\\\\", MetadataValueTreeNode.METADATA_VALUE_TREE_NODE_HIERARCHY_TOKEN);
     1097        String metadata_value_raw = metadata_value_text_field.getText();
     1098        String metadata_value = XMLTools.removeInvalidCharacters(metadata_value_raw);
     1099        return metadata_value.replaceAll("\\\\", MetadataValueTreeNode.METADATA_VALUE_TREE_NODE_HIERARCHY_TOKEN);
    10971100    }
    10981101
  • trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r8652 r8695  
    9696    }
    9797    return child;
    98     }
    99 
    100 
    101     /** Set the #text node value of some element.
    102      * @param element the Element whose value we wish to set
    103      * @param value the new value for the element as a String
    104      * Soon to be deprecated!
    105      */
    106     static final public void setValue(Element element, String value) {
    107     // Remove any existing child node(s)
    108     clear(element);
    109     // Add new text node.
    110     if (value != null) {
    111         element.appendChild(element.getOwnerDocument().createTextNode(value));
    112     }
    11398    }
    11499
     
    136121    }
    137122    catch (Exception exception) {
    138         exception.printStackTrace();
     123        DebugStream.printStackTrace(exception);
    139124    }
    140125
    141126    return document;
     127    }
     128
     129
     130    /** Removes characters that are invalid in XML (see http://www.w3.org/TR/2000/REC-xml-20001006#charsets) */
     131    static public String removeInvalidCharacters(String text)
     132    {
     133    char[] safe_characters = new char[text.length()];
     134    int j = 0;
     135
     136    char[] raw_characters = new char[text.length()];
     137    text.getChars(0, text.length(), raw_characters, 0);
     138    for (int i = 0; i < raw_characters.length; i++) {
     139        char character = raw_characters[i];
     140        if ((character >= 0x20 && character <= 0xD7FF) || character == 0x09 || character == 0x0A || character == 0x0D || (character >= 0xE000 && character <= 0xFFFD) || (character >= 0x10000 && character <= 0x10FFFF)) {
     141        safe_characters[j] = character;
     142        j++;
     143        }
     144    }
     145
     146    return new String(safe_characters, 0, j);
     147    }
     148
     149
     150    /** Set the #text node value of some element.
     151     * @param element the Element whose value we wish to set
     152     * @param value the new value for the element as a String
     153     * Soon to be deprecated!
     154     */
     155    static final public void setValue(Element element, String value) {
     156    // Remove any existing child node(s)
     157    clear(element);
     158    // Add new text node.
     159    if (value != null) {
     160        element.appendChild(element.getOwnerDocument().createTextNode(value));
     161    }
    142162    }
    143163
     
    166186    }
    167187    catch (Exception exception) {
    168         exception.printStackTrace();
     188        DebugStream.printStackTrace(exception);
    169189    }
    170190    }
Note: See TracChangeset for help on using the changeset viewer.