Search in sources :

Example 1 with ResourceClassTerm

use of org.activityinfo.i18n.tools.model.ResourceClassTerm in project activityinfo by bedatadriven.

the class Check method checkForXmlEntities.

private void checkForXmlEntities() throws IOException {
    Pattern entityPattern = Pattern.compile("&#?[A-Za-z0-9]+;");
    boolean entityFound = false;
    for (String className : Project.INSTANCE.getResourceClasses()) {
        ResourceClass resourceClass = new ResourceClass(Project.INSTANCE.getSourceDirectory(), className);
        InspectingVisitor visitor = resourceClass.inspect();
        for (ResourceClassTerm term : visitor.getTerms()) {
            Matcher matcher = entityPattern.matcher(term.getDefaultTranslation());
            if (matcher.find()) {
                System.err.println(String.format("Term %s in %s uses the XML entity %s.", term.getKey(), className, matcher.group()));
                entityFound = true;
            }
        }
        for (String language : Project.INSTANCE.getLanguages()) {
            File resourceFile = resourceClass.getResourceFile(language);
            Map<String, String> translations = resourceClass.readResource(language);
            for (String key : translations.keySet()) {
                String translation = translations.get(key);
                Matcher matcher = entityPattern.matcher(translation);
                if (matcher.find()) {
                    System.err.println(String.format("Translation %s in %s uses XML entity %s", key, resourceFile.getName(), matcher.group()));
                    entityFound = true;
                }
            }
        }
    }
    if (entityFound) {
        System.err.println("XML Entities should not be used in terms because they are not parsed in many contexts.");
        System.err.println("Use unicode escapes instead.");
        failed = true;
    }
}
Also used : Pattern(java.util.regex.Pattern) ResourceClassTerm(org.activityinfo.i18n.tools.model.ResourceClassTerm) Matcher(java.util.regex.Matcher) InspectingVisitor(org.activityinfo.i18n.tools.parser.InspectingVisitor) File(java.io.File) ResourceClass(org.activityinfo.i18n.tools.model.ResourceClass)

Example 2 with ResourceClassTerm

use of org.activityinfo.i18n.tools.model.ResourceClassTerm in project activityinfo by bedatadriven.

the class PoEditorSource method updateTerms.

/**
 * Updates the translation source, adding any missing terms and their default
 * translations.
 *
 * @param terms
 */
public void updateTerms(List<ResourceClassTerm> terms, boolean sync) throws IOException {
    List<PoTermUpdate> updates = Lists.newArrayList();
    for (ResourceClassTerm term : terms) {
        updates.add(new PoTermUpdate(term.getKey(), term.getDefaultTranslation()));
    }
    PoEditorClient client = new PoEditorClient(apiToken);
    PoUploadResponse response = client.upload(projectId, updates, sync);
    PoUploadResponse.Details details = response.getDetails();
    System.out.println(String.format("Terms:       %5d  Added: %5d  Deleted: %d", details.getTerms().getParsed(), details.getTerms().getAdded(), details.getTerms().getDeleted()));
    System.out.println(String.format("Definitions: %5d  Added: %5d  Updated: %d", details.getDefinitions().getParsed(), details.getDefinitions().getAdded(), details.getDefinitions().getUpdated()));
}
Also used : ResourceClassTerm(org.activityinfo.i18n.tools.model.ResourceClassTerm) PoUploadResponse(org.activityinfo.i18n.tools.po.PoUploadResponse) PoTermUpdate(org.activityinfo.i18n.tools.po.PoTermUpdate) PoEditorClient(org.activityinfo.i18n.tools.po.PoEditorClient)

Example 3 with ResourceClassTerm

use of org.activityinfo.i18n.tools.model.ResourceClassTerm in project activityinfo by bedatadriven.

the class Pull method validateMessages.

private TranslationSet validateMessages(ResourceClass resourceClass, CompilationUnit cu, TranslationSet translationSet) throws IOException {
    InspectingVisitor inspector = new InspectingVisitor(resourceClass.getJavaSourceFile().getName());
    inspector.visit(cu, null);
    for (ResourceClassTerm resourceClassTerm : inspector.getTerms()) {
        checkForNewline(resourceClassTerm, translationSet);
    }
    if (!inspector.isMessageSubtype()) {
        return translationSet;
    }
    ValidatingVisitor validator = new ValidatingVisitor(translationSet);
    validator.visit(cu, null);
    return validator.getValidatedSet();
}
Also used : ResourceClassTerm(org.activityinfo.i18n.tools.model.ResourceClassTerm) ValidatingVisitor(org.activityinfo.i18n.tools.parser.ValidatingVisitor) InspectingVisitor(org.activityinfo.i18n.tools.parser.InspectingVisitor)

Example 4 with ResourceClassTerm

use of org.activityinfo.i18n.tools.model.ResourceClassTerm in project activityinfo by bedatadriven.

the class Push method execute.

public void execute() throws IOException {
    List<ResourceClassTerm> terms = Lists.newArrayList();
    for (String className : Project.INSTANCE.getResourceClasses()) {
        ResourceClass resourceClass = new ResourceClass(Project.INSTANCE.getSourceDirectory(), className);
        InspectingVisitor visitor = resourceClass.inspect();
        terms.addAll(visitor.getTerms());
    }
    if (dryRun) {
        Project.INSTANCE.getTranslationSource().dumpNewTerms(terms);
    } else {
        Project.INSTANCE.getTranslationSource().updateTerms(terms, purge);
    }
}
Also used : ResourceClassTerm(org.activityinfo.i18n.tools.model.ResourceClassTerm) InspectingVisitor(org.activityinfo.i18n.tools.parser.InspectingVisitor) ResourceClass(org.activityinfo.i18n.tools.model.ResourceClass)

Example 5 with ResourceClassTerm

use of org.activityinfo.i18n.tools.model.ResourceClassTerm in project activityinfo by bedatadriven.

the class InspectingVisitor method visit.

@Override
public void visit(MethodDeclaration n, Void arg) {
    ResourceClassTerm term = new ResourceClassTerm(AstEvaluator.parseTermKey(n));
    for (AnnotationExpr annotation : n.getAnnotations()) {
        if (annotation.getName().getName().equals(defaultValueAnnotation)) {
            term.setDefaultTranslation(AstEvaluator.parseAnnotationValue(annotation));
        } else if (annotation.getName().getName().equals("Meaning")) {
            term.setMeaning(AstEvaluator.parseAnnotationValue(annotation));
        }
    }
    if (termMap.containsKey(term.getKey())) {
        throw new RuntimeException("Duplicate key: " + term.getKey());
    }
    termMap.put(term.getKey(), term);
}
Also used : ResourceClassTerm(org.activityinfo.i18n.tools.model.ResourceClassTerm) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr)

Aggregations

ResourceClassTerm (org.activityinfo.i18n.tools.model.ResourceClassTerm)5 InspectingVisitor (org.activityinfo.i18n.tools.parser.InspectingVisitor)3 ResourceClass (org.activityinfo.i18n.tools.model.ResourceClass)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 File (java.io.File)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ValidatingVisitor (org.activityinfo.i18n.tools.parser.ValidatingVisitor)1 PoEditorClient (org.activityinfo.i18n.tools.po.PoEditorClient)1 PoTermUpdate (org.activityinfo.i18n.tools.po.PoTermUpdate)1 PoUploadResponse (org.activityinfo.i18n.tools.po.PoUploadResponse)1