Skip to content

Commit

Permalink
WIP: asciidoc reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jan 5, 2018
1 parent 8dff725 commit 3282d13
Show file tree
Hide file tree
Showing 48 changed files with 1,120 additions and 60 deletions.
2 changes: 1 addition & 1 deletion api/zanata-common-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@

<dependency>
<!-- we still need this because the annotation must present in the
interface/dto otherwise RESTEasy server implementation won't work-->
interface/Model otherwise RESTEasy server implementation won't work-->
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<exclusions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public enum DocumentType {

SUBTITLE("srt", "sbt", "sub", "vtt"),
PROPERTIES("properties"), PROPERTIES_UTF8("properties"),
XML("xml"), XLIFF("xlf"), TS("ts"), JSON("json");
XML("xml"), XLIFF("xlf"), TS("ts"), JSON("json"),
ASCIIDOC("adoc");

private static final Set<String> allSourceExtensions = buildExtensionsList(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ public static List<DocumentType> fileProjectSourceDocTypes() {
return Arrays.asList(XML_DOCUMENT_TYPE_DEFINITION,
PLAIN_TEXT, IDML, HTML, OPEN_DOCUMENT_TEXT, OPEN_DOCUMENT_PRESENTATION,
OPEN_DOCUMENT_GRAPHICS, OPEN_DOCUMENT_SPREADSHEET, SUBTITLE,
GETTEXT, PROPERTIES, PROPERTIES_UTF8, XLIFF, TS, JSON);
GETTEXT, PROPERTIES, PROPERTIES_UTF8, XLIFF, TS, JSON, ASCIIDOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void jsonSchemaIsTheSameWithAndWithoutJaxbAnnotation()
JsonSchema jsonSchemaWithJaxb = mixedMapper.generateJsonSchema(
dtoClass,
mixedConfig);
// this is to test, if any dto javabean property has a name that is
// this is to test, if any Model javabean property has a name that is
// different from the name in @XmlElement, we have a corresponding
// @JsonProperty to make sure the JSON name is controlled. At this
// stage, it's okay not to have a @JsonProperty as long as the javabean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class TranslationMemoryPage extends BasePage {
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(TranslationMemoryPage.class);
public static final String ID_UNAVAILABLE = "This Id is not available";
public static final String ID_UNAVAILABLE = "This ExplicitId is not available";
public static final String UPLOAD_ERROR =
"There was an error uploading the file";
public static final String NO_MEMORIES =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void translationMemoryIdsAreUnique() throws Exception {

assertThat(translationMemoryEditPage.getErrors())
.contains(TranslationMemoryPage.ID_UNAVAILABLE)
.as("The Id Is Not Available error is displayed");
.as("The ExplicitId Is Not Available error is displayed");

translationMemoryEditPage = translationMemoryEditPage
.clickSaveAndExpectFailure();
Expand All @@ -111,7 +111,7 @@ public void translationMemoryIdsAreUnique() throws Exception {

assertThat(translationMemoryEditPage.getErrors())
.contains(TranslationMemoryPage.ID_UNAVAILABLE)
.as("The Id Is Not Available error is displayed");
.as("The ExplicitId Is Not Available error is displayed");
}

@Trace(summary = "The administrator can import data from a tmx data " +
Expand Down
7 changes: 7 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,13 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<manifestEntries>
<Dependencies>org.asciidoctor</Dependencies> (2)
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
8 changes: 7 additions & 1 deletion server/services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@
</build>

<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>1.6.0-alpha.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang</artifactId>
Expand Down Expand Up @@ -1435,7 +1441,7 @@
<version>1.4</version>
</dependency>
<dependency>
<!-- New group Id -->
<!-- New group ExplicitId -->
<groupId>com.io7m.xom</groupId>
<artifactId>xom</artifactId>
<exclusions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void continueToPreviousUrl() {
* Only for open id.
*
* @param authProvider
* Open Id authentication provider.
* Open ExplicitId authentication provider.
*/
public String openIdLogin(String authProvider) {
OpenIdProviderType providerType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void verifySlugAvailable(ValueChangeEvent e) {

public boolean validateSlug(String slug, String componentId) {
if (!slugEntityServiceImpl.isSlugAvailable(slug, TransMemory.class)) {
facesMessages.addToControl(componentId, "This Id is not available");
facesMessages.addToControl(componentId, "This ExplicitId is not available");
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ else if (type.equals("fedora"))
if (type.equals("yahoo"))
return "Yahoo";
if (type.equals("openid"))
return "Open Id";
return "Open ExplicitId";
else
return "Unknown";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.adapter;

import org.zanata.adapter.asciidoc.AsciidocFilter;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class AsciidocAdapter extends OkapiFilterAdapter {

public AsciidocAdapter() {
super(prepareFilter(), IdSource.contentHash);
}

private static AsciidocFilter prepareFilter() {
return new AsciidocFilter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.zanata.adapter.asciidoc;

import net.sf.okapi.common.filters.EventBuilder;
import net.sf.okapi.common.filters.IFilter;
import net.sf.okapi.common.filters.InlineCodeFinder;
import net.sf.okapi.common.resource.ITextUnit;
import net.sf.okapi.common.resource.TextFragment;
/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class AsciidocEventBuilder extends EventBuilder {
private InlineCodeFinder codeFinder;

public AsciidocEventBuilder(String rootId, IFilter subFilter) {
super(rootId, subFilter);
codeFinder = null;
}

@Override
protected ITextUnit postProcessTextUnit(ITextUnit textUnit) {
TextFragment text = textUnit.getSource().getFirstContent();
if (codeFinder != null) {
codeFinder.process(text);
}
return textUnit;
}

public void setCodeFinder(InlineCodeFinder codeFinder) {
this.codeFinder = codeFinder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package org.zanata.adapter.asciidoc;

import net.sf.okapi.common.BOMNewlineEncodingDetector;
import net.sf.okapi.common.Event;
import net.sf.okapi.common.IParameters;
import net.sf.okapi.common.exceptions.OkapiBadFilterInputException;
import net.sf.okapi.common.exceptions.OkapiUnsupportedEncodingException;
import net.sf.okapi.common.filters.AbstractFilter;
import net.sf.okapi.common.filters.FilterConfiguration;
import net.sf.okapi.common.resource.RawDocument;
import net.sf.okapi.filters.json.Parameters;
import net.sf.okapi.filters.json.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class AsciidocFilter extends AbstractFilter {
private static final String MIME_TYPE = "application/asciidoc";
private static final Logger log = LoggerFactory.getLogger(AsciidocFilter.class);

private BOMNewlineEncodingDetector detector;
private RawDocument currentRawDocument;
private AsciidocEventBuilder eventBuilder;
private Parameters params;
private AsciidocParser parser;

public AsciidocFilter() {
super();
setMimeType(MIME_TYPE);
setMultilingual(false);
setName("okf_asciidoc");
setDisplayName("Asciidoc Filter");

addConfiguration(new FilterConfiguration(
getName(),
MIME_TYPE,
getClass().getName(),
"Asciidoc",
"Asciidoc files",
null,
".adoc"));
setParameters(new Parameters());
}

@Override
protected boolean isUtf8Encoding() {
return detector != null && detector.hasUtf8Encoding();
}

@Override
protected boolean isUtf8Bom() {
return detector != null && detector.hasUtf8Bom();
}

@Override
public void open(RawDocument input) {
open(input, true);
}

@Override
public void open(RawDocument input, boolean generateSkeleton) {
currentRawDocument = input;

detector = new BOMNewlineEncodingDetector(input.getStream(), input.getEncoding());
detector.detectAndRemoveBom();
setNewlineType(detector.getNewlineType().toString());

String encoding = getDetectedEncoding();
input.setEncoding(encoding);
setEncoding(encoding);
setOptions(input.getSourceLocale(), input.getTargetLocale(), encoding, generateSkeleton);

BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(detector.getInputStream(), encoding));
}
catch (UnsupportedEncodingException e) {
throw new OkapiUnsupportedEncodingException(
String.format("The encoding '%s' is not supported.", encoding), e);
}

if (input.getInputURI() != null) {
setDocumentName(input.getInputURI().getPath());
}

// Create EventBuilder with document name as rootId
if (eventBuilder == null) {
eventBuilder = new AsciidocEventBuilder(getParentId(), this);
} else {
eventBuilder.reset(getParentId(), this);
}
eventBuilder.setPreserveWhitespace(true);

// Compile code finder rules
if (params.getUseCodeFinder()) {
params.getCodeFinder().compile();
eventBuilder.setCodeFinder(params.getCodeFinder());
}

eventBuilder.addFilterEvent(createStartFilterEvent());
this.parser = new AsciidocParser(reader, eventBuilder);
try {
parser.parse();
} catch (ParseException e) {
String error = String.format("Error parsing ASCIIDOC file: %s", e.getMessage());
log.error(error, e);
throw new OkapiBadFilterInputException(error, e);
}

eventBuilder.flushRemainingTempEvents();
eventBuilder.addFilterEvent(createEndFilterEvent());
}

@Override
public boolean hasNext() {
return eventBuilder.hasNext();
}

@Override
public Event next() {
return eventBuilder.next();
}

@Override
public IParameters getParameters() {
return params;
}

@Override
public void setParameters(IParameters iParameters) {
this.params = (Parameters)iParameters;
}

@Override
public void close() {
if (currentRawDocument != null) {
currentRawDocument.close();
detector = null;
eventBuilder = null;
}
}

private String getDetectedEncoding() {
String detectedEncoding = getEncoding();
if (detector.isDefinitive()) {
detectedEncoding = detector.getEncoding();
log.debug("Overriding user set encoding (if any). Setting auto-detected encoding {}.",
detectedEncoding);

} else if (!detector.isDefinitive() && getEncoding().equals(RawDocument.UNKOWN_ENCODING)) {
detectedEncoding = detector.getEncoding();
log.debug("Default encoding and detected encoding not found. Using best guess encoding {}",
detectedEncoding);
}
return detectedEncoding;
}
}
Loading

0 comments on commit 3282d13

Please sign in to comment.