forked from fabric8io/fabric8-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fabric8io#1372): Set Kind/Filename mapping as AsciiDoc file (fabr…
…ic8io#1381) The mapping between Kind and Filename is moved to an AsciiDoc file, so it can be reused in documentation as well. By default, it loads this mapping from `/fabric8/default-kind-filename-mapping.adoc` classpath location, but using env variable or system property named `fabric8.mapping` you can specify your own file. Initially will inspect classpath and if it returns null, then it will treat `fabric8.mapping` value as a disk location.
- Loading branch information
1 parent
321b4b3
commit 71fca8b
Showing
16 changed files
with
591 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
core/src/main/java/io/fabric8/maven/core/util/AsciiDocParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package io.fabric8.maven.core.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* AsciiDoc utils for parsing specific elements coded in AsciiDoc format. | ||
*/ | ||
public class AsciiDocParser { | ||
|
||
private static final String END_TABLE = "|==="; | ||
|
||
/** | ||
* | ||
* This method serializes the content of an AsciiDoc table used to set the mapping between kinds and filenames. | ||
* These tables are of form: | ||
* | ||
* <pre> | ||
* [cols=2*,options="header"] | ||
* |=== | ||
* |Filename | ||
* |Kind | ||
* | ||
* a|ConfigMap | ||
* a|`cm` | ||
* | ||
* |CronJob | ||
* |cronjob | ||
* |=== | ||
* </pre> | ||
* | ||
* Basically the table contains an optional attributes section (@code{[]}), then the headers of the columns. | ||
* Between line breaks, both columns representing the kind (first column) and filename (second column). | ||
* Notice that one-line columns definition @code{|Cell in column 1, row 1|Cell in column 1, row 1} is not supported. | ||
* | ||
* This method returns an @code{IllegalArgumentException} if does not contain two columns. | ||
* | ||
* @param table definition in AsciiDoc format. | ||
* @return A serialization of all columns, being pair elements the first column and the odd elements the second column. In previous example @code{"cm","ConfigMap","cronjob","CronJob"} | ||
*/ | ||
public Map<String, List<String>> serializeKindFilenameTable(final InputStream table) { | ||
|
||
final Map<String, List<String>> serializedContent = new HashMap<>(); | ||
|
||
try (final BufferedReader tableContent = new BufferedReader(new InputStreamReader(table))) { | ||
|
||
skipUntilColumns(tableContent); | ||
|
||
boolean endTable = false; | ||
|
||
while (!endTable) { | ||
final List<String> readRow = readRow(tableContent); | ||
final String separator = readEmptyLineOrEndTable(tableContent); | ||
|
||
String kind = readRow.get(0); | ||
serializedContent.put(kind, readRow.subList(1, readRow.size())); | ||
|
||
if (END_TABLE.equals(separator)) { | ||
endTable = true; | ||
} | ||
|
||
} | ||
|
||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
||
return serializedContent; | ||
|
||
} | ||
|
||
private List<String> readRow(final BufferedReader tableContent) throws IOException { | ||
final List<String> content = new ArrayList<>(); | ||
|
||
final String firstColumn = readColumn(tableContent); | ||
final String secondColumn = readColumn(tableContent); | ||
|
||
content.add(firstColumn); | ||
final String[] filenameTypes = secondColumn.split(","); | ||
|
||
for (String filenameType : filenameTypes) { | ||
content.add(filenameType.trim()); | ||
} | ||
|
||
return content; | ||
} | ||
|
||
private String readColumn(final BufferedReader tableContent) throws IOException { | ||
final String column = tableContent.readLine(); | ||
|
||
if(column == null || column.isEmpty()) { | ||
throw new IllegalArgumentException("Trying to read a column but white line or EOF was found."); | ||
} | ||
|
||
int separator; | ||
if ((separator = column.indexOf("|")) < 0) { | ||
throw new IllegalArgumentException(String.format("Expected the initial of a column with (|) but %s found.", column)); | ||
} | ||
|
||
return column.trim().substring(separator + 1) | ||
.replaceAll("[`_*]", "") | ||
.trim(); | ||
|
||
} | ||
|
||
/** | ||
* Reads empty line or throw an exception if a none empty line was found. | ||
*/ | ||
private String readEmptyLineOrEndTable(final BufferedReader tableContent) throws IOException { | ||
final String column = tableContent.readLine(); | ||
|
||
if (column != null && column.startsWith(END_TABLE)) { | ||
return END_TABLE; | ||
} | ||
|
||
if(column == null || !column.isEmpty()) { | ||
throw new IllegalArgumentException(String.format("Trying to read an empty line for end of row, but content %s was found or EOF", column)); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
/** | ||
* Moves buffer until it finds the first content column (skipping headers). | ||
* @param tableContent | ||
*/ | ||
private void skipUntilColumns(final BufferedReader tableContent) throws IOException { | ||
String line; | ||
while ((line = tableContent.readLine()) != null) { | ||
if(line.trim().isEmpty()){ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
core/src/main/java/io/fabric8/maven/core/util/kubernetes/KindFilenameMapperUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.maven.core.util.kubernetes; | ||
|
||
import io.fabric8.maven.core.util.AsciiDocParser; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class KindFilenameMapperUtil { | ||
|
||
public static Map<String, List<String>> loadMappings() { | ||
|
||
final String location = "/META-INF/fabric8/kind-fileindicator-mapping-default.adoc"; | ||
|
||
try (final InputStream mappingFile = loadContent(location)) { | ||
final AsciiDocParser asciiDocParser = new AsciiDocParser(); | ||
return asciiDocParser.serializeKindFilenameTable(mappingFile); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private static InputStream loadContent(String location) { | ||
InputStream resourceAsStream = KindFilenameMapperUtil.class.getResourceAsStream(location); | ||
|
||
if (resourceAsStream == null) { | ||
throw new IllegalArgumentException(String.format("%s cannot be found in classpath", location)); | ||
} | ||
|
||
return resourceAsStream; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.