简体   繁体   中英

Navigation through Objects in Java

My aim is to recreate the structure of XML in custom Objects to operate with it further. Actually, I want to have XML as input and produce LaTeX as output. For this task I have implemented principles of JAXB library. But don't think that this is a good idea, because it is not convenient to retain the needed structure of document as output in TeX.

Here is an example of my custom class:

public class Section {

    private String title;
    private List<Par> par;
    private List<SubSec> subsec;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = "\\section {" + title + "}";
    }


    public List<Par> getPar() {
        if (par == null) {
            par = new ArrayList<Par>();
        }
         return this.par;
    }

    public List<SubSec> getSubSec() {
        if (subsec == null) {
            subsec = new ArrayList<SubSec> ();
        }
         return this.subsec;
    }

}

So I have a list of Section class, which have titles, list of paragraphs (Par) and list of subsections (SubSec) (simplify LaTeX article structure). Paragraphs contain text, but subsection can include also list of paragraphs. After XML input I transfer all Data from it in objects, instances of this Classes.

As example:

List<Section> listSections = new ArrayList<Section>();

// omitting the actions to recreate the structure and  set values to Objects
// now, to retrieve and write:

for (int j = 0; j < listSections.size(); j++) {
    List<Par> listParText = listSections.get(j).getPar();
    writer.write(listSections.get(j).getTitle());
    writer.newLine();
    for (Par parList : listParText) {
        if (parList.getText() != null) {
            writer.write(parList.getText());
            writer.newLine();
         }
     }
}

The problem is, that I can't recreate the structure of the document on the stage custom objects -> TeX. Although the structure is preserved on stage XML - custom objects. In Objects model I have, for example:

Section1(title): Par(text), Par(text), Par(text)
Section2(title): Subsection1(title): Par(text), Par(text), Par(text)
                 Subsection2(title): Par(text), Par(text)
Section3(title): Par(text)

Is there a way to save this order and get value in the same order to write them to file? Get values with getters and setters is NOT a problem to me, problem to retrieve them with proper order.

Update To clarify the problem, lets suppose every Section contains paragraphs (Par), subsection (SubSec), Tables, Figures in certain order. But obviously Java not allow to make a list like: List<SubSec, Par, Table, Fig> . I can put information there in certain order, but not retrieve. Or can I?

创建父类(例如DocumentComponent),它的SubSec,Par,Table和Fig都是子类,然后说一个文档是DocumentComponents的有序列表,这会起作用吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM