View Javadoc
1   /*
2    * Copyright (c) 2021 Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * bahlef - initial API and implementation and/or initial documentation
9    */
10  package de.funfried.netbeans.plugins.external.formatter.sql.jsqlformatter;
11  
12  import org.netbeans.api.annotations.common.CheckForNull;
13  
14  import com.manticore.jsqlformatter.JSQLFormatter;
15  
16  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
17  
18  /**
19   * Delegation class to the JSQLFormatter implementation.
20   *
21   * @author Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
22   */
23  public final class JSQLFormatterWrapper {
24  	/**
25  	 * Package private Constructor for creating a new instance of {@link JSQLFormatterWrapper}.
26  	 */
27  	JSQLFormatterWrapper() {
28  	}
29  
30  	/**
31  	 * Formats the given {@code code} with the given configurations and returns
32  	 * the formatted code.
33  	 *
34  	 * @param code the unformatted SQL code
35  	 * @param options an array of Formatting Options expressed as Key=Value pairs
36  	 *
37  	 * @return the formatted SQL code
38  	 *
39  	 * @throws FormattingFailedException if the external formatter failed to format the given code
40  	 */
41  	@CheckForNull
42  	public String format(String code, String... options) throws FormattingFailedException {
43  		if (code == null) {
44  			return null;
45  		}
46  
47  		try {
48  			return JSQLFormatter.format(code, options);
49  		} catch (Exception ex) {
50  			throw new FormattingFailedException(ex);
51  		}
52  	}
53  }