解析Java中缩进的文本树 [英] Parse indented text tree in Java
问题描述
我有我需要用java解析缩进文件, 我需要一些方法来把这个在科类,如下所示
根
目录root1
文本1
text1.1
text1.2
文本2
text2.1
text2.2
root2
文本1
text1.1
text1.2
文本2
text2.1
text2.2.2
我有类把缩进东西,它看起来像
公共类科
{
私人列表<节>儿童;
私人字符串文本;
私人诠释的深度;
公共课(串T)
{
文= T;
}
公开名单<节>的getChildren()
{
如果(儿童== NULL)
{
孩子=新的ArrayList<节>();
}
返回儿童;
}
公共无效setChildren(名单<节> newChildren)
{
如果(newChildren == NULL){
孩子= newChildren;
} 其他 {
如果(儿童== NULL){
孩子=新的ArrayList<节>();
}
对于(节子:newChildren){
this.addChild(子);
}
}
}
公共无效的addChild(节子)
{
如果(儿童== NULL){
孩子=新的ArrayList<节>();
}
如果(孩子!= NULL){
children.add(子);
}
}
公共字符串的getText()
{
返回文本;
}
公共无效的setText(字符串newText)
{
文= newText;
}
公共字符串getDepth()
{
返回深度;
}
公共无效setDepth(INT newDepth)
{
深度= newDepth;
}
}
我需要一些方法来解析该文件并将其放置在预期的结果,我们一个科对象,它看起来像下面的
部分=
文本=根
儿童
Child1:文本=中root1
Child1:text1中
Child1 =文本1.1
CHILD2 =文本1.2
CHILD2:文本2
Child1 =文本2.1
CHILD2 =文本2.2
儿童
CHILD2:文本=root2
Child1:text1中
Child1 =文本1.1
CHILD2 =文本1.2
CHILD2:文本2
Child1 =文本2.1
CHILD2 =文本2.2
这里是我开始有些code
INT indentCount = 0;
而(字符串文本= reader.readline()
{
indentCount = countLeadingSpaces(串词);
// TODO在此创建的部分
}
公共静态INT countLeadingSpaces(串词)
{
INT长度= word.length();
诠释计数= 0;
的for(int i = 0; I<长度;我++)
{
烧焦第一= word.charAt(ⅰ);
如果(Character.isWhitespace(第一))
{
算上++;
}
其他
{
返回计数;
}
}
返回计数;
}
我添加了一个父指针为好。也许文字可以被解析没有它,但父指针更容易。首先,你需要有更多的构造函数:
静态最终诠释root_depth = 4; //假设4空格precede树根
公共课(字符串文本,诠释深度){
this.text =文本;
this.depth =深度;
this.children =新的ArrayList<节>();
this.parent = NULL;
}
公共课(字符串文本,诠释的深度,部分家长){
this.text =文本;
this.depth =深度;
this.children =新的ArrayList<节>();
this.parent =父母;
}
然后,当你开始分析文件,读一行行:
第preV = NULL;
对(串线;(行= bufferedReader.readLine())= NULL;!){
如果($ P $光伏== NULL和放大器;&安培;符合root_depth空格开头){
科根=新科(text_of_line,root_depth);
preV =根;
}
其他 {
INT t_depth =无。的空格在此行的开始;
如果(t_depth> prev.getDepth())
//假设空段是不允许
第t_section =新科(text_of_line,t_depth,preV);
prev.addChild(t_section);
}
否则,如果(t_depth == prev.getDepth){
第t_section =新科(text_of_line,t_depth,prev.getParent());
。prev.getParent()的addChild(t_section);
}
其他 {
而(t_depth< prev.getDepth()){
preV = prev.getParent();
}
//在这一点上,(t_depth == prev.getDepth())=真
第t_section =新科(text_of_line,t_depth,prev.getParent());
。prev.getParent()的addChild(t_section);
}
}
}
我已经掩盖了的伪code一些细节问题,但我想你怎么去这解析的总体思路。千万记得要实现的方法的addChild(),getDepth()的getParent()等。
I have an indented file that I need to parsed using java, I need some way to place this in a Section class as shown below
root
root1
text1
text1.1
text1.2
text2
text2.1
text2.2
root2
text1
text1.1
text1.2
text2
text2.1
text2.2.2
I have the class for putting the indented stuff it looks like
public class Section
{
private List<Section> children;
private String text;
private int depth;
public Section(String t)
{
text =t;
}
public List<Section> getChildren()
{
if (children == null)
{
children = new ArrayList<Section>();
}
return children;
}
public void setChildren(List<Section> newChildren)
{
if (newChildren == null) {
children = newChildren;
} else {
if (children == null) {
children = new ArrayList<Section>();
}
for (Section child : newChildren) {
this.addChild(child);
}
}
}
public void addChild(Section child)
{
if (children == null) {
children = new ArrayList<Section>();
}
if (child != null) {
children.add(child);
}
}
public String getText()
{
return text;
}
public void setText(String newText)
{
text =newText;
}
public String getDepth()
{
return depth;
}
public void setDepth(int newDepth)
{
depth = newDepth;
}
}
I need some way to parse the file and place it in expected result which us a Section object which would look like below
Section=
Text="Root"
Children
Child1: Text= "root1"
Child1: "text1"
Child1="Text 1.1"
Child2="Text 1.2"
Child2: "text2"
Child1="Text 2.1"
Child2="Text 2.2"
Children
Child2: Text= "root2"
Child1: "text1"
Child1="Text 1.1"
Child2="Text 1.2"
Child2: "text2"
Child1="Text 2.1"
Child2="Text 2.2"
Here is some code that I have started
int indentCount=0;
while(String text = reader.readline()
{
indentCount=countLeadingSpaces(String word);
//TODO create the section here
}
public static int countLeadingSpaces(String word)
{
int length=word.length();
int count=0;
for(int i=0;i<length;i++)
{
char first = word.charAt(i);
if(Character.isWhitespace(first))
{
count++;
}
else
{
return count;
}
}
return count;
}
I added a parent pointer as well. Maybe the text can be parsed without it, but parent pointers make it easier. First of all, you need to have more constructors:
static final int root_depth = 4; // assuming 4 whitespaces precede the tree root
public Section(String text, int depth) {
this.text = text;
this.depth = depth;
this.children = new ArrayList<Section>();
this.parent = null;
}
public Section(String text, int depth, Section parent) {
this.text = text;
this.depth = depth;
this.children = new ArrayList<Section>();
this.parent = parent;
}
Then, when you start parsing the file, read it line by line:
Section prev = null;
for (String line; (line = bufferedReader.readLine()) != null; ) {
if (prev == null && line begins with root_depth whitespaces) {
Section root = new Section(text_of_line, root_depth);
prev = root;
}
else {
int t_depth = no. of whitespaces at the beginning of this line;
if (t_depth > prev.getDepth())
// assuming that empty sections are not allowed
Section t_section = new Section(text_of_line, t_depth, prev);
prev.addChild(t_section);
}
else if (t_depth == prev.getDepth) {
Section t_section = new Section(text_of_line, t_depth, prev.getParent());
prev.getParent().addChild(t_section);
}
else {
while (t_depth < prev.getDepth()) {
prev = prev.getParent();
}
// at this point, (t_depth == prev.getDepth()) = true
Section t_section = new Section(text_of_line, t_depth, prev.getParent());
prev.getParent().addChild(t_section);
}
}
}
I have glossed over some finer points of the pseudo-code, but I think you get the overall idea of how to go about this parsing. Do remember to implement the methods addChild(), getDepth(), getParent(), etc.
这篇关于解析Java中缩进的文本树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!