View Javadoc
1   /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.transformer;
35  
36  /**
37   * This introduces to encapsulate the type of problem and the relevant properties.
38   */
39  public interface TransformationProblem {
40  
41      static Builder error(String message, Object... args) {
42          return new Builder()
43                      .withSeverityType(SeverityType.ERROR)
44                      .withMessage(String.format(message, args));
45      }
46  
47      static Builder warning(String message, Object... args) {
48          return new Builder()
49                      .withSeverityType(SeverityType.WARNING)
50                      .withMessage(String.format(message, args));
51      }
52  
53      static Builder deprecated(String message, Object... args) {
54          return new Builder()
55                  .withSeverityType(SeverityType.DEPRECATED)
56                  .withMessage(String.format(message, args));
57      }
58  
59  
60      String getLocation();
61  
62      String getMessage();
63  
64      SeverityType getSeverityType();
65  
66      Exception getException();
67  
68      /**
69       * Possible types of messages.
70       */
71      enum SeverityType {
72          ERROR,
73          WARNING,
74          DEPRECATED
75      }
76  
77      /**
78       * {@link TransformationProblem} builder.
79       */
80      class Builder {
81  
82          private String message;
83          private SeverityType severityType;
84          private Exception exception = null;
85          private String location;
86  
87          public Builder withMessage(String message) {
88              this.message = message;
89              return this;
90          }
91  
92          public Builder withSeverityType(SeverityType severityType) {
93              this.severityType = severityType;
94              return this;
95          }
96  
97          public Builder withLocation(String location) {
98              this.location = location;
99              return this;
100         }
101 
102         public Builder withException(Exception exception) {
103             this.exception = exception;
104             return this;
105         }
106 
107         public TransformationProblem build() {
108             return new TransformationProblemImpl(message, location, severityType, exception);
109         }
110 
111         /**
112          * Default {@link TransformationProblem} implementation type.
113          */
114         class TransformationProblemImpl implements TransformationProblem {
115             private String location;
116             private String message;
117             private SeverityType severityType;
118             private Exception exception;
119 
120             public TransformationProblemImpl(String message, String location, SeverityType severityType, Exception exception) {
121                 this.location = location;
122                 this.message = message;
123                 this.severityType = severityType;
124                 this.exception = exception;
125             }
126 
127             @Override
128             public String getLocation() {
129                 return this.location;
130             }
131 
132             public String getMessage() {
133                 return this.message;
134             }
135 
136             public SeverityType getSeverityType() {
137                 return this.severityType;
138             }
139 
140             public Exception getException() {
141                 return this.exception;
142             }
143         }
144     }
145 
146 }