解析器java接口到html接口
我希望创建一个应用程序,将输入的java代码翻译成HTML,我有一个解析器用于创建框架、按钮、文本字段等。 包javaawt的所有组件都已重写。 我需要实现一个创建标记html对话框并插入任何组件的方法解析器java接口到html接口,java,html,Java,Html,我希望创建一个应用程序,将输入的java代码翻译成HTML,我有一个解析器用于创建框架、按钮、文本字段等。 包javaawt的所有组件都已重写。 我需要实现一个创建标记html对话框并插入任何组件的方法 public static void addmenu(ArrayList<pMenu> bar){ tag_div = doc.createElement("div"); body.appendChild(tag_div); tag_
public static void addmenu(ArrayList<pMenu> bar){
tag_div = doc.createElement("div");
body.appendChild(tag_div);
tag_div.setAttribute("id", "myNav");
tag_div.setAttribute("class", "frame overlay");
Element a = doc.createElement("a");
a.appendChild(doc.createTextNode("x"));
tag_div.appendChild(a);
a.setAttribute("href", "javascript:void(0)");
a.setAttribute("class", "closebtn");
a.setAttribute("onclick", "closeNav()");
span_menu = doc.createElement("span");
span_menu.setAttribute("class", "overlay-content");
tag_div.appendChild(span_menu);
/
parse_menu(bar);
/*
Element viewport = doc.createElement("META");
viewport.setAttribute("name", "viewport");
viewport.setAttribute("content", "user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi");
head.appendChild(viewport);
body = doc.createElement("BODY");
body.setAttribute("onhashchange", "updateFooter();");
body.setAttribute("onload", "defaultFrame();");
rootElement.appendChild(body);
*/
}
public static void parse_menu(ArrayList<pMenu> bar){
int i;
Element ul = doc.createElement("ul");
span_menu.appendChild(ul);
for(i=0;i<bar.size();i++){
Element li = doc.createElement("li");
ul.appendChild(li);
li.appendChild(doc.createTextNode("- " + bar.get(i).getName() + ": "));
li.setAttribute("onclick", "insert_menu(this)");
li.setAttribute("class", "colorMenu");
li.setAttribute("style", "color: #ecf503;");
//System.out.println();
if(bar.get(i).has_item())
parse_menu_item(li, bar.get(i).getArray_menu());
}
}
private static void parse_menu_item(Element tag_li, ArrayList<pMenuItem> pmenui){
int i;
Element ul = doc.createElement("ul");
tag_li.appendChild(ul);
ul.setAttribute("hidden", "true");
for(i=0;i<pmenui.size();i++){
Element li = doc.createElement("li");
ul.appendChild(li);
li.appendChild(doc.createTextNode(pmenui.get(i).getName()) );
li.setAttribute("id", pmenui.get(i).getName() );
li.setAttribute("onclick", "app.fire(id)");
li.setAttribute("class", "subnav");
if(pmenui.get(i).has_item())
parse_menu_item(li, pmenui.get(i).getArray_menu());
}
//System.out.println("</ul>");
}
public static void parse(pComponent c){
if(c instanceof pFrame){
for(pComponent component : ((pFrame) c).getComponents()) //tutti i pComponent del pFrame
parse(component);
} else if(c instanceof pPanel){ //AGGIUNGO I PANNELLI, con all'interno i pComponent
for(pComponent component : ((pPanel) c).getComponents())
parse(component);
}else if(c instanceof pDialog){
System.out.println(c.getClass().getName());
c.create();
for(pComponent component : ((pDialog) c).getComponents()){
parse(component);
}
}else{
if((c instanceof pMenuBar)||(c instanceof pMenuItem)||(c instanceof pMenu)){
return;
}
System.out.println("creating: "+c.getName());
System.out.println("parent: " +c.getRoot().getName()+" "+ c.getRoot().getClass().getName());
c.create(); //chiamo il metodo create di ogni pComponent dell'applicazione.
}
}
public static void createPage(String title, Set<Set<String>> partition){
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
Element rootElement = doc.createElement("HTML");
head = doc.createElement("HEAD");
doc.appendChild(rootElement);
addCss("css/index.css");
addCss("css/ionic.css");
addCss("css/ionicons.min.css");
addCss("css/menu/menulaterale.css");
rootElement.appendChild(head);
Element viewport = doc.createElement("META");
viewport.setAttribute("name", "viewport");
viewport.setAttribute("content", "user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi");
head.appendChild(viewport);
Element title1 = doc.createElement("TITLE");
title1.appendChild(doc.createTextNode(title));
head.appendChild(title1);
body = doc.createElement("BODY");
body.setAttribute("onhashchange", "updateFooter();");
body.setAttribute("onload", "defaultFrame();");
rootElement.appendChild(body);
_partition = partition;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public static Element addFrame(String title){
Element frame = doc.createElement("DIV");
frame.setAttribute("id", title);
frame.setAttribute("class", "frame");
Element div_menu = doc.createElement("div");
frame.appendChild(div_menu);
div_menu.setAttribute("class", "scritta_menu");
div_menu.setAttribute("onclick", "openNav()");
div_menu.appendChild(doc.createTextNode("||| MENU"));
return frame;
}
//AGGIUNGO L'HEADER DELLA BARRA - RUBRICA - DOVE DOVREBBE ANDARE IL MENU
public static void addHeader(String title){
/*
SAMPLE IONIC CODE
<div id="title" class="bar bar-header bar-positive">
<h1 class="title">Java AWT Calc</h1>
</div>
*/
Element header = doc.createElement("DIV");
header.setAttribute("id", "title");
header.setAttribute("class", "bar bar-header bar-positive");
Element h1 = doc.createElement("H1");
h1.setAttribute("class", "title");
h1.appendChild(doc.createTextNode(title));
header.appendChild(h1);
body.appendChild(header);
}
private static void addFooter(){
Element footer = doc.createElement("DIV");
Element framesBullets = doc.createElement("DIV");
Element buttonLeft = doc.createElement("BUTTON");
Element buttonRight = doc.createElement("BUTTON");
footer.setAttribute("id", "footer");
buttonLeft.setAttribute("id","buttonLeft");
buttonRight.setAttribute("id","buttonRight");
framesBullets.setAttribute("id", "framesBullets");
framesBullets.setAttribute("class", "title" );
footer.setAttribute("class", "bar bar-positive bar-footer");
buttonLeft.setAttribute("class", "hide button button-clear icon-left ion-android-arrow-dropleft-circle");
buttonRight.setAttribute("class", "hide button button-clear icon-right ion-android-arrow-dropright-circle");
footer.appendChild(buttonLeft);
footer.appendChild(framesBullets);
footer.appendChild(buttonRight);
body.appendChild(footer);
}
private static void addScript(String path){
Element script= doc.createElement("SCRIPT");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", path);
body.appendChild(script);
}
private static void addCss(String path){
Element css = doc.createElement("LINK");
css.setAttribute("rel", "stylesheet");
css.setAttribute("type", "text/css");
css.setAttribute("href", path);
head.appendChild(css);
}
public static void addButton(String name, Element root){
Element button = doc.createElement("BUTTON");
button.setAttribute("type", "button");
button.setAttribute("class", "button");
button.setAttribute("id", name);
button.appendChild(doc.createTextNode(name));
button.setAttribute("onClick", buttonEvent);
/*root.appendChild(button);*/
elements.put(name, button);
}
public static Element addDialog(String title,Element root) {
System.out.println("dialog creata");
Element dialog=doc.createElement("Dialog");
dialog.setAttribute("id", "dialog");
elements.put(title, dialog);
return dialog;
}
public static void addLabel(String name,Element root){
Element list = doc.createElement("DIV");
Element label = doc.createElement("LABEL");
label.setAttribute("id", name);
label.setAttribute("class", "label");
label.appendChild(doc.createTextNode(name));
list.appendChild(label);
/*root.appendChild(list);*/
elements.put(name, list);
}
public static void addRadioButton(String name,Element root) {
System.out.println("Radio button creato");
Element button = doc.createElement("INPUT");
button.setAttribute("type", "radio");
button.setAttribute("name", "name");
button.setAttribute("value", "value");
button.setAttribute("id", name);
button.appendChild(doc.createTextNode(name));
button.setAttribute("onClick", buttonEvent);
/* root.appendChild(button);*/
elements.put(name, button);
}
public static void addTextField(int size, String title,Element root){
Element list = doc.createElement("DIV");
list.setAttribute("class", "areaditesto");
Element label = doc.createElement("LABEL");
Element textField = doc.createElement("INPUT");
textField.setAttribute("type", "text");
textField.setAttribute("placeholder", title);
textField.setAttribute("id", title);
textField.setAttribute("onkeyup", textEvent);
label.appendChild(textField);
list.appendChild(label);
/*root.appendChild(list);*/
elements.put(title, list);
}
public static void writePage(String fl) throws URISyntaxException{
Iterator setsIterator = _partition.iterator();
int id = 0;
while(setsIterator.hasNext()){
Set<String> set = (Set<String>) setsIterator.next();
Element frame = addFrame("frame"+id);
Iterator setIterator = set.iterator();
while(setIterator.hasNext()){
String key = (String) setIterator.next();
Element element = elements.get(key);
if(element != null){
frame.appendChild(element);
}
}
if(frame.hasChildNodes()){
frames.put("frame"+id, frame);
id++;
}
}
Set<Entry<String, Element>> f = frames.entrySet();
Iterator i = f.iterator();
while(i.hasNext()){
Entry<String, Element> entry = (Entry<String, Element>) i.next();
body.appendChild(entry.getValue());
}
addFooter();
/*AGGIUNTA DEGLI SCRIPT*/
addScript("js/hammer.min.js");
addScript("js/index.js");
addScript("js/test.js");
addScript("js/cordova.js");
addScript("js/phonegap.js");
addScript("js/ionic.bundle.min.js");
addScript("js/menu/menulaterale.js");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "html");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fl);
transformer.transform(source, result);
} catch (TransformerException exception) {
exception.printStackTrace();
}
}
}
publicstaticvoidaddmenu(arraylistbar){
tag_div=doc.createElement(“div”);
正文.附加子项(标记_div);
tag_div.setAttribute(“id”、“myNav”);
tag_div.setAttribute(“类”、“帧覆盖”);
元素a=doc.createElement(“a”);
a、 appendChild(doc.createTextNode(“x”));
标签分区附件(a);
a、 setAttribute(“href”,“javascript:void(0)”;
a、 setAttribute(“类”、“closebtn”);
a、 setAttribute(“onclick”、“closeNav()”);
span_menu=doc.createElement(“span”);
span_menu.setAttribute(“类”、“覆盖内容”);
标记分区附加子项(span\u菜单);
/
解析菜单(条);
/*
元素视口=doc.createElement(“元”);
setAttribute(“名称”、“视口”);
setAttribute(“内容”,“用户可缩放=否,初始缩放=1,最大缩放=1,最小缩放=1,宽度=设备宽度,高度=设备高度,目标密度dpi=设备dpi”);
head.appendChild(视口);
body=doc.createElement(“body”);
setAttribute(“onhashchange”,“updateFooter();”;
setAttribute(“onload”,“defaultFrame();”);
根元素。子元素(体);
*/
}
公共静态void parse_菜单(ArrayList栏){
int i;
元素ul=doc.createElement(“ul”);
span_菜单追加子项(ul);
对于(i=0;iWelcome on StackOverflow!请查看和[MVCE]。目前,您的代码太长,无法被其他人阅读。另外,请告诉我们您尝试了什么?出现了什么问题?嗨,Nathan,我无论如何都做不到。为了了解解析器的工作原理,我尝试添加一个新方法addDialog()。此方法添加一个新对话框,但不在其中插入组件,而是将它们插入主框架。有很多代码,没有解释。请尝试缩短您的请求并澄清它:您想在java中获取字符串
,解析它,并将其中的元素转换为java中的元素
?目前尚不清楚所有这些都在你的问题中。我需要将java用户界面转换为html,为此我重写了AWT库,因此通过问题中的解析器,每个java组件都转换为html。我很难创建对话框,我无法集成组件。
public class pDialog extends pComponent {
Repository r= new Repository();
ActionListener _actionListener;
ArrayList<pComponent>b= new ArrayList<pComponent>();
public pDialog(String title){
_title= title;
}
public void addActionListener(ActionListener actionListener){
this._actionListener= actionListener;
}
public void setSize(int x,int y){
}
public void add(pComponent component){
component.setRoot(this);
System.out.println(component.getName() + " add to dialog parent "+ this.getName());
b.add(component);
}
public void setRoot(pComponent root){
super.setRoot(root);
}
public ArrayList<pComponent> getComponents(){
return b;
}
public void create(){
parserHTML.addDialog(_title, this.getRoot().getElement());
}