I am generating java source code using JCodeModel. I would to add copyright information to the generated code. Is this possible currently?
I tried using javadoc()in JDefinedClass , it adds the information only above the class definition.
com.sun.codemodel.writer.PrologCodeWriter is exactly what you are looking for
You can create a CodeWriter that writes the copyright header. This CodeWriter can delegate to another one - namely, to the one that you would usually pass to the CodeModel#build method.
A complete example:
import java.io.IOException;
import java.io.OutputStream;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.writer.SingleStreamCodeWriter;
public class HeaderInCodeModel
{
public static void main(String[] args) throws Exception
{
JCodeModel codeModel = new JCodeModel();
codeModel._class("com.example.Example");
CodeWriter codeWriter = new SingleStreamCodeWriter(System.out);
String header = "// Copyright 2017 - example.com\n";
CodeWriter codeWriterWithHeader =
createCodeWriterWithHeader(header, codeWriter);
codeModel.build(codeWriterWithHeader);
}
private static CodeWriter createCodeWriterWithHeader(
String header, CodeWriter delegate)
{
CodeWriter codeWriter = new CodeWriter()
{
#Override
public OutputStream openBinary(JPackage pkg, String fileName)
throws IOException
{
OutputStream result = delegate.openBinary(pkg, fileName);
if (header != null)
{
result.write(header.getBytes());
}
return result;
}
#Override
public void close() throws IOException
{
delegate.close();
}
};
return codeWriter;
}
}
The resulting class will be
// Copyright 2017 - example.com
package com.example;
public class Example {
}
Related
How do I invoke the following method JsonParser:
/**
* Converts a JSON document to XML.
* #param io input
* #param options parser options
* #return parser
* #throws IOException I/O exception
*/
private static IOContent toXML(final IO io, final JsonParserOptions options) throws IOException {
final JsonConverter conv = JsonConverter.get(options);
final IOContent xml = new IOContent(conv.convert(io).serialize().finish());
xml.name(io.name());
return xml;
}
yet I'm certainly not seeing this method from the IDE:
The method is in the JavaDocs:
Method Detail
toXML
public static IOContent toXML(IO io,
JsonParserOptions options)
throws java.io.IOException
Converts a JSON document to XML.
Parameters:
io - input
options - parser options
Returns:
parser
Throws:
java.io.IOException - I/O exception
The build file is using:
compile group: 'org.basex', name: 'basex', version: '9.2.4'
which is the most recent version I see on the repository:
maven { url "https://mvnrepository.com/" }
I went so far as to assemble the project and extracted the .class file from the JAR from the resulting BaseX but didn't go futher to find if this method is there or not.
Perhaps I'm just not invoking the method properly?
whoops:
package basex;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Logger;
import org.basex.build.json.JsonParser;
import org.basex.build.xml.SAXWrapper;
import org.basex.core.MainOptions;
import org.basex.io.IOFile;
public class JsonToXmlTransformer {
private static final Logger log = Logger.getLogger(JsonToXmlTransformer.class.getName());
public JsonToXmlTransformer() {
}
private void baseXparseJsonFile(String fileName) throws IOException {
org.basex.build.json.JsonParser jsonParser = new org.basex.build.json.JsonParser(new IOFile(fileName), new MainOptions());
SAXWrapper foo = org.basex.build.json.JsonParser.xmlParser(new IOFile(fileName));
foo.parse();
String bar = foo.toString();
log.info(bar);
}
public void transform(String fileName) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
org.json.JSONObject json = new org.json.JSONObject(content);
log.info(org.json.XML.toString(json));
}
}
had the wrong package...
I am trying to integrate XLLoop from a servlet and trying to run in via HTTP protocol. Below is my code:
XlloopServlet.java
#WebServlet(value = "/FunctionServer", name = "FunctionServer", asyncSupported = true)
public class XlloopServlet extends FunctionServlet {
private static final long serialVersionUID = -3845895326255874126L;
#Override
public void init(final ServletConfig config) throws ServletException {
// Create a function information handler to register our functions
FunctionInformationHandler infoHandler = new FunctionInformationHandler();
// Create a reflection function handler and add the required methods
FunctionHandler handler = new FunctionHandler();
infoHandler.add(handler.getFunctions());
// Set the handlers
CompositeFunctionHandler compositeHandler = new CompositeFunctionHandler();
compositeHandler.add(handler);
compositeHandler.add(infoHandler);
// Setting the function handler in the parent servlet
setHandler(compositeHandler);
}
and my FunctionHandler class which registers the functions:
public class FunctionHandler implements IFunctionHandler, FunctionProvider {
private ReflectFunctionHandler rfh;
public FunctionHandler() {
// Create a reflection function handler and add the Math methods
rfh = new ReflectFunctionHandler();
rfh.addMethods("Math.", Math.class);
rfh.addMethods("Math.", Maths.class);
rfh.addMethods("CSV.", CSV.class);
rfh.addMethods("Reflect.", Reflect.class);
}
#Override
public XLoper execute(IFunctionContext arg0, String arg1, XLoper[] arg2) throws RequestException {
return rfh.execute(arg0, arg1, arg2);
}
#Override
public boolean hasFunction(String arg0) {
return rfh.hasFunction(arg0);
}
#Override
public FunctionInformation[] getFunctions() {
return rfh.getFunctions();
}
public ReflectFunctionHandler getReflectFunctionHandler() {
return rfh;
}
}
My XLLoop ini file is as below:
protocol=http
url=http://localhost:8080/MyApp/FunctionServer
Now, when I try to call a function from my excel, I get a call in the servlet class and everything executes, but functions are not getting executed on the excel file.
Anyone having any idea about how to integrate XLLoop plugin on a webserver like tomcat?
I've just implemented this with JAX-RS and a bit of Spring. I use a REST endpoint to populate the xlloop.ini file with the correct server host/port for the running service and then package up my xlsb, xll and ini file in a zip for clients to download. It's not particularly pretty at the moment but the web.xml and Startup snippets are below.
The thing I haven't spent time on yet is memory management. If a lot of users load a lot of data, I'll need to periodically clean that up, so beware of idle session threads!
Web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>com.myapp.web.excel.XLLoopStartup</listener-class>
</listener>
XLLoopStartup.java
public class XLLoopStartup implements ServletContextListener {
public static XLLoopStartup INSTANCE;
private FunctionServer fs;
#Inject
private SomeInjectionThing usefulSpringStuff;
#Override
public void contextDestroyed(ServletContextEvent sce) {
}
#Override
public void contextInitialized(ServletContextEvent sce) {
INSTANCE = this;
// Initialize my Spring stuff
if (sce != null){
WebApplicationContextUtils//
.getRequiredWebApplicationContext(sce.getServletContext())//
.getAutowireCapableBeanFactory()//
.autowireBean(this);
}
Executors.newSingleThreadExecutor().execute(new Runnable() {
#Override
public void run() {
registerConverters();
fs = new FunctionServer(Integer.parseInt(System.getProperty("port.tomcat.xlloop", "10606")));
ReflectFunctionHandler rfh = new ReflectFunctionHandler();
rfh.addMethods(ExcelTrades.CATEGORY, ExcelTrades.class);
rfh.addMethods(ExcelUtils.CATEGORY, ExcelUtils.class);
rfh.addMethods(ExcelPositions.CATEGORY, ExcelPositions.class);
rfh.addMethods(ExcelProducts.CATEGORY, ExcelProducts.class);
// Create a function information handler to register our functions
FunctionInformationHandler firh = new FunctionInformationHandler();
firh.add(rfh.getFunctions());
// Set the handlers
CompositeFunctionHandler cfh = new CompositeFunctionHandler();
cfh.add(rfh);
cfh.add(firh);
DebugFunctionHandler debugFunctionHandler = new DebugFunctionHandler(cfh);
fs.setFunctionHandler(new SecureFunctionHandler(debugFunctionHandler));
try {
fs.run();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
// For quick testing
public static void main(String[] args) {
new XLLoopStartup().contextInitialized(null);
}
// Function classes can statically access this instance and get spring things from it
public SomeInjectionThing getThing() {
return usefulSpringStuff;
}
}
ExcelService.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.io.IOUtils;
import io.swagger.annotations.Api;
#Api("excel")
#Path("/excel")
public class ExcelService {
#Context
UriInfo uri;
#GET
#Path("/download")
#Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response download() {
StringWriter sw = new StringWriter();
// Create an INI file. We should probably store all default settings in a file and just add the server info to
// it.
sw.write("server=");
sw.write(uri.getBaseUri().getHost());
sw.write(":");
sw.write(System.getProperty("port.tomcat.xlloop", "10605"));
String inifile = sw.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
// Add the ini file to the zip
ZipEntry entry = new ZipEntry("xlloop.ini");
zos.putNextEntry(entry);
zos.write(inifile.getBytes());
zos.closeEntry();
// Add the Excel files
writeFileFromClasspath(zos, "xlloop.xll");
// This is my custom Excel macro sheet with other useful functions for user authentication etc.
writeFileFromClasspath(zos, "xlloop.xlsb");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return Response.ok(new ByteArrayInputStream(baos.toByteArray()))
.header("Content-Disposition", "attachment; filename=xlloop.zip").build();
}
private void writeFileFromClasspath(ZipOutputStream zos, String filename) throws IOException {
ZipEntry xlFileEntry = new ZipEntry(filename);
zos.putNextEntry(xlFileEntry);
zos.write(IOUtils.toByteArray(ExcelService.class.getClassLoader().getResourceAsStream(filename)));
zos.closeEntry();
}
}
In my application I have a method which I cant execute without main method. It only runs inside the main method. When I call that method inside my servlet class. It show an exception
My class with Main Method
package com.books.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.HashSet;
import java.util.Set;
import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
public class ParserTest {
// download
public void download(String url, File destination) throws IOException, Exception {
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
public static Set<String> nounPhrases = new HashSet<>();
private static String line = "The Moon is a barren, rocky world ";
public void getNounPhrases(Parse p) {
if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP")
|| p.getType().equals("NNPS")) {
nounPhrases.add(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
}
}
public void parserAction() throws Exception {
// InputStream is = new FileInputStream("en-parser-chunking.bin");
File modelFile = new File("en-parser-chunking.bin");
if (!modelFile.exists()) {
System.out.println("Downloading model.");
download("https://drive.google.com/uc?export=download&id=0B4uQtYVPbChrY2ZIWmpRQ1FSVVk", modelFile);
}
ParserModel model = new ParserModel(modelFile);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses) {
// p.show();
getNounPhrases(p);
}
}
public static void main(String[] args) throws Exception {
new ParserTest().parserAction();
System.out.println("List of Noun Parse : " + nounPhrases);
}
}
It gives me below output
List of Noun Parse : [barren,, world, Moon]
Then I commented the main method and. Called the ParserAction() method in my servlet class
if (name.equals("bkDescription")) {
bookDes = value;
try {
new ParserTest().parserAction();
System.out.println("Nouns Are"+ParserTest.nounPhrases);
} catch (Exception e) {
}
It gives me the below exceptions
And below error in my Browser
Why is this happening ? I can run this with main method. But when I remove main method and called in my servlet. it gives an exception. Is there any way to fix this issue ?
NOTE - I have read below instructions in OpenNLP documentation , but I have no clear idea about it. Please help me to fix his issue.
Unlike the other components to instantiate the Parser a factory method
should be used instead of creating the Parser via the new operator.
The parser model is either trained for the chunking parser or the tree
insert parser the parser implementation must be chosen correctly. The
factory method will read a type parameter from the model and create an
instance of the corresponding parser implementation.
Either create an object of ParserTest class or remove new keyword in this line new ParserTest().parserAction();
Lately i have been trying to make communication between minecraft server (running with Java) and scratch (running with JavaScript).
I have written the code in java already:
package me.yotam180;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class HttpProcessor {
public MainClass plugin;
public HttpProcessor (MainClass plug) throws IOException {
plugin = plug;
plugin.getLogger().info("CREATED HTTTP PROCESSOR");
HttpServer server = HttpServer.create(new InetSocketAddress(9090), 0);
server.createContext("/pollplayer", new PollPlayerHandler());
server.createContext("/killplayer", new KillPlayerHandler());
plugin.getLogger().info("STARTED HTTTP SERVER");
server.setExecutor(null); // creates a default executor
server.start();
}
static class PollPlayerHandler implements HttpHandler {
#SuppressWarnings("deprecation")
#Override
public void handle(HttpExchange httpExchange) throws IOException {
// TODO Auto-generated method stub
Map <String,String>parms = HttpProcessor.queryToMap(httpExchange.getRequestURI().getQuery());
StringBuilder response = new StringBuilder();
response.append(Bukkit.getPlayer(parms.get("name")).getLocation().toString());
HttpProcessor.writeResponse(httpExchange, response.toString());
}
}
static class KillPlayerHandler implements HttpHandler {
#SuppressWarnings("deprecation")
#Override
public void handle(HttpExchange httpExchange) throws IOException {
// TODO Auto-generated method stub
Map <String,String>parms = HttpProcessor.queryToMap(httpExchange.getRequestURI().getQuery());
Bukkit.getPlayer(parms.get("name")).setHealth(0);
HttpProcessor.writeResponse(httpExchange, "SUCCESS");
}
}
public static void writeResponse(HttpExchange httpExchange, String response) throws IOException {
httpExchange.sendResponseHeaders(200, response.length());
OutputStream os = httpExchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
public static Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<String, String>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
}
Now I have to make the scratch side HTTP Client. Every way i tried, It just didn't work. I try to open my browser, i write http://localhost:9090/pollplayer?name=yotam_salmon and it reports my player location beautifully. Now my problmem is the scratch JS.
Here it is:
new (function () {
var ext = this;
// Cleanup function when the extension is unloaded
ext._shutdown = function () { };
// Status reporting code
// Use this to report missing hardware, plugin or unsupported browser
ext._getStatus = function () {
return { status: 2, msg: 'Ready' };
};
ext.get_Player = function (name, callback) {
//in this function i need to call http://localhost:9090/pollplayer?name= + name, wait for the response and then callback it.
//the response can't be "return response;", and it cannot be call backed from another function. If this function was called, it
//has to report the location back as a string
};
// Block and block menu descriptions
var descriptor = {
blocks: [
['R', 'location of %s', 'get_Player', 'Player'],
]
};
// Register the extension
ScratchExtensions.register('ScratchCraft', descriptor, ext);
})();
I cannot format my JS code differently, because Scratch works only with this format.(It is explained here: http://llk.github.io/scratch-extension-docs/). In the ext.get_Player function i have to go to the Java http server, request /pollplayer?name= + name, and callback it .
I would be happy to get a solution :) Thanks!
The solution was very simple. I just had to add an header of "Allow-Access-Cross-Origin", and it was solved.
httpExchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
httpExchange.getResponseHeaders().set("Content-Type", "text/plain");
Ok, the case is simple. I need to be able to enable/disable logging for a JDK class (HttpURLConnection) programmatically.
public class HttpLoggingTest {
/**
Just a dummy to get some action from HttpURLConnection
*/
private static void getSomething(String urlStr) throws MalformedURLException, IOException {
System.out.println("----- " + urlStr);
HttpURLConnection conn = (HttpURLConnection) new URL("http://www.google.com").openConnection();
for (Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
conn.disconnect();
}
public static void main(String[] args) throws MalformedURLException, IOException {
// HERE : Enable JDK logging for class
// sun.net.www.protocol.http.HttpURLConnection
getSomething("http://www.goodle.com");
// HERE: Disable JDK logging for class
// sun.net.www.protocol.http.HttpURLConnection
getSomething("http://www.microsoft.com");
}
}
In other words: before the first URL call the logging must be enabled and then disabled before the next call.
That is the challenge !
I'm unable to figure out how to do it.
Must work with Java 7.
Note:
I can do it by using configuration file, logging.properties :
sun.net.www.protocol.http.HttpURLConnection.level = ALL
but I want to have a programmatic solution.
UPDATE
Here's code that works in Java 6 but not in Java 7:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HttpLoggingTest {
/**
Just a dummy to get some action from HttpURLConnection
*/
private static void getSomething(String urlStr) throws MalformedURLException, IOException {
System.out.println("----- " + urlStr);
HttpURLConnection conn = (HttpURLConnection) new URL("http://www.google.com").openConnection();
for (Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
conn.disconnect();
}
private static void enableConsoleHandler() {
//get the top Logger
Logger topLogger = java.util.logging.Logger.getLogger("");
// Handler for console (reuse it if it already exists)
Handler consoleHandler = null;
//see if there is already a console handler
for (Handler handler : topLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
//found the console handler
consoleHandler = handler;
break;
}
}
if (consoleHandler == null) {
//there was no console handler found, create a new one
consoleHandler = new ConsoleHandler();
topLogger.addHandler(consoleHandler);
}
consoleHandler.setLevel(Level.ALL);
}
public static void main(String[] args) throws MalformedURLException, IOException {
enableConsoleHandler();
final Logger httpLogger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
// Enable JDK logging for class
//sun.net.www.protocol.http.HttpURLConnection
httpLogger.setLevel(java.util.logging.Level.FINE);
getSomething("http://www.goodle.com");
// Disable JDK logging for class
// sun.net.www.protocol.http.HttpURLConnection
httpLogger.setLevel(java.util.logging.Level.INFO);
getSomething("http://www.microsoft.com");
}
}
UPDATE2
In order to make sure that a solution only enables output from our target class (and not all sorts of other JDK internal classes) I've created this minimal JAXB example. Here JAXB is simply an example of 'something else', it could have been any other part of the JDK that also use PlatformLogger.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Minimal dummy JAXB example. Only purpose is to provoke
* some JAXB action. Non-prod quality!
*/
#XmlRootElement(name = "book")
public class Celebrity {
#XmlElement
public String getFirstName() {
return "Marilyn";
}
#XmlElement
public String getLastName() {
return "Monroe";
}
public void printXML() {
JAXBContext context;
try {
context = JAXBContext.newInstance(Celebrity.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(this, System.out);
} catch (JAXBException ex) {
}
}
}
Instantiate an instance of the Celebrity class and call printXML(). Put that into getSomething() method. This must not generate JAXB internal logging output ... or else you've enabled logging for more than you thought.
Stumbled over PlatformLoggingMXBean the other day. I'll need to try something like:
PlatformLoggingMXBean platformLoggingMXBean =
ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class);
platformLoggingMXBean.setLoggerLevel(
"sun.net.www.protocol.http.HttpURLConnection", "FINE");
and see it it works.
Try:
java.util.logging.Logger logger =
java.util.logging.Logger.getLogger(
"sun.net.www.protocol.http.HttpURLConnection");
logger.setLevel(java.util.logging.Level.FINE);