| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Ini.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.config</a> > <span class="el_source">Ini.java</span></div><h1>Ini.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.config; |
| |
| import org.apache.shiro.io.ResourceUtils; |
| import org.apache.shiro.util.CollectionUtils; |
| import org.apache.shiro.util.StringUtils; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.io.Reader; |
| import java.io.UnsupportedEncodingException; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.LinkedHashMap; |
| import java.util.Map; |
| import java.util.Scanner; |
| import java.util.Set; |
| |
| /** |
| * A class representing the <a href="http://en.wikipedia.org/wiki/INI_file">INI</a> text configuration format. |
| * <p/> |
| * An Ini instance is a map of {@link Ini.Section Section}s, keyed by section name. Each |
| * {@code Section} is itself a map of {@code String} name/value pairs. Name/value pairs are guaranteed to be unique |
| * within each {@code Section} only - not across the entire {@code Ini} instance. |
| * |
| * @since 1.0 |
| */ |
| public class Ini implements Map<String, Ini.Section> { |
| |
| <span class="fc" id="L50"> private static transient final Logger log = LoggerFactory.getLogger(Ini.class);</span> |
| |
| public static final String DEFAULT_SECTION_NAME = ""; //empty string means the first unnamed section |
| public static final String DEFAULT_CHARSET_NAME = "UTF-8"; |
| |
| public static final String COMMENT_POUND = "#"; |
| public static final String COMMENT_SEMICOLON = ";"; |
| public static final String SECTION_PREFIX = "["; |
| public static final String SECTION_SUFFIX = "]"; |
| |
| protected static final char ESCAPE_TOKEN = '\\'; |
| |
| private final Map<String, Section> sections; |
| |
| /** |
| * Creates a new empty {@code Ini} instance. |
| */ |
| <span class="fc" id="L67"> public Ini() {</span> |
| <span class="fc" id="L68"> this.sections = new LinkedHashMap<String, Section>();</span> |
| <span class="fc" id="L69"> }</span> |
| |
| /** |
| * Creates a new {@code Ini} instance with the specified defaults. |
| * |
| * @param defaults the default sections and/or key-value pairs to copy into the new instance. |
| */ |
| public Ini(Ini defaults) { |
| <span class="nc" id="L77"> this();</span> |
| <span class="nc bnc" id="L78" title="All 2 branches missed."> if (defaults == null) {</span> |
| <span class="nc" id="L79"> throw new NullPointerException("Defaults cannot be null.");</span> |
| } |
| <span class="nc bnc" id="L81" title="All 2 branches missed."> for (Section section : defaults.getSections()) {</span> |
| <span class="nc" id="L82"> Section copy = new Section(section);</span> |
| <span class="nc" id="L83"> this.sections.put(section.getName(), copy);</span> |
| <span class="nc" id="L84"> }</span> |
| <span class="nc" id="L85"> }</span> |
| |
| /** |
| * Returns {@code true} if no sections have been configured, or if there are sections, but the sections themselves |
| * are all empty, {@code false} otherwise. |
| * |
| * @return {@code true} if no sections have been configured, or if there are sections, but the sections themselves |
| * are all empty, {@code false} otherwise. |
| */ |
| public boolean isEmpty() { |
| <span class="fc" id="L95"> Collection<Section> sections = this.sections.values();</span> |
| <span class="fc bfc" id="L96" title="All 2 branches covered."> if (!sections.isEmpty()) {</span> |
| <span class="pc bpc" id="L97" title="1 of 2 branches missed."> for (Section section : sections) {</span> |
| <span class="pc bpc" id="L98" title="1 of 2 branches missed."> if (!section.isEmpty()) {</span> |
| <span class="fc" id="L99"> return false;</span> |
| } |
| <span class="nc" id="L101"> }</span> |
| } |
| <span class="fc" id="L103"> return true;</span> |
| } |
| |
| /** |
| * Returns the names of all sections managed by this {@code Ini} instance or an empty collection if there are |
| * no sections. |
| * |
| * @return the names of all sections managed by this {@code Ini} instance or an empty collection if there are |
| * no sections. |
| */ |
| public Set<String> getSectionNames() { |
| <span class="nc" id="L114"> return Collections.unmodifiableSet(sections.keySet());</span> |
| } |
| |
| /** |
| * Returns the sections managed by this {@code Ini} instance or an empty collection if there are |
| * no sections. |
| * |
| * @return the sections managed by this {@code Ini} instance or an empty collection if there are |
| * no sections. |
| */ |
| public Collection<Section> getSections() { |
| <span class="fc" id="L125"> return Collections.unmodifiableCollection(sections.values());</span> |
| } |
| |
| /** |
| * Returns the {@link Section} with the given name or {@code null} if no section with that name exists. |
| * |
| * @param sectionName the name of the section to retrieve. |
| * @return the {@link Section} with the given name or {@code null} if no section with that name exists. |
| */ |
| public Section getSection(String sectionName) { |
| <span class="fc" id="L135"> String name = cleanName(sectionName);</span> |
| <span class="fc" id="L136"> return sections.get(name);</span> |
| } |
| |
| /** |
| * Ensures a section with the specified name exists, adding a new one if it does not yet exist. |
| * |
| * @param sectionName the name of the section to ensure existence |
| * @return the section created if it did not yet exist, or the existing Section that already existed. |
| */ |
| public Section addSection(String sectionName) { |
| <span class="fc" id="L146"> String name = cleanName(sectionName);</span> |
| <span class="fc" id="L147"> Section section = getSection(name);</span> |
| <span class="pc bpc" id="L148" title="1 of 2 branches missed."> if (section == null) {</span> |
| <span class="fc" id="L149"> section = new Section(name);</span> |
| <span class="fc" id="L150"> this.sections.put(name, section);</span> |
| } |
| <span class="fc" id="L152"> return section;</span> |
| } |
| |
| /** |
| * Removes the section with the specified name and returns it, or {@code null} if the section did not exist. |
| * |
| * @param sectionName the name of the section to remove. |
| * @return the section with the specified name or {@code null} if the section did not exist. |
| */ |
| public Section removeSection(String sectionName) { |
| <span class="nc" id="L162"> String name = cleanName(sectionName);</span> |
| <span class="nc" id="L163"> return this.sections.remove(name);</span> |
| } |
| |
| private static String cleanName(String sectionName) { |
| <span class="fc" id="L167"> String name = StringUtils.clean(sectionName);</span> |
| <span class="fc bfc" id="L168" title="All 2 branches covered."> if (name == null) {</span> |
| <span class="fc" id="L169"> log.trace("Specified name was null or empty. Defaulting to the default section (name = \"\")");</span> |
| <span class="fc" id="L170"> name = DEFAULT_SECTION_NAME;</span> |
| } |
| <span class="fc" id="L172"> return name;</span> |
| } |
| |
| /** |
| * Sets a name/value pair for the section with the given {@code sectionName}. If the section does not yet exist, |
| * it will be created. If the {@code sectionName} is null or empty, the name/value pair will be placed in the |
| * default (unnamed, empty string) section. |
| * |
| * @param sectionName the name of the section to add the name/value pair |
| * @param propertyName the name of the property to add |
| * @param propertyValue the property value |
| */ |
| public void setSectionProperty(String sectionName, String propertyName, String propertyValue) { |
| <span class="fc" id="L185"> String name = cleanName(sectionName);</span> |
| <span class="fc" id="L186"> Section section = getSection(name);</span> |
| <span class="fc bfc" id="L187" title="All 2 branches covered."> if (section == null) {</span> |
| <span class="fc" id="L188"> section = addSection(name);</span> |
| } |
| <span class="fc" id="L190"> section.put(propertyName, propertyValue);</span> |
| <span class="fc" id="L191"> }</span> |
| |
| /** |
| * Returns the value of the specified section property, or {@code null} if the section or property do not exist. |
| * |
| * @param sectionName the name of the section to retrieve to acquire the property value |
| * @param propertyName the name of the section property for which to return the value |
| * @return the value of the specified section property, or {@code null} if the section or property do not exist. |
| */ |
| public String getSectionProperty(String sectionName, String propertyName) { |
| <span class="nc" id="L201"> Section section = getSection(sectionName);</span> |
| <span class="nc bnc" id="L202" title="All 2 branches missed."> return section != null ? section.get(propertyName) : null;</span> |
| } |
| |
| /** |
| * Returns the value of the specified section property, or the {@code defaultValue} if the section or |
| * property do not exist. |
| * |
| * @param sectionName the name of the section to add the name/value pair |
| * @param propertyName the name of the property to add |
| * @param defaultValue the default value to return if the section or property do not exist. |
| * @return the value of the specified section property, or the {@code defaultValue} if the section or |
| * property do not exist. |
| */ |
| public String getSectionProperty(String sectionName, String propertyName, String defaultValue) { |
| <span class="nc" id="L216"> String value = getSectionProperty(sectionName, propertyName);</span> |
| <span class="nc bnc" id="L217" title="All 2 branches missed."> return value != null ? value : defaultValue;</span> |
| } |
| |
| /** |
| * Creates a new {@code Ini} instance loaded with the INI-formatted data in the resource at the given path. The |
| * resource path may be any value interpretable by the |
| * {@link ResourceUtils#getInputStreamForPath(String) ResourceUtils.getInputStreamForPath} method. |
| * |
| * @param resourcePath the resource location of the INI data to load when creating the {@code Ini} instance. |
| * @return a new {@code Ini} instance loaded with the INI-formatted data in the resource at the given path. |
| * @throws ConfigurationException if the path cannot be loaded into an {@code Ini} instance. |
| */ |
| public static Ini fromResourcePath(String resourcePath) throws ConfigurationException { |
| <span class="pc bpc" id="L230" title="1 of 2 branches missed."> if (!StringUtils.hasLength(resourcePath)) {</span> |
| <span class="nc" id="L231"> throw new IllegalArgumentException("Resource Path argument cannot be null or empty.");</span> |
| } |
| <span class="fc" id="L233"> Ini ini = new Ini();</span> |
| <span class="fc" id="L234"> ini.loadFromPath(resourcePath);</span> |
| <span class="fc" id="L235"> return ini;</span> |
| } |
| |
| /** |
| * Loads data from the specified resource path into this current {@code Ini} instance. The |
| * resource path may be any value interpretable by the |
| * {@link ResourceUtils#getInputStreamForPath(String) ResourceUtils.getInputStreamForPath} method. |
| * |
| * @param resourcePath the resource location of the INI data to load into this instance. |
| * @throws ConfigurationException if the path cannot be loaded |
| */ |
| public void loadFromPath(String resourcePath) throws ConfigurationException { |
| InputStream is; |
| try { |
| <span class="fc" id="L249"> is = ResourceUtils.getInputStreamForPath(resourcePath);</span> |
| <span class="nc" id="L250"> } catch (IOException e) {</span> |
| <span class="nc" id="L251"> throw new ConfigurationException(e);</span> |
| <span class="fc" id="L252"> }</span> |
| <span class="fc" id="L253"> load(is);</span> |
| <span class="fc" id="L254"> }</span> |
| |
| /** |
| * Loads the specified raw INI-formatted text into this instance. |
| * |
| * @param iniConfig the raw INI-formatted text to load into this instance. |
| * @throws ConfigurationException if the text cannot be loaded |
| */ |
| public void load(String iniConfig) throws ConfigurationException { |
| <span class="fc" id="L263"> load(new Scanner(iniConfig));</span> |
| <span class="fc" id="L264"> }</span> |
| |
| /** |
| * Loads the INI-formatted text backed by the given InputStream into this instance. This implementation will |
| * close the input stream after it has finished loading. It is expected that the stream's contents are |
| * UTF-8 encoded. |
| * |
| * @param is the {@code InputStream} from which to read the INI-formatted text |
| * @throws ConfigurationException if unable |
| */ |
| public void load(InputStream is) throws ConfigurationException { |
| <span class="pc bpc" id="L275" title="1 of 2 branches missed."> if (is == null) {</span> |
| <span class="nc" id="L276"> throw new NullPointerException("InputStream argument cannot be null.");</span> |
| } |
| InputStreamReader isr; |
| try { |
| <span class="fc" id="L280"> isr = new InputStreamReader(is, DEFAULT_CHARSET_NAME);</span> |
| <span class="nc" id="L281"> } catch (UnsupportedEncodingException e) {</span> |
| <span class="nc" id="L282"> throw new ConfigurationException(e);</span> |
| <span class="fc" id="L283"> }</span> |
| <span class="fc" id="L284"> load(isr);</span> |
| <span class="fc" id="L285"> }</span> |
| |
| /** |
| * Loads the INI-formatted text backed by the given Reader into this instance. This implementation will close the |
| * reader after it has finished loading. |
| * |
| * @param reader the {@code Reader} from which to read the INI-formatted text |
| */ |
| public void load(Reader reader) { |
| <span class="fc" id="L294"> Scanner scanner = new Scanner(reader);</span> |
| try { |
| <span class="fc" id="L296"> load(scanner);</span> |
| } finally { |
| <span class="nc" id="L298"> try {</span> |
| <span class="pc" id="L299"> scanner.close();</span> |
| <span class="nc" id="L300"> } catch (Exception e) {</span> |
| <span class="nc" id="L301"> log.debug("Unable to cleanly close the InputStream scanner. Non-critical - ignoring.", e);</span> |
| <span class="pc" id="L302"> }</span> |
| <span class="nc" id="L303"> }</span> |
| <span class="fc" id="L304"> }</span> |
| |
| private void addSection(String name, StringBuilder content) { |
| <span class="fc bfc" id="L307" title="All 2 branches covered."> if (content.length() > 0) {</span> |
| <span class="fc" id="L308"> String contentString = content.toString();</span> |
| <span class="fc" id="L309"> String cleaned = StringUtils.clean(contentString);</span> |
| <span class="pc bpc" id="L310" title="1 of 2 branches missed."> if (cleaned != null) {</span> |
| <span class="fc" id="L311"> Section section = new Section(name, contentString);</span> |
| <span class="pc bpc" id="L312" title="1 of 2 branches missed."> if (!section.isEmpty()) {</span> |
| <span class="fc" id="L313"> sections.put(name, section);</span> |
| } |
| } |
| } |
| <span class="fc" id="L317"> }</span> |
| |
| /** |
| * Loads the INI-formatted text backed by the given Scanner. This implementation will close the |
| * scanner after it has finished loading. |
| * |
| * @param scanner the {@code Scanner} from which to read the INI-formatted text |
| */ |
| public void load(Scanner scanner) { |
| |
| <span class="fc" id="L327"> String sectionName = DEFAULT_SECTION_NAME;</span> |
| <span class="fc" id="L328"> StringBuilder sectionContent = new StringBuilder();</span> |
| |
| <span class="fc bfc" id="L330" title="All 2 branches covered."> while (scanner.hasNextLine()) {</span> |
| |
| <span class="fc" id="L332"> String rawLine = scanner.nextLine();</span> |
| <span class="fc" id="L333"> String line = StringUtils.clean(rawLine);</span> |
| |
| <span class="fc bfc" id="L335" title="All 6 branches covered."> if (line == null || line.startsWith(COMMENT_POUND) || line.startsWith(COMMENT_SEMICOLON)) {</span> |
| //skip empty lines and comments: |
| <span class="fc" id="L337"> continue;</span> |
| } |
| |
| <span class="fc" id="L340"> String newSectionName = getSectionName(line);</span> |
| <span class="fc bfc" id="L341" title="All 2 branches covered."> if (newSectionName != null) {</span> |
| //found a new section - convert the currently buffered one into a Section object |
| <span class="fc" id="L343"> addSection(sectionName, sectionContent);</span> |
| |
| //reset the buffer for the new section: |
| <span class="fc" id="L346"> sectionContent = new StringBuilder();</span> |
| |
| <span class="fc" id="L348"> sectionName = newSectionName;</span> |
| |
| <span class="pc bpc" id="L350" title="1 of 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="fc" id="L351"> log.debug("Parsing " + SECTION_PREFIX + sectionName + SECTION_SUFFIX);</span> |
| } |
| } else { |
| //normal line - add it to the existing content buffer: |
| <span class="fc" id="L355"> sectionContent.append(rawLine).append("\n");</span> |
| } |
| <span class="fc" id="L357"> }</span> |
| |
| //finish any remaining buffered content: |
| <span class="fc" id="L360"> addSection(sectionName, sectionContent);</span> |
| <span class="fc" id="L361"> }</span> |
| |
| protected static boolean isSectionHeader(String line) { |
| <span class="fc" id="L364"> String s = StringUtils.clean(line);</span> |
| <span class="pc bpc" id="L365" title="2 of 6 branches missed."> return s != null && s.startsWith(SECTION_PREFIX) && s.endsWith(SECTION_SUFFIX);</span> |
| } |
| |
| protected static String getSectionName(String line) { |
| <span class="fc" id="L369"> String s = StringUtils.clean(line);</span> |
| <span class="fc bfc" id="L370" title="All 2 branches covered."> if (isSectionHeader(s)) {</span> |
| <span class="fc" id="L371"> return cleanName(s.substring(1, s.length() - 1));</span> |
| } |
| <span class="fc" id="L373"> return null;</span> |
| } |
| |
| public boolean equals(Object obj) { |
| <span class="nc bnc" id="L377" title="All 2 branches missed."> if (obj instanceof Ini) {</span> |
| <span class="nc" id="L378"> Ini ini = (Ini) obj;</span> |
| <span class="nc" id="L379"> return this.sections.equals(ini.sections);</span> |
| } |
| <span class="nc" id="L381"> return false;</span> |
| } |
| |
| @Override |
| public int hashCode() { |
| <span class="nc" id="L386"> return this.sections.hashCode();</span> |
| } |
| |
| public String toString() { |
| <span class="pc bpc" id="L390" title="1 of 2 branches missed."> if (CollectionUtils.isEmpty(this.sections)) {</span> |
| <span class="nc" id="L391"> return "<empty INI>";</span> |
| } else { |
| <span class="fc" id="L393"> StringBuilder sb = new StringBuilder("sections=");</span> |
| <span class="fc" id="L394"> int i = 0;</span> |
| <span class="fc bfc" id="L395" title="All 2 branches covered."> for (Ini.Section section : this.sections.values()) {</span> |
| <span class="fc bfc" id="L396" title="All 2 branches covered."> if (i > 0) {</span> |
| <span class="fc" id="L397"> sb.append(",");</span> |
| } |
| <span class="fc" id="L399"> sb.append(section.toString());</span> |
| <span class="fc" id="L400"> i++;</span> |
| <span class="fc" id="L401"> }</span> |
| <span class="fc" id="L402"> return sb.toString();</span> |
| } |
| } |
| |
| public int size() { |
| <span class="nc" id="L407"> return this.sections.size();</span> |
| } |
| |
| public boolean containsKey(Object key) { |
| <span class="nc" id="L411"> return this.sections.containsKey(key);</span> |
| } |
| |
| public boolean containsValue(Object value) { |
| <span class="nc" id="L415"> return this.sections.containsValue(value);</span> |
| } |
| |
| public Section get(Object key) { |
| <span class="nc" id="L419"> return this.sections.get(key);</span> |
| } |
| |
| public Section put(String key, Section value) { |
| <span class="nc" id="L423"> return this.sections.put(key, value);</span> |
| } |
| |
| public Section remove(Object key) { |
| <span class="nc" id="L427"> return this.sections.remove(key);</span> |
| } |
| |
| public void putAll(Map<? extends String, ? extends Section> m) { |
| <span class="nc" id="L431"> this.sections.putAll(m);</span> |
| <span class="nc" id="L432"> }</span> |
| |
| public void clear() { |
| <span class="nc" id="L435"> this.sections.clear();</span> |
| <span class="nc" id="L436"> }</span> |
| |
| public Set<String> keySet() { |
| <span class="nc" id="L439"> return Collections.unmodifiableSet(this.sections.keySet());</span> |
| } |
| |
| public Collection<Section> values() { |
| <span class="nc" id="L443"> return Collections.unmodifiableCollection(this.sections.values());</span> |
| } |
| |
| public Set<Entry<String, Section>> entrySet() { |
| <span class="nc" id="L447"> return Collections.unmodifiableSet(this.sections.entrySet());</span> |
| } |
| |
| /** |
| * An {@code Ini.Section} is String-key-to-String-value Map, identifiable by a |
| * {@link #getName() name} unique within an {@link Ini} instance. |
| */ |
| public static class Section implements Map<String, String> { |
| private final String name; |
| private final Map<String, String> props; |
| |
| <span class="fc" id="L458"> private Section(String name) {</span> |
| <span class="pc bpc" id="L459" title="1 of 2 branches missed."> if (name == null) {</span> |
| <span class="nc" id="L460"> throw new NullPointerException("name");</span> |
| } |
| <span class="fc" id="L462"> this.name = name;</span> |
| <span class="fc" id="L463"> this.props = new LinkedHashMap<String, String>();</span> |
| <span class="fc" id="L464"> }</span> |
| |
| <span class="fc" id="L466"> private Section(String name, String sectionContent) {</span> |
| <span class="pc bpc" id="L467" title="1 of 2 branches missed."> if (name == null) {</span> |
| <span class="nc" id="L468"> throw new NullPointerException("name");</span> |
| } |
| <span class="fc" id="L470"> this.name = name;</span> |
| Map<String,String> props; |
| <span class="pc bpc" id="L472" title="1 of 2 branches missed."> if (StringUtils.hasText(sectionContent) ) {</span> |
| <span class="fc" id="L473"> props = toMapProps(sectionContent);</span> |
| } else { |
| <span class="nc" id="L475"> props = new LinkedHashMap<String,String>();</span> |
| } |
| <span class="pc bpc" id="L477" title="1 of 2 branches missed."> if ( props != null ) {</span> |
| <span class="fc" id="L478"> this.props = props;</span> |
| } else { |
| <span class="nc" id="L480"> this.props = new LinkedHashMap<String,String>();</span> |
| } |
| <span class="fc" id="L482"> }</span> |
| |
| private Section(Section defaults) { |
| <span class="nc" id="L485"> this(defaults.getName());</span> |
| <span class="nc" id="L486"> putAll(defaults.props);</span> |
| <span class="nc" id="L487"> }</span> |
| |
| //Protected to access in a test case - NOT considered part of Shiro's public API |
| |
| protected static boolean isContinued(String line) { |
| <span class="pc bpc" id="L492" title="1 of 2 branches missed."> if (!StringUtils.hasText(line)) {</span> |
| <span class="nc" id="L493"> return false;</span> |
| } |
| <span class="fc" id="L495"> int length = line.length();</span> |
| //find the number of backslashes at the end of the line. If an even number, the |
| //backslashes are considered escaped. If an odd number, the line is considered continued on the next line |
| <span class="fc" id="L498"> int backslashCount = 0;</span> |
| <span class="pc bpc" id="L499" title="1 of 2 branches missed."> for (int i = length - 1; i > 0; i--) {</span> |
| <span class="fc bfc" id="L500" title="All 2 branches covered."> if (line.charAt(i) == ESCAPE_TOKEN) {</span> |
| <span class="fc" id="L501"> backslashCount++;</span> |
| } else { |
| break; |
| } |
| } |
| <span class="fc bfc" id="L506" title="All 2 branches covered."> return backslashCount % 2 != 0;</span> |
| } |
| |
| private static boolean isKeyValueSeparatorChar(char c) { |
| <span class="fc bfc" id="L510" title="All 6 branches covered."> return Character.isWhitespace(c) || c == ':' || c == '=';</span> |
| } |
| |
| private static boolean isCharEscaped(CharSequence s, int index) { |
| <span class="pc bpc" id="L514" title="2 of 4 branches missed."> return index > 0 && s.charAt(index - 1) == ESCAPE_TOKEN;</span> |
| } |
| |
| //Protected to access in a test case - NOT considered part of Shiro's public API |
| protected static String[] splitKeyValue(String keyValueLine) { |
| <span class="fc" id="L519"> String line = StringUtils.clean(keyValueLine);</span> |
| <span class="pc bpc" id="L520" title="1 of 2 branches missed."> if (line == null) {</span> |
| <span class="nc" id="L521"> return null;</span> |
| } |
| <span class="fc" id="L523"> StringBuilder keyBuffer = new StringBuilder();</span> |
| <span class="fc" id="L524"> StringBuilder valueBuffer = new StringBuilder();</span> |
| |
| <span class="fc" id="L526"> boolean buildingKey = true; //we'll build the value next:</span> |
| |
| <span class="fc bfc" id="L528" title="All 2 branches covered."> for (int i = 0; i < line.length(); i++) {</span> |
| <span class="fc" id="L529"> char c = line.charAt(i);</span> |
| |
| <span class="fc bfc" id="L531" title="All 2 branches covered."> if (buildingKey) {</span> |
| <span class="pc bpc" id="L532" title="1 of 4 branches missed."> if (isKeyValueSeparatorChar(c) && !isCharEscaped(line, i)) {</span> |
| <span class="fc" id="L533"> buildingKey = false;//now start building the value</span> |
| } else { |
| <span class="fc" id="L535"> keyBuffer.append(c);</span> |
| } |
| } else { |
| <span class="pc bpc" id="L538" title="1 of 6 branches missed."> if (valueBuffer.length() == 0 && isKeyValueSeparatorChar(c) && !isCharEscaped(line, i)) {</span> |
| //swallow the separator chars before we start building the value |
| } else { |
| <span class="fc" id="L541"> valueBuffer.append(c);</span> |
| } |
| } |
| } |
| |
| <span class="fc" id="L546"> String key = StringUtils.clean(keyBuffer.toString());</span> |
| <span class="fc" id="L547"> String value = StringUtils.clean(valueBuffer.toString());</span> |
| |
| <span class="pc bpc" id="L549" title="1 of 4 branches missed."> if (key == null || value == null) {</span> |
| <span class="fc" id="L550"> String msg = "Line argument must contain a key and a value. Only one string token was found.";</span> |
| <span class="fc" id="L551"> throw new IllegalArgumentException(msg);</span> |
| } |
| |
| <span class="fc" id="L554"> log.trace("Discovered key/value pair: {} = {}", key, value);</span> |
| |
| <span class="fc" id="L556"> return new String[]{key, value};</span> |
| } |
| |
| private static Map<String, String> toMapProps(String content) { |
| <span class="fc" id="L560"> Map<String, String> props = new LinkedHashMap<String, String>();</span> |
| String line; |
| <span class="fc" id="L562"> StringBuilder lineBuffer = new StringBuilder();</span> |
| <span class="fc" id="L563"> Scanner scanner = new Scanner(content);</span> |
| <span class="fc bfc" id="L564" title="All 2 branches covered."> while (scanner.hasNextLine()) {</span> |
| <span class="fc" id="L565"> line = StringUtils.clean(scanner.nextLine());</span> |
| <span class="fc bfc" id="L566" title="All 2 branches covered."> if (isContinued(line)) {</span> |
| //strip off the last continuation backslash: |
| <span class="fc" id="L568"> line = line.substring(0, line.length() - 1);</span> |
| <span class="fc" id="L569"> lineBuffer.append(line);</span> |
| <span class="fc" id="L570"> continue;</span> |
| } else { |
| <span class="fc" id="L572"> lineBuffer.append(line);</span> |
| } |
| <span class="fc" id="L574"> line = lineBuffer.toString();</span> |
| <span class="fc" id="L575"> lineBuffer = new StringBuilder();</span> |
| <span class="fc" id="L576"> String[] kvPair = splitKeyValue(line);</span> |
| <span class="fc" id="L577"> props.put(kvPair[0], kvPair[1]);</span> |
| <span class="fc" id="L578"> }</span> |
| |
| <span class="fc" id="L580"> return props;</span> |
| } |
| |
| public String getName() { |
| <span class="fc" id="L584"> return this.name;</span> |
| } |
| |
| public void clear() { |
| <span class="nc" id="L588"> this.props.clear();</span> |
| <span class="nc" id="L589"> }</span> |
| |
| public boolean containsKey(Object key) { |
| <span class="nc" id="L592"> return this.props.containsKey(key);</span> |
| } |
| |
| public boolean containsValue(Object value) { |
| <span class="nc" id="L596"> return this.props.containsValue(value);</span> |
| } |
| |
| public Set<Entry<String, String>> entrySet() { |
| <span class="fc" id="L600"> return this.props.entrySet();</span> |
| } |
| |
| public String get(Object key) { |
| <span class="fc" id="L604"> return this.props.get(key);</span> |
| } |
| |
| public boolean isEmpty() { |
| <span class="fc" id="L608"> return this.props.isEmpty();</span> |
| } |
| |
| public Set<String> keySet() { |
| <span class="fc" id="L612"> return this.props.keySet();</span> |
| } |
| |
| public String put(String key, String value) { |
| <span class="fc" id="L616"> return this.props.put(key, value);</span> |
| } |
| |
| public void putAll(Map<? extends String, ? extends String> m) { |
| <span class="nc" id="L620"> this.props.putAll(m);</span> |
| <span class="nc" id="L621"> }</span> |
| |
| public String remove(Object key) { |
| <span class="nc" id="L624"> return this.props.remove(key);</span> |
| } |
| |
| public int size() { |
| <span class="fc" id="L628"> return this.props.size();</span> |
| } |
| |
| public Collection<String> values() { |
| <span class="nc" id="L632"> return this.props.values();</span> |
| } |
| |
| public String toString() { |
| <span class="fc" id="L636"> String name = getName();</span> |
| <span class="pc bpc" id="L637" title="1 of 2 branches missed."> if (DEFAULT_SECTION_NAME.equals(name)) {</span> |
| <span class="nc" id="L638"> return "<default>";</span> |
| } |
| <span class="fc" id="L640"> return name;</span> |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| <span class="nc bnc" id="L645" title="All 2 branches missed."> if (obj instanceof Section) {</span> |
| <span class="nc" id="L646"> Section other = (Section) obj;</span> |
| <span class="nc bnc" id="L647" title="All 4 branches missed."> return getName().equals(other.getName()) && this.props.equals(other.props);</span> |
| } |
| <span class="nc" id="L649"> return false;</span> |
| } |
| |
| @Override |
| public int hashCode() { |
| <span class="nc" id="L654"> return this.name.hashCode() * 31 + this.props.hashCode();</span> |
| } |
| } |
| |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |