| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Setting Values |
| |
| <p> |
| The following methods allow you to add, remove, and modify entries and sections in a config file: |
| </p> |
| <ul class='javatree'> |
| <li class='jc'>{@link oaj.config.Config} |
| <ul> |
| <li class='jm'>{@link oaj.config.Config#set(String,Object) set(String,Object)} |
| <li class='jm'>{@link oaj.config.Config#set(String,Object,Serializer) set(String,Object,Serializer)} |
| <li class='jm'>{@link oaj.config.Config#set(String,Object,Serializer,ConfigMod,String,List) set(String,Object,Serializer,ConfigMod,String,List)} |
| <li class='jm'>{@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List) set(String,Object,Serializer,ConfigMod[],String,List)} |
| <li class='jm'>{@link oaj.config.Config#set(String,String) set(String,String)} |
| <li class='jm'>{@link oaj.config.Config#remove(String) remove(String)} |
| <li class='jm'>{@link oaj.config.Config#setSection(String,List) setSection(String,List)} |
| <li class='jm'>{@link oaj.config.Config#setSection(String,List,Map) setSection(String,List,Map)} |
| <li class='jm'>{@link oaj.config.Config#removeSection(String) removeSection(String)} |
| </ul> |
| </ul> |
| <p class='bpcode w800'> |
| <jc>// Construct the sample config file programmatically</jc> |
| Config c = Config.<jsm>create</jsm>(<js>"MyConfig.cfg"</js>).build() |
| .set(<js>"key1"</js>, 1) |
| .set(<js>"key2"</js>, <jk>true</jk>) |
| .set(<js>"key3"</js>, <jk>new int</jk>[]{1,2,3}) |
| .set(<js>"key4"</js>, <jk>new</jk> URI(<js>"http://foo"</js>)) |
| .set(<js>"Section1/key1"</js>, 2) |
| .set(<js>"Section1/key2"</js>, <jk>false</jk>) |
| .set(<js>"Section1/key3"</js>, <jk>new int</jk>[]{4,5,6}) |
| .set(<js>"Section1/key4"</js>, <jk>new</jk> URI(<js>"http://bar"</js>)) |
| .commit(); |
| </p> |
| <p> |
| The method {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List)} can be used |
| for adding comments and pre-lines to entries, and specifying encoded values. |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Set an encoded value with some comments.</jc> |
| c.set(<js>"key1"</js>, 1, <jk>null</jk>, ConfigMod.<jsf>ENCODED</jsf>, <js>"Same-line comment"</js>, |
| Arrays.asList( |
| <js>"# Comment 1"</js>, <js>""</js>, <js>"# Comment 2"</js> |
| ) |
| ); |
| </p> |
| <p class='bpcode w800'> |
| <cc># Comment 1</cc> |
| |
| <cc># Comment 2</cc> |
| <ck>key1</ck> = <cv>1</cv> <cc># Same-line comment</cc> |
| </p> |
| <p> |
| The last 4 arguments in {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List)} |
| are optional in that if you pass <jk>null</jk>, the current value will not be overwritten. |
| To unset the same-line comment, you should pass in a blank string. |
| To remove pre-lines, you should pass in an empty list. |
| </p> |
| <p> |
| Sections can be added with optional pre-lines using the <c>setSection</c> methods: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Set an encoded value with some comments.</jc> |
| c.setSection(<js>"NewSection"</js>, |
| Arrays.asList( |
| <js>"# Section comment 1"</js>, <js>""</js>, <js>"# Section comment 2"</js> |
| ) |
| ); |
| </p> |
| <p class='bpcode w800'> |
| <cc># Section comment 1</cc> |
| |
| <cc># Section comment 2</cc> |
| <cs>[NewSection]</cs> |
| </p> |
| <p> |
| Changes made to the configuration are transactional in nature. |
| They are kept in memory until you call the {@link oaj.config.Config#commit()} method. |
| Until then, you still see the modified values when you call any of the getters, but the modified values |
| exist only in memory. |
| </p> |
| <p> |
| Changes can be rolled back using the {@link oaj.config.Config#rollback()} method. |
| </p> |