Search in sources :

Example 6 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project ceylon by eclipse.

the class MemoPushbackReader method handleOption.

private void handleOption() throws IOException {
    String option = readName(false);
    String optName = section + "." + option;
    skipWhitespace(false);
    Token tok = peekToken();
    if (tok == Token.assign) {
        expect('=');
        handleOptionValue(optName);
    } else if (tok == Token.error) {
        throw new InvalidPropertiesFormatException("Unexpected token in configuration file at line " + (counterdr.getLineNumber() + 1));
    } else {
        listener.onOption(optName, "true", reader.getAndClearMemo());
    }
}
Also used : InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException)

Example 7 with InvalidPropertiesFormatException

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

the class Transformer method load.

/**
 * Loads a file listing types and properties contained in namespaces.
 * The file location is relative to the {@code Transformer} class.
 * The file format is a tree structured with indentation as below:
 *
 * <ul>
 *   <li>Lines with zero-space indentation are namespace URIs.</li>
 *   <li>Lines with one-space  indentation are classes within the last namespace URIs found so far.</li>
 *   <li>Lines with two-spaces indentation are properties within the last class found so far.</li>
 *   <li>All other indentations are illegal and cause an {@link InvalidPropertiesFormatException} to be thrown.
 *       This exception type is not really appropriate since the file format is not a {@code .properties} file,
 *       but it is the closest we could find in existing exceptions and we don't want to define a new exception
 *       type since this error should never happen.</li>
 * </ul>
 *
 * The returned map is structured as below:
 *
 * <ul>
 *   <li>Keys are XML names of types, ignoring {@code "_TYPE"} suffix (e.g. {@code "CI_Citation"})</li>
 *   <li>Values are maps where:<ul>
 *     <li>Keys are XML names of properties (e.g. {@code "title"})</li>
 *     <li>Values are either:<ul>
 *       <li>Namespace URI if {@link #isNamespace(String)} returns {@code true} for that value.</li>
 *       <li>New name of the element otherwise. In such case, the map must be queried again with
 *           that new name for obtaining the namespace.</li>
 *     </ul></li>
 *   </ul></li>
 * </ul>
 *
 * @param  filename  name of the file to load.
 * @param  capacity  initial hash map capacity. This is only a hint.
 */
static Map<String, Map<String, String>> load(final String filename, final int capacity) {
    final Map<String, Map<String, String>> m = new HashMap<>(capacity);
    try (LineNumberReader in = new LineNumberReader(new InputStreamReader(TransformingReader.class.getResourceAsStream(filename), "UTF-8"))) {
        // All attributes for a given type.
        Map<String, String> attributes = null;
        // Value to store in 'attributes' map.
        String namespace = null;
        String line;
        while ((line = in.readLine()) != null) {
            final int length = line.length();
            final int start = CharSequences.skipLeadingWhitespaces(line, 0, length);
            if (start < length && line.charAt(start) != '#') {
                String element = line.substring(start).trim();
                switch(start) {
                    case 0:
                        {
                            // Report illegal format.
                            if (!isNamespace(element))
                                break;
                            namespace = element.intern();
                            attributes = null;
                            continue;
                        }
                    case 1:
                        {
                            // Report illegal format.
                            if (namespace == null)
                                break;
                            final int s = element.indexOf(NO_NAMESPACE);
                            if (s >= 0) {
                                element = element.substring(0, s).trim();
                            }
                            element = element.intern();
                            attributes = m.computeIfAbsent(element, (k) -> new HashMap<>());
                            if (s < 0) {
                                // Record namespace for this type only if '!' is not present.
                                if (attributes.put(element, namespace) != null)
                                    break;
                            }
                            continue;
                        }
                    case 2:
                        {
                            // Report illegal format.
                            if (attributes == null || namespace == null)
                                break;
                            final int s = element.indexOf(RENAME_SEPARATOR);
                            if (s >= 0) {
                                final String old = element.substring(0, s).trim().intern();
                                element = element.substring(s + 1).trim().intern();
                                // Report an error if duplicated values.
                                if (attributes.put(old, element) != null)
                                    break;
                            } else {
                                element = element.intern();
                            }
                            // Report an error if duplicated values.
                            if (attributes.put(element, namespace) != null)
                                break;
                            continue;
                        }
                }
                throw new InvalidPropertiesFormatException(// See method javadoc.
                Errors.format(Errors.Keys.ErrorInFileAtLine_2, filename, in.getLineNumber()));
            }
        }
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
    /*
         * At this point we finished computing the map values. Many values are maps with only 1 entry.
         * Save a little bit of space by replacing maps of 1 element by Collections.singletonMap(…).
         */
    m.replaceAll((k, v) -> CollectionsExt.compact(v));
    return m;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) LineNumberReader(java.io.LineNumberReader)

Example 8 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project Info-Evaluation by TechnionYP5777.

the class MySQLConnector method getConnection.

private Connection getConnection() throws SQLException, IOException, ClassNotFoundException {
    Properties props = new Properties();
    try {
        props.loadFromXML(getClass().getResourceAsStream("../resources/DatabaseConnectionProperties.xml"));
    } catch (InvalidPropertiesFormatException ¢) {
        throw ¢;
    }
    String $ = props.getProperty("jdbc.username");
    String password = props.getProperty("jdbc.password");
    String driver = props.getProperty("jdbc.driver");
    String host = props.getProperty("jdbc.host");
    try {
        Class.forName(driver);
    } catch (ClassNotFoundException ¢) {
        throw ¢;
    }
    try {
        return DriverManager.getConnection(host, $, password);
    } catch (SQLException ¢) {
        throw ¢;
    }
}
Also used : InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) SQLException(java.sql.SQLException) Properties(java.util.Properties)

