Skip to main content



      Home
Home » Modeling » TMF (Xtext) » XPand Multiple Ouput Files and Multiple Beautifiers
XPand Multiple Ouput Files and Multiple Beautifiers [message #556122] Mon, 30 August 2010 17:30 Go to next message
Eclipse UserFriend
I have a template that generates a bunch of .html and .js files. For the .js files i want to apply a JavaScript beautifier and for the .html files i want to apply an XML or HMTL beautifier.

How do i achieve this goal?

My generator workflow is as follows
module workflow.HyvesMobileGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var fileEncoding = "UTF-8"
var modelPath = "./src/model/"

Workflow {
	
	component = org.eclipse.xtext.mwe.Reader {
		// lookup all resources on the classpath
		// useJavaClassPath = true
		
		// or define search scope explicitly
		path = modelPath
		
		// this class will be generated by the xtext generator 
		register = nl.hyves.mobiledsl.HyvesMobileStandaloneSetup {}
		load = {
			slot = "model"
			type = "Model"
		}
	}
	
	component = org.eclipse.xpand2.Generator {
		metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel {}
		expand = "templates::Template::main FOREACH model"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}
}


Re: XPand Multiple Ouput Files and Multiple Beautifiers [message #556123 is a reply to message #556122] Mon, 30 August 2010 17:43 Go to previous messageGo to next message
Eclipse UserFriend
What i also would like to know is:

How do your specify your own beautifier?
What interface / class do they extend?
What beautifiers are there available?
Re: XPand Multiple Ouput Files and Multiple Beautifiers [message #556150 is a reply to message #556123] Tue, 31 August 2010 02:38 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

if you try code completion within the outlet part of the xpand generator component above you will also find the entries "postprocessor" and "name". The postprocessor is for beautifiers (Xpand is shipped with one for java and one for xml). Navigating to them you will also see the interface they have to implement.
Using different outlets (distinguishing them by name) you can easily write to different paths. I bet this is covered in the Xpand documentation.

Alex
Re: XPand Multiple Ouput Files and Multiple Beautifiers [message #557134 is a reply to message #556150] Sat, 04 September 2010 17:42 Go to previous messageGo to next message
Eclipse UserFriend
I took the time to implement a JavaScript beautifier for xtend myself.
Maybe i can commit this to the xtend sourcecode repository?

package org.eclipse.xtend.beautifiers.js;

import java.util.logging.Logger;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.wst.jsdt.core.formatter.CodeFormatter;
import org.eclipse.wst.jsdt.internal.formatter.DefaultCodeFormatter;
import org.eclipse.xpand2.output.FileHandle;
import org.eclipse.xpand2.output.PostProcessor;

/**
 * Special xtend beautifer which beautifies javascript files
 * @author Tjerk Wolterink (tjerkw@gmail.com)
 */
public class JavaScriptBeautifier implements PostProcessor {
    private Logger log = Logger.getLogger(JavaScriptBeautifier.class.getName());
    private int indentationLevel;

    public void afterClose(FileHandle impl) {
        // TODO Auto-generated method stub
        
    }

    public void setIndentationLevel(int indentationLevel) {
        this.indentationLevel = indentationLevel;
    }
    
    public void beforeWriteAndClose(FileHandle impl) {
        if(!impl.getAbsolutePath().endsWith("js")) {
            log.info("Ingoring non js file: "+impl.getAbsolutePath());
            return;
        }
        log.info("Applying js beautifer to file: "+impl.getAbsolutePath());
        
        DefaultCodeFormatter formatter = new DefaultCodeFormatter();
        String source = impl.getBuffer().toString();
        TextEdit result = formatter.format(CodeFormatter.K_UNKNOWN, source, 0, source.length(), indentationLevel, "\n");
        if(result == null) {
            log.info("Result from formatter is null, probably a syntax error in de file: "+impl.getAbsolutePath());
            return;
        }
        IDocument document = new Document(source);
        try {
            result.apply(document);
            impl.setBuffer(document.get());
            
        } catch (MalformedTreeException e) {
            e.printStackTrace();
            log.info("Formatting failed: "+e);
            
        } catch (BadLocationException e) {
            e.printStackTrace();
            log.info("Formatting failed: "+e);
        }
      
    }
}

Re: XPand Multiple Ouput Files and Multiple Beautifiers [message #557135 is a reply to message #557134] Sat, 04 September 2010 17:43 Go to previous message
Eclipse UserFriend
And now that i am thinking of it.. it would be cool to have a Generic postprocessor which chooses the correct beautifier based on the file extension... xml beautifer for .xml files, javascript beautifier for .js files etc...

That would be awesome!
Previous Topic:Xtext 0.7.2 + Maven2 - "Could not open the editor"
Next Topic:Removing elements from Model
Goto Forum:
  


Current Time: Wed Jul 02 14:48:39 EDT 2025

Powered by FUDForum. Page generated in 0.03371 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top