001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.Arrays;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Set;
029
030import ca.uhn.fhir.parser.IParser;
031
032/**
033 * This object supplies default configuration to all {@link IParser parser} instances
034 * created by a given {@link FhirContext}. It is accessed using {@link FhirContext#getParserOptions()}
035 * and {@link FhirContext#setParserOptions(ParserOptions)}.
036 * <p>
037 * It is fine to share a ParserOptions instances across multiple context instances.
038 * </p>
039 */
040public class ParserOptions {
041
042        private boolean myStripVersionsFromReferences = true;
043        private Set<String> myDontStripVersionsFromReferencesAtPaths = Collections.emptySet();
044        private boolean myOverrideResourceIdWithBundleEntryFullUrl = true;
045        
046        /**
047         * If supplied value(s), any resource references at the specified paths will have their
048         * resource versions encoded instead of being automatically stripped during the encoding
049         * process. This setting has no effect on the parsing process.
050         * <p>
051         * This method provides a finer-grained level of control than {@link #setStripVersionsFromReferences(boolean)}
052         * and any paths specified by this method will be encoded even if {@link #setStripVersionsFromReferences(boolean)}
053         * has been set to <code>true</code> (which is the default)
054         * </p>
055         *
056         * @param thePaths
057         *           A collection of paths for which the resource versions will not be removed automatically
058         *           when serializing, e.g. "Patient.managingOrganization" or "AuditEvent.object.reference". Note that
059         *           only resource name and field names with dots separating is allowed here (no repetition
060         *           indicators, FluentPath expressions, etc.)
061         * @see #setStripVersionsFromReferences(boolean)
062         * @return Returns a reference to <code>this</code> parser so that method calls can be chained together
063         */
064        public ParserOptions setDontStripVersionsFromReferencesAtPaths(String... thePaths) {
065                if (thePaths == null) {
066                        setDontStripVersionsFromReferencesAtPaths((List<String>) null);
067                } else {
068                        setDontStripVersionsFromReferencesAtPaths(Arrays.asList(thePaths));
069                }
070                return this;
071        }
072        
073        /**
074         * If set to <code>true<code> (which is the default), resource references containing a version
075         * will have the version removed when the resource is encoded. This is generally good behaviour because
076         * in most situations, references from one resource to another should be to the resource by ID, not
077         * by ID and version. In some cases though, it may be desirable to preserve the version in resource
078         * links. In that case, this value should be set to <code>false</code>.
079         * 
080         * @return Returns the parser instance's configuration setting for stripping versions from resource references when
081         *         encoding. Default is <code>true</code>.
082         */
083        public boolean isStripVersionsFromReferences() {
084                return myStripVersionsFromReferences ;
085        }
086
087        /**
088         * If set to <code>true<code> (which is the default), resource references containing a version
089         * will have the version removed when the resource is encoded. This is generally good behaviour because
090         * in most situations, references from one resource to another should be to the resource by ID, not
091         * by ID and version. In some cases though, it may be desirable to preserve the version in resource
092         * links. In that case, this value should be set to <code>false</code>.
093         * <p>
094         * This method provides the ability to globally disable reference encoding. If finer-grained
095         * control is needed, use {@link #setDontStripVersionsFromReferencesAtPaths(String...)}
096         * </p>
097         * @param theStripVersionsFromReferences
098         *           Set this to <code>false<code> to prevent the parser from removing
099         *           resource versions from references.
100         * @return Returns a reference to <code>this</code> parser so that method calls can be chained together
101         * @see #setDontStripVersionsFromReferencesAtPaths(String...)
102         */
103        public ParserOptions setStripVersionsFromReferences(boolean theStripVersionsFromReferences) {
104                myStripVersionsFromReferences = theStripVersionsFromReferences;
105                return this;
106        }
107
108        /**
109         * If supplied value(s), any resource references at the specified paths will have their
110         * resource versions encoded instead of being automatically stripped during the encoding
111         * process. This setting has no effect on the parsing process.
112         * <p>
113         * This method provides a finer-grained level of control than {@link #setStripVersionsFromReferences(boolean)}
114         * and any paths specified by this method will be encoded even if {@link #setStripVersionsFromReferences(boolean)}
115         * has been set to <code>true</code> (which is the default)
116         * </p>
117         *
118         * @param thePaths
119         *           A collection of paths for which the resource versions will not be removed automatically
120         *           when serializing, e.g. "Patient.managingOrganization" or "AuditEvent.object.reference". Note that
121         *           only resource name and field names with dots separating is allowed here (no repetition
122         *           indicators, FluentPath expressions, etc.)
123         * @see #setStripVersionsFromReferences(boolean)
124         * @return Returns a reference to <code>this</code> parser so that method calls can be chained together
125         */
126        @SuppressWarnings("unchecked")
127        public ParserOptions setDontStripVersionsFromReferencesAtPaths(Collection<String> thePaths) {
128                if (thePaths == null) {
129                        myDontStripVersionsFromReferencesAtPaths = Collections.emptySet();
130                } else if (thePaths instanceof HashSet) {
131                        myDontStripVersionsFromReferencesAtPaths = (Set<String>) ((HashSet<String>)thePaths).clone();
132                } else {
133                        myDontStripVersionsFromReferencesAtPaths = new HashSet<String>(thePaths);
134                }
135                return this;
136        }
137
138        /**
139         * Returns the value supplied to {@link IParser#setDontStripVersionsFromReferencesAtPaths(String...)}
140         * 
141         * @see #setDontStripVersionsFromReferencesAtPaths(String...)
142         * @see #setStripVersionsFromReferences(boolean)
143         */
144        public Set<String> getDontStripVersionsFromReferencesAtPaths() {
145                return myDontStripVersionsFromReferencesAtPaths;
146        }
147
148        /**
149         * If set to <code>true</code> (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's
150         * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this
151         * to <code>false</code> if this is not the desired behavior (e.g. the client code wishes to perform additional
152         * validation checks between the fullUrl and the resource id).
153         *
154         * @return Returns the parser instance's configuration setting for overriding resource ids with Bundle.entry.fullUrl when
155         *         parsing the source data into a Bundle object. Default is <code>true</code>.
156         */
157        public boolean isOverrideResourceIdWithBundleEntryFullUrl() {
158                return myOverrideResourceIdWithBundleEntryFullUrl;
159        }
160
161        /**
162         * If set to <code>true</code> (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's
163         * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this
164         * to <code>false</code> if this is not the desired behavior (e.g. the client code wishes to perform additional
165         * validation checks between the fullUrl and the resource id).
166         *
167         * @param theOverrideResourceIdWithBundleEntryFullUrl
168         *           Set this to <code>false</code> to prevent the parser from overriding resource ids with the
169         *           Bundle.entry.fullUrl
170         *
171         * @return Returns a reference to <code>this</code> parser so that method calls can be chained together
172         */
173        public ParserOptions setOverrideResourceIdWithBundleEntryFullUrl(boolean theOverrideResourceIdWithBundleEntryFullUrl) {
174                myOverrideResourceIdWithBundleEntryFullUrl = theOverrideResourceIdWithBundleEntryFullUrl;
175                return this;
176        }
177}