Example 9 with InvalidPropertiesFormatException

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

the class SuperProperties method loadFromXML.

public synchronized void loadFromXML(final InputStream in) throws IOException {
    if (in == null) {
        throw new NullPointerException();
    }
    final DocumentBuilder builder = getDocumentBuilder();
    try {
        final Document doc = builder.parse(in);
        final NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        final int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            final Element entry = (Element) entries.item(i);
            final String key = entry.getAttribute("key");
            final String value = entry.getTextContent();
            put(key, value);
            // search backwards for a comment
            for (Node node = entry.getPreviousSibling(); node != null && !(node instanceof Element); node = node.getPreviousSibling()) {
                if (node instanceof Comment) {
                    final InputStream cin = new ByteArrayInputStream(((Comment) node).getData().getBytes());
                    // read comment line by line
                    final StringBuilder comment = new StringBuilder();
                    final LinkedHashMap<String, String> attributes = new LinkedHashMap<>();
                    int nextByte;
                    char nextChar;
                    boolean firstLine = true;
                    int commentIndent = Integer.MAX_VALUE;
                    do {
                        // read one line
                        final StringBuilder commentLine = new StringBuilder();
                        int commentLineIndent = 0;
                        boolean inIndent = true;
                        while (true) {
                            nextByte = cin.read();
                            if (nextByte < 0) {
                                break;
                            }
                            // & 0xff
                            nextChar = (char) nextByte;
                            if (inIndent && nextChar == ' ') {
                                commentLineIndent++;
                                commentLine.append(' ');
                            } else if (inIndent && nextChar == '\t') {
                                commentLineIndent += 4;
                                commentLine.append("    ");
                            } else if (nextChar == '\r' || nextChar == '\n') {
                                break;
                            } else {
                                inIndent = false;
                                commentLine.append(nextChar);
                            }
                        }
                        // Determine indent
                        if (!firstLine && commentIndent == Integer.MAX_VALUE && commentLine.length() > 0) {
                            // if this is a new comment block, the comment indent size for this
                            // block is based the first full line of the comment (ignoring the
                            // line with the <!--
                            commentIndent = commentLineIndent;
                        }
                        commentLineIndent = Math.min(commentIndent, commentLineIndent);
                        if (commentLine.toString().trim().startsWith("@")) {
                            // process property attribute
                            final String attribute = commentLine.toString().trim().substring(1);
                            final String[] parts = attribute.split("=", 2);
                            final String attributeName = parts[0].trim();
                            final String attributeValue = parts.length == 2 ? parts[1].trim() : "";
                            attributes.put(attributeName, attributeValue);
                        } else {
                            // append comment
                            if (comment.length() != 0) {
                                comment.append(lineSeparator);
                            }
                            comment.append(commentLine.toString().substring(commentLineIndent));
                        }
                        firstLine = false;
                    } while (nextByte > 0);
                    if (comment.length() > 0) {
                        setComment(key, comment.toString());
                    }
                    this.attributes.put(normalize(key), attributes);
                    break;
                }
            }
        }
    } catch (final SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : Comment(org.w3c.dom.Comment) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LinkedHashMap(java.util.LinkedHashMap) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException)

