001 /* 002 * Copyright 2005,2009 Ivan SZKIBA 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.ini4j.spi; 017 018 import org.ini4j.Config; 019 import org.ini4j.Options; 020 021 public class OptionsBuilder implements OptionsHandler 022 { 023 private boolean _header; 024 private String _lastComment; 025 private Options _options; 026 027 public static OptionsBuilder newInstance(Options opts) 028 { 029 OptionsBuilder instance = newInstance(); 030 031 instance.setOptions(opts); 032 033 return instance; 034 } 035 036 public void setOptions(Options value) 037 { 038 _options = value; 039 } 040 041 @Override public void endOptions() 042 { 043 044 // comment only .opt file ... 045 if ((_lastComment != null) && _header) 046 { 047 _options.setComment(_lastComment); 048 } 049 } 050 051 @Override public void handleComment(String comment) 052 { 053 if ((_lastComment != null) && _header) 054 { 055 _options.setComment(_lastComment); 056 _header = false; 057 } 058 059 _lastComment = comment; 060 } 061 062 @Override public void handleOption(String name, String value) 063 { 064 if (getConfig().isMultiOption()) 065 { 066 _options.add(name, value); 067 } 068 else 069 { 070 _options.put(name, value); 071 } 072 073 if (_lastComment != null) 074 { 075 if (_header) 076 { 077 _options.setComment(_lastComment); 078 } 079 else 080 { 081 _options.putComment(name, _lastComment); 082 } 083 084 _lastComment = null; 085 } 086 087 _header = false; 088 } 089 090 @Override public void startOptions() 091 { 092 _header = true; 093 } 094 095 protected static OptionsBuilder newInstance() 096 { 097 return ServiceFinder.findService(OptionsBuilder.class); 098 } 099 100 private Config getConfig() 101 { 102 return _options.getConfig(); 103 } 104 }