View Javadoc
1   /*
2    * Copyright (c) 2020 bahlef.
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.xml.base;
11  
12  import java.util.Collections;
13  import java.util.List;
14  import java.util.SortedSet;
15  
16  import javax.swing.text.BadLocationException;
17  import javax.swing.text.StyledDocument;
18  
19  import org.apache.commons.lang3.tuple.Pair;
20  import org.netbeans.api.annotations.common.CheckForNull;
21  
22  import de.funfried.netbeans.plugins.external.formatter.FormatJob;
23  import de.funfried.netbeans.plugins.external.formatter.FormatterService;
24  import de.funfried.netbeans.plugins.external.formatter.MimeType;
25  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
26  
27  /**
28   * Abstract base implementation of {@link FormatterService} for XML formatters.
29   *
30   * @author bahlef
31   */
32  public abstract class AbstractXmlFormatterService implements FormatterService {
33  	/**
34  	 * Returns the {@link FormatJob}.
35  	 *
36  	 * @param document the {@link StyledDocument} which should be formatted
37  	 */
38  	protected abstract FormatJob getFormatJob(StyledDocument document);
39  
40  	/**
41  	 * {@inheritDoc}
42  	 */
43  	@Override
44  	public boolean format(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) throws BadLocationException, FormattingFailedException {
45  		if (!canHandle(document)) {
46  			throw new FormattingFailedException("The file type '" + MimeType.getMimeTypeAsString(document) + "' is not supported");
47  		}
48  
49  		getFormatJob(document).format();
50  
51  		return true;
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	@Override
58  	public List<MimeType> getSupportedMimeTypes() {
59  		return Collections.singletonList(MimeType.XML);
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 */
65  	@Override
66  	@CheckForNull
67  	public Boolean organizeImports(StyledDocument document, boolean afterFixImports) throws BadLocationException {
68  		return null;
69  	}
70  }