Search in sources :

Example 11 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project jena by apache.

the class Metadata method read.

// Protect all classloader choosing -- sometimes systems mess with even the system class loader.
private static void read(Properties properties, String resourceName) {
    // Armour-plate this - classloaders and using them can be blocked by some environments.
    try {
        ClassLoader classLoader = null;
        try {
            classLoader = SystemUtils.chooseClassLoader();
        } catch (ARQException ex) {
        }
        if (classLoader == null) {
            try {
                classLoader = Metadata.class.getClassLoader();
            } catch (ARQException ex) {
            }
        }
        if (classLoader == null) {
            Log.error(Metadata.class, "No classloader");
            return;
        }
        InputStream in = classLoader.getResourceAsStream(resourceName);
        if (in == null)
            // In development, there is no properties file.
            return;
        try {
            properties.loadFromXML(in);
        } catch (InvalidPropertiesFormatException ex) {
            throw new ARQException("Invalid properties file", ex);
        } catch (IOException ex) {
            throw new ARQException("Metadata ==> IOException", ex);
        }
    } catch (Throwable ex) {
        Log.error(Metadata.class, "Unexpected Thorwable", ex);
        return;
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 12 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project Bytecoder by mirkosertic.

the class PropertiesDefaultHandler method load.

public void load(Properties props, InputStream in) throws IOException, InvalidPropertiesFormatException, UnsupportedEncodingException {
    this.properties = props;
    try {
        SAXParser parser = new SAXParserImpl();
        parser.parse(in, this);
    } catch (SAXException saxe) {
        throw new InvalidPropertiesFormatException(saxe);
    }
/**
 * String xmlVersion = propertiesElement.getAttribute("version"); if
 * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new
 * InvalidPropertiesFormatException( "Exported Properties file format
 * version " + xmlVersion + " is not supported. This java installation
 * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" +
 * " may need to install a newer version of JDK.");
 */
}
Also used : SAXParserImpl(jdk.internal.util.xml.impl.SAXParserImpl) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) SAXException(jdk.internal.org.xml.sax.SAXException)

Example 13 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project ofbiz-framework by apache.

the class UtilProperties method xmlToProperties.

/**
 * Convert XML property file to Properties instance. This method will convert
 * both the Java XML properties file format and the OFBiz custom XML
 * properties file format.
 *
 * <p>The format of the custom XML properties file is:</p>
 * <pre>
 * {@code
 * <resource>
 *     <property key="key">
 *     <value xml:lang="locale 1">Some value</value>
 *     <value xml:lang="locale 2">Some value</value>
 *     ...
 *     </property>
 *     ...
 * </resource>
 * }
 * </pre>
 * where <em>"locale 1", "locale 2"</em> are valid xml:lang values..
 *
 * @param in XML file InputStream
 * @param locale The desired locale
 * @param properties Optional Properties object to populate
 * @return Properties instance or null if not found
 */
public static Properties xmlToProperties(InputStream in, Locale locale, Properties properties) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }
    Document doc = null;
    try {
        doc = UtilXml.readXmlDocument(in, true, "XML Properties file");
        in.close();
    } catch (Exception e) {
        Debug.logWarning(e, "XML file for locale " + locale + " could not be loaded.", module);
        in.close();
        return null;
    }
    Element resourceElement = doc.getDocumentElement();
    List<? extends Element> propertyList = UtilXml.childElementList(resourceElement, "property");
    if (UtilValidate.isNotEmpty(propertyList)) {
        // Custom XML properties file format
        if (locale == null) {
            throw new IllegalArgumentException("locale cannot be null");
        }
        String localeString = locale.toString();
        String correctedLocaleString = localeString.replace('_', '-');
        for (Element property : propertyList) {
            // Support old way of specifying xml:lang value.
            // Old way: en_AU, new way: en-AU
            Element value = UtilXml.firstChildElement(property, "value", "xml:lang", correctedLocaleString);
            if (value == null) {
                value = UtilXml.firstChildElement(property, "value", "xml:lang", localeString);
            }
            if (value != null) {
                if (properties == null) {
                    properties = new Properties();
                }
                String valueString = UtilXml.elementValue(value);
                if (valueString != null) {
                    properties.put(property.getAttribute("key"), valueString);
                }
            }
        }
        return properties;
    }
    propertyList = UtilXml.childElementList(resourceElement, "entry");
    if (UtilValidate.isEmpty(propertyList)) {
        throw new InvalidPropertiesFormatException("XML properties file invalid or empty");
    }
    // Java XML properties file format
    for (Element property : propertyList) {
        String value = UtilXml.elementValue(property);
        if (value != null) {
            if (properties == null) {
                properties = new Properties();
            }
            properties.put(property.getAttribute("key"), value);
        }
    }
    return properties;
}
Also used : InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Properties(java.util.Properties) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 14 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project jena by apache.

the class Metadata method read.

// Protect all classloader choosing -- sometimes systems mess with even the system
// class loader.
private static void read(Properties properties, String resourceName) {
    // environments.
    try {
        ClassLoader classLoader = null;
        try {
            classLoader = SystemUtils.chooseClassLoader();
        } catch (AtlasException ex) {
        }
        if (classLoader == null) {
            try {
                classLoader = Metadata.class.getClassLoader();
            } catch (AtlasException ex) {
            }
        }
        if (classLoader == null) {
            Log.error(Metadata.class, "No classloader");
            return;
        }
        InputStream in = classLoader.getResourceAsStream(resourceName);
        if (in == null)
            return;
        try {
            properties.loadFromXML(in);
        } catch (InvalidPropertiesFormatException ex) {
            throw new JenaException("Invalid properties file", ex);
        } catch (IOException ex) {
            throw new RuntimeIOException("Metadata ==> IOException", ex);
        }
    } catch (Throwable ex) {
        Log.error(Metadata.class, "Unexpected Throwable", ex);
        return;
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) InputStream(java.io.InputStream) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException) AtlasException(org.apache.jena.atlas.AtlasException)

Example 15 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project j2objc by google.

the class PropertiesXmlLoader method load.

public void load(final Properties p, InputStream in) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new NullPointerException("in == null");
    }
    try {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(new DefaultHandler() {

            private String key;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                key = null;
                if (qName.equals("entry")) {
                    key = attributes.getValue("key");
                }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                if (key != null) {
                    String value = new String(ch, start, length);
                    p.put(key, value);
                    key = null;
                }
            }
        });
        reader.parse(new InputSource(in));
    } catch (SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) Attributes(org.xml.sax.Attributes) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Aggregations

InvalidPropertiesFormatException (java.util.InvalidPropertiesFormatException)18 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 Properties (java.util.Properties)6 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)2 LineNumberReader (java.io.LineNumberReader)2 SQLException (java.sql.SQLException)2 SAXException (jdk.internal.org.xml.sax.SAXException)2 SAXParserImpl (jdk.internal.util.xml.impl.SAXParserImpl)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 SAXException (org.xml.sax.SAXException)2 ClientException (com.iplanet.services.cdm.ClientException)1 SSOException (com.iplanet.sso.SSOException)1 SMSException (com.sun.identity.sm.SMSException)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1