Example 10 with InvalidPropertiesFormatException

use of java.util.InvalidPropertiesFormatException in project SSM by Intel-bigdata.

the class MetaStoreUtils method getDBAdapter.

public static MetaStore getDBAdapter(SmartConf conf) throws MetaStoreException {
    URL pathUrl = ClassLoader.getSystemResource("");
    String path = pathUrl.getPath();
    characterTakeUpBytes = conf.getInt(SmartConfKeys.SMART_METASTORE_CHARACTER_TAKEUP_BYTES_KEY, SmartConfKeys.SMART_METASTORE_CHARACTER_TAKEUP_BYTES_DEFAULT);
    String fileName = "druid.xml";
    String expectedCpPath = path + fileName;
    LOG.info("Expected DB connection pool configuration path = " + expectedCpPath);
    File cpConfigFile = new File(expectedCpPath);
    if (cpConfigFile.exists()) {
        LOG.info("Using pool configure file: " + expectedCpPath);
        Properties p = new Properties();
        try {
            p.loadFromXML(new FileInputStream(cpConfigFile));
            String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY);
            if (url != null) {
                p.setProperty("url", url);
            }
            String purl = p.getProperty("url");
            if (purl == null || purl.length() == 0) {
                // For testing
                purl = getDefaultSqliteDB();
                p.setProperty("url", purl);
                LOG.warn("Database URL not specified, using " + purl);
            }
            if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) {
                String dbName = getMysqlDBName(purl);
                for (String name : DB_NAME_NOT_ALLOWED) {
                    if (dbName.equals(name)) {
                        throw new MetaStoreException(String.format("The database %s in mysql is for DB system use, " + "please appoint other database in druid.xml.", name));
                    }
                }
            }
            try {
                String pw = conf.getPasswordFromHadoop(SmartConfKeys.SMART_METASTORE_PASSWORD);
                if (pw != null && pw != "") {
                    p.setProperty("password", pw);
                }
            } catch (IOException e) {
                LOG.info("Can not get metastore password from hadoop provision credentials," + " use the one configured in druid.xml .");
            }
            for (String key : p.stringPropertyNames()) {
                if (key.equals("password")) {
                    LOG.info("\t" + key + " = **********");
                } else {
                    LOG.info("\t" + key + " = " + p.getProperty(key));
                }
            }
            return new MetaStore(new DruidPool(p));
        } catch (Exception e) {
            if (e instanceof InvalidPropertiesFormatException) {
                throw new MetaStoreException("Malformat druid.xml, please check the file.", e);
            } else {
                throw new MetaStoreException(e);
            }
        }
    } else {
        LOG.info("DB connection pool config file " + expectedCpPath + " NOT found.");
    }
    // Get Default configure from druid-template.xml
    fileName = "druid-template.xml";
    expectedCpPath = path + fileName;
    LOG.info("Expected DB connection pool configuration path = " + expectedCpPath);
    cpConfigFile = new File(expectedCpPath);
    LOG.info("Using pool configure file: " + expectedCpPath);
    Properties p = new Properties();
    try {
        p.loadFromXML(new FileInputStream(cpConfigFile));
    } catch (Exception e) {
        throw new MetaStoreException(e);
    }
    String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY);
    if (url != null) {
        p.setProperty("url", url);
    }
    for (String key : p.stringPropertyNames()) {
        LOG.info("\t" + key + " = " + p.getProperty(key));
    }
    return new MetaStore(new DruidPool(p));
}
Also used : MetaStoreException(org.smartdata.metastore.MetaStoreException) MetaStore(org.smartdata.metastore.MetaStore) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) DruidPool(org.smartdata.metastore.DruidPool) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) SQLException(java.sql.SQLException) IOException(java.io.IOException) MetaStoreException(org.smartdata.metastore.MetaStoreException)

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