View Javadoc
1   /**
2    * This file Copyright (c) 2016 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      String getLocation();
54  
55      String getMessage();
56  
57      SeverityType getSeverityType();
58  
59      Exception getException();
60  
61      /**
62       * Possible types of messages.
63       */
64      enum SeverityType {
65          ERROR,
66          WARNING
67      }
68  
69      /**
70       * {@link TransformationProblem} builder.
71       */
72      class Builder {
73  
74          private String message;
75          private SeverityType severityType;
76          private Exception exception = null;
77          private String location;
78  
79          public Builder withMessage(String message) {
80              this.message = message;
81              return this;
82          }
83  
84          public Builder withSeverityType(SeverityType severityType) {
85              this.severityType = severityType;
86              return this;
87          }
88  
89          public Builder withLocation(String location) {
90              this.location = location;
91              return this;
92          }
93  
94          public Builder withException(Exception exception) {
95              this.exception = exception;
96              return this;
97          }
98  
99          public TransformationProblem build() {
100             return new TransformationProblemImpl(message, location, severityType, exception);
101         }
102 
103         /**
104          * Default {@link TransformationProblem} implementation type.
105          */
106         class TransformationProblemImpl implements TransformationProblem {
107             private String location;
108             private String message;
109             private SeverityType severityType;
110             private Exception exception;
111 
112             public TransformationProblemImpl(String message, String location, SeverityType severityType, Exception exception) {
113                 this.location = location;
114                 this.message = message;
115                 this.severityType = severityType;
116                 this.exception = exception;
117             }
118 
119             @Override
120             public String getLocation() {
121                 return this.location;
122             }
123 
124             public String getMessage() {
125                 return this.message;
126             }
127 
128             public SeverityType getSeverityType() {
129                 return this.severityType;
130             }
131 
132             public Exception getException() {
133                 return this.exception;
134             }
135         }
136     }
137 
138 }