/* * Copyright 2008 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 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 General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package t2ext.weaver; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import t2ext.util.FileCopy; import t2ext.util.InfoFile; import t2ext.util.Util; import t2ext.weaver.utils.Template; import t2ext.xml.XMLBuilder; /** * FIXME when the classInfo.xml contains whitespace the getChildNodes() calls * return more results than normally. Some of which are unexpected. * * @author Maaike Gerritsen */ public class WeaverCreator { Element _classInfo; public void createWeaver() { Document doc = (new XMLBuilder()).readXMLFile(FileCopy.TEMP_DIR+"classInfo.xml"); NodeList list = doc.getChildNodes(); _classInfo = (Element) list.item(0); StringBuilder total = new StringBuilder(); total.append(Template.getPackage()); total.append(Template.getImports()); total.append(Template.getClassOpener()); total.append(Template.getFieldAuxMethods()); total.append("\n"); total.append(Template.getInitialisationPointCut(getClassName())); total = appendPointCuts(total); total.append(Template.getInitialisationAdvice()); total.append(Template.getCloseBracket()); writeToFile(total.toString()); } private StringBuilder appendPointCuts(StringBuilder total) { NodeList methods = _classInfo.getChildNodes(); InfoFile infoFile = InfoFile.load(); int id = 0; for(int i=0;i 0) { name = name.substring(0, name.length() - 1); builder.append(name); } builder.append(")"); return builder.toString(); } private String getClassName() { return _classInfo.getAttribute("name").toString(); } private void writeToFile(String total) { // TODO paths? File weaverDir = new File(FileCopy.TEMP_DIR+"t2ext/weaver"); if (weaverDir.mkdirs()) { File file = new File(weaverDir.getAbsolutePath() + "/Weaver.aj"); System.out.println("Writing aspect "+file); try { FileOutputStream fStream = new FileOutputStream(file); BufferedOutputStream out = new BufferedOutputStream(fStream); out.write(total.getBytes()); out.flush(); out.close(); } catch(IOException e) { System.out.println("Failed to write to "+file); e.printStackTrace(); System.exit(1); } } else { System.out.println("Failed to create "+weaverDir); System.exit(1); } } }