/// <summary>
        /// Add the contents of the file to the root BoostInfoTree.
        /// </summary>
        ///
        /// <param name="fileName">The path to the INFO file.</param>
        /// <returns>The new root BoostInfoTree.</returns>
        public BoostInfoTree read(String fileName)
        {
            TextReader stream = new FileReader(fileName);
            // Use "try/finally instead of "try-with-resources" or "using"
            // which are not supported before Java 7.
            try {
                read(stream, root_);
            } finally {
                stream.close();
            }

            return root_;
        }
        /// <summary>
        /// Internal helper method for parsing INFO files line by line.
        /// </summary>
        ///
        private BoostInfoTree parseLine(String line, BoostInfoTree context)
        {
            // Skip blank lines and comments.
            int commentStart = line.indexOf(';');
            if (commentStart >= 0)
                line = line.Substring(0,(commentStart)-(0)).trim();
            if (line.Length == 0)
                return context;

            // Usually we are expecting key and optional value.
            // Use ArrayList without generics so it works with older Java compilers.
            ArrayList<String> strings = new ArrayList<String>();
            shlex_split(line, strings);
            bool isSectionStart = false;
            bool isSectionEnd = false;
            for (int i = 0; i < strings.Count; ++i) {
                isSectionStart = (isSectionStart || "{".equals(strings[i]));
                isSectionEnd = (isSectionEnd || "}".equals(strings[i]));
            }

            if (!isSectionStart && !isSectionEnd) {
                String key = strings[0];
                String val = "";
                if (strings.Count > 1)
                    val = strings[1];

                // If it is an "#include", load the new file instead of inserting keys.
                if ("#include".equals(key)) {
                    TextReader stream = new FileReader(val);
                    // Use "try/finally instead of "try-with-resources" or "using"
                    // which are not supported before Java 7.
                    try {
                        context = read(stream, context);
                    } finally {
                        stream.close();
                    }
                } else
                    context.createSubtree(key, val);

                return context;
            }

            // OK, who is the joker who put a { on the same line as the key name?!
            int sectionStart = line.indexOf('{');
            if (sectionStart > 0) {
                String firstPart = line.Substring(0,(sectionStart)-(0));
                String secondPart = line.Substring(sectionStart);

                BoostInfoTree ctx = parseLine(firstPart, context);
                return parseLine(secondPart, ctx);
            }

            // If we encounter a {, we are beginning a new context.
            // TODO: Error if there was already a subcontext here.
            if (line[0] == '{') {
                context = context.getLastChild();
                return context;
            }

            // If we encounter a }, we are ending a list context.
            if (line[0] == '}') {
                context = context.getParent();
                return context;
            }

            throw new Exception("BoostInfoParser: input line is malformed");
        }
            public static IdentityCertificate loadIdentityCertificateFromFile(
					String filename)
            {
                StringBuilder encodedData = new StringBuilder();

                try {
                    TextReader certFile = new FileReader(
                                            filename);
                    // Use "try/finally instead of "try-with-resources" or "using"
                    // which are not supported before Java 7.
                    try {
                        String line;
                        while ((line = certFile.readLine()) != null)
                            encodedData.append(line);
                    } finally {
                        certFile.close();
                    }
                } catch (FileNotFoundException ex) {
                    throw new SecurityException(
                            "Can't find IdentityCertificate file " + filename
                                    + ": " + ex.Message);
                } catch (IOException ex_0) {
                    throw new SecurityException(
                            "Error reading IdentityCertificate file " + filename
                                    + ": " + ex_0.Message);
                }

                byte[] decodedData = net.named_data.jndn.util.Common.base64Decode(encodedData.toString());
                IdentityCertificate cert = new IdentityCertificate();
                try {
                    cert.wireDecode(new Blob(decodedData, false));
                } catch (EncodingException ex_1) {
                    throw new SecurityException(
                            "Can't decode the IdentityCertificate from file "
                                    + filename + ": " + ex_1.Message);
                }
                return cert;
            }
Exemple #4
0
        /// <summary>
        /// Open path_, parse the configuration file and set config_.
        /// </summary>
        ///
        private void parse()
        {
            if (path_.equals(""))
                throw new Exception(
                        "ConfigFile::parse: Failed to locate the configuration file for parsing");

            TextReader input;
            try {
                input = new FileReader(path_);
            } catch (FileNotFoundException ex) {
                // We don't expect this to happen since we just checked for the file.
                throw new Exception(ex.Message);
            }

            // Use "try/finally instead of "try-with-resources" or "using"
            // which are not supported before Java 7.
            try {
                String line;
                while ((line = input.readLine()) != null) {
                    line = line.trim();
                    if (line.equals("") || line[0] == ';')
                        // Skip empty lines and comments.
                        continue;

                    int iSeparator = line.indexOf('=');
                    if (iSeparator < 0)
                        continue;

                    String key = line.Substring(0,(iSeparator)-(0)).trim();
                    String value_ren = line.Substring(iSeparator + 1).trim();

                    ILOG.J2CsMapping.Collections.Collections.Put(config_,key,value_ren);
                }
            } finally {
                input.close();
            }
        }
        /// <summary>
        /// Read from a key file
        /// </summary>
        ///
        /// <param name="keyName"></param>
        /// <param name="keyClass">[PUBLIC, PRIVATE, SYMMETRIC]</param>
        /// <returns>The key bytes.</returns>
        /// <exception cref="IOException"></exception>
        /// <exception cref="System.Security.SecurityException"></exception>
        private byte[] read(Name keyName, KeyClass keyClass)
        {
            String extension = (String) ILOG.J2CsMapping.Collections.Collections.Get(keyTypeMap_,keyClass);
            StringBuilder contents = new StringBuilder();
            try {
                TextReader reader = new FileReader(nameTransform(keyName.toUri(), extension).FullName);
                // Use "try/finally instead of "try-with-resources" or "using"
                // which are not supported before Java 7.
                try {
                    String line = null;
                    while ((line = reader.readLine()) != null)
                        contents.append(line);
                } finally {
                    reader.close();
                }
            } catch (SecurityException e) {
                throw new SecurityException(
                        "FilePrivateKeyStorage: Failed to read key: "
                                + e.Message);
            } catch (IOException e_0) {
                throw new SecurityException(
                        "FilePrivateKeyStorage: Failed to read key: "
                                + e_0.Message);
            }

            return net.named_data.jndn.util.Common.base64Decode(contents.toString());
        }