小编Don*_*oby的帖子

Java生产XML版本1.1

我有一个具有以下标头的xml文件:<?xml version="1.1" encoding="UTF-8"?>。这也是强制性的,此文件的版本为1.1,因为其中包含一些字符,而这些字符在1.1版中是不允许的。

我的任务是从文件中提取一些实体,然后再次将其另存为xml文件。如果我生成一个新的xml文件,它将是1.0版。由于新文件中存在一些编码错误,因此我认为问题是错误的xml版本。是否有可能在1.1版中生成xml文件(包括正确的标头)?

这是我当前代码的一个片段:

//read the file
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
...
}

//write the output file
SAXTransformerFactory fac = (SAXTransformerFactory)TransformerFactory.newInstance();
final TransformerHandler tfh = fac.newTransformerHandler();
Transformer transformer = tfh.getTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Run Code Online (Sandbox Code Playgroud)

我希望有人能帮助我。

最好,迈克尔

java xml sax

4
推荐指数
1
解决办法
1133
查看次数

我应该为我的单例类创建受保护的构造函数吗?

按照设计,在Singleton模式中,构造函数应标记为私有,并提供重新构建同一类型实例的私有静态成员的创建方法.我只创建了这样的单例类.

public class SingletonPattern {// singleton class

    private static SingletonPattern pattern = new SingletonPattern();

    private SingletonPattern() {

    }

    public static SingletonPattern getInstance() {
        return pattern;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,我必须扩展一个单例类来添加新的行为.但私有构造函数不允许定义子类.我正在考虑将默认构造函数更改为单例基类的受保护构造函数.

如果我定义我的构造函数,可能会出现什么问题protected

寻找专家意见....

java singleton design-patterns

3
推荐指数
1
解决办法
3848
查看次数

如何将变量传递给内部线程类?

我想定期将图像加载到图像项目.我的外部类正在生成URL,我需要将它传递给内部类.我该如何实现这一目标?

public class MapTimer extends TimerTask{
    public void run() {
        System.out.println("Map starting...");

        String URL=null,serverquery=null;
        try {
            sendMessage(this.message);
            item.setLabel(item.getLabel()+"start");
            serverquery=receiveMessage();
            item.setLabel(item.getLabel()+"stop");
            URL = getURL(serverquery);               // my url to be passed to innerclass
            System.out.println("URl is "+serverquery);
            item.setLabel(URL+item.getLabel());
            Thread t = new Thread() {
            public void run() {
                    item.setLabel(item.getLabel()+"6");
                    try {
                          Image image = loadImage(URL);            // using url
                          System.out.println("GEtting image....");
                          item = new ImageItem(null, image, 0, null);
                          form.append(item);
                          display.setCurrent(form);

                    } catch (IOException ioe) {
                          item.setLabel("Error1");
                    }
                    catch (Exception ioe) {
                          item.setLabel("Error1");
                    }
              } …
Run Code Online (Sandbox Code Playgroud)

java image

3
推荐指数
1
解决办法
3646
查看次数

Rails验证可防止保存

我有这样的用户模型:

class User < ActiveRecord::Base
    validates :password, :presence => true,
                         :confirmation => true,
                         :length => { :within => 6..40 }
    .
    .
    .
end
Run Code Online (Sandbox Code Playgroud)

在User模型中,我有一个我想要从OrdersController保存的billing_id列,如下所示:

class OrdersController < ApplicationController
    .
    .
    .
    def create
        @order = Order.new(params[:order])
        if @order.save
            if @order.purchase
                response = GATEWAY.store(credit_card, options)
                result = response.params['billingid']
                @thisuser = User.find(current_user)
                @thisuser.billing_id  = result
                if @thisuser.save
                        redirect_to(root_url), :notice => 'billing id saved')
                    else
                        redirect_to(root_url), :notice => @thisuser.errors)
                    end
            end
        end
    end
Run Code Online (Sandbox Code Playgroud)

因为validates :password在User模型中,@thisuser.save不保存.但是,一旦我注释掉验证,@thisuser.save返回true.这对我来说是一个陌生的领域,因为我认为这个验证仅在创建新用户时有效.有人可以告诉我validates …

validation ruby-on-rails

3
推荐指数
1
解决办法
1838
查看次数

java中的易失性和原子操作

我已阅读有关Java中原子操作的文章,但仍有一些疑问需要澄清:

int volatile num;
public void doSomething() {
  num = 10;  // write operation
  System.out.println(num)   // read
  num = 20;  // write
  System.out.println(num);  // read
}
Run Code Online (Sandbox Code Playgroud)

所以我在1方法上做了4次操作,它们是原子操作吗?如果多个线程同时调用doSomething()方法会发生什么?

java atomic volatile

3
推荐指数
1
解决办法
1617
查看次数

用Java检查内存布局

我正在尝试继承,出于教育目的,我想检查为各种对象和对象中的字段分配的地址.有没有一个工具可以让我看到JVM正在使用的内存以及使用它的内容.

例如,如果我有两个类:

class A { int i,j; int f { ...} }
class B extends A { int c; /* more methods, overriding f and declaring new ones as well */ }
Run Code Online (Sandbox Code Playgroud)

并在对象a和实例中实例化这些类b.

是否有一个工具可用于分析内存使用情况并确切地查看为这些内存分配的内存?

谢谢!

java memory-management

3
推荐指数
2
解决办法
5411
查看次数

是否可以使用Mockito抛出抽象异常?

我有一个用例,我试图确保在我的类中调用特定方法时抛出一个抽象异常.

我使用Mockito来做这件事,但是注意到Mockito在调用方法时根本不会抛出异常.

要测试的类:

public void doSomething() throws CustomException {
  try {
    Collection<T> results = dao.getDatabaseResults();
      } catch (ProblemException e) {
          throw new CustomException("There was an exception", e);
  }
}
Run Code Online (Sandbox Code Playgroud)

问题异常类:

public abstract class ProblemException extends RuntimeException {

  public ProblemException(String message) {
      super(message);
  }

  public ProblemException(String message, Throwable e) {
      super(message, e);
  }
Run Code Online (Sandbox Code Playgroud)

测试类:

public testDoSomething() throws Exception {
   CustomDAO mockDAO = Mockito.mock(CustomDAO.class);

   Mockito.when(mockDAO.getDatabaseResults()).thenThrow(new ProblemException);

   try {
       foo.doSomething();
       Assert.fail();
   } catch (CustomException e) {
      //Some more asserts
   }
Run Code Online (Sandbox Code Playgroud)

目前,上述测试类将无法编译,因为您无法创建抽象类的新实例.

我没有更改AbstractException类的权限,也无法更改DAO类上的getDatabaseResults()方法抛出的异常类型.

对于这个问题,您对最干净的解决方案有什么建议吗? …

java unit-testing exception abstract mockito

3
推荐指数
1
解决办法
3157
查看次数

使用xslt将xml转换为html

我在做什么:我正在尝试使用xslt将xml转换为html.


问题:程序正在执行而没有任何错误,它也会生成输出文件,但它不会将xml转换为html.我的猜测是forxsl 中的循环不是获取数据.


XSLTTest.java

 package JavaXSLTExample;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XSLTTest {
public static void main(String[] args)
{
    /*if (args.length != 3)
    {
        System.err.println("give command as follows : ");
        System.err.println("XSLTTest data.xml converted.xsl converted.html");
        return;
    }*/
    String dataXML = "C:\\Users\\Devrath\\Desktop\\XSL\\FileOne.xml";
    String inputXSL = "C:\\Users\\Devrath\\Desktop\\XSL\\FileTwo.xsl";
    String outputHTML = "C:\\Users\\Devrath\\Desktop\\XSL\\output1.html";

    XSLTTest st = new XSLTTest();
    try
    {
        st.transform(dataXML, inputXSL, outputHTML);
    }
    catch (TransformerConfigurationException e)
    {
        System.err.println("TransformerConfigurationException");
        System.err.println(e); …
Run Code Online (Sandbox Code Playgroud)

java xml xslt android

3
推荐指数
1
解决办法
8301
查看次数

如何使用大于X的整数参数来模拟方法调用

如何使用大于X的整数参数值模拟Mockito的方法调用?

我想写这样的东西:

doReturn("FooBar").when(persons).getPersons(Mockito.gt(10));
Run Code Online (Sandbox Code Playgroud)

mocking mockito

3
推荐指数
3
解决办法
4345
查看次数

wicket ModalWindow错误

我是wicket的新手,当我尝试运行我的应用程序时,他遇到错误:

WicketMessage:模态窗口内容id错误.组件ID:myPanel; 内容ID:内容:

在我的AddStudent html中:

<span wicket:id="InformationDialog"/>
<span wicket:id="myPanel"/>
Run Code Online (Sandbox Code Playgroud)

这是我打开标签后的第一件事

在AddStudent.java中(在构造函数中):

panel=new InformationPanel("myPanel");
message=new ModalWindow("InformationDialog");
message.setContent(panel);
message.setCssClassName(ModalWindow.CSS_CLASS_BLUE);
message.setTitle("Important Information");
Run Code Online (Sandbox Code Playgroud)

InformationPanel扩展Panel的位置:

<html>
<wicket:panel>
<table>
<tr>
<td><span wicket:id="message"/></td>
</tr>
<tr>
<td><input type ="button" value ="OK" wicket:id="ok"/></td>
</tr>
</table>
</wicket:panel>
<html>
Run Code Online (Sandbox Code Playgroud)

显然,我有一个相应的java类 - 它可能没有关系,但在这里它是:

package myapp.project;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.panel.Panel;

public class InformationPanel extends Panel {
    private Button ok;
    private Label messageLabel;
    public InformationPanel(String id){
        super(id);
        messageLabel=new Label("message","");
        ok=new Button("ok"){
            public void onSubmit(){
                AddStudent student = new AddStudent();
                setResponsePage(student);
            }
        }; …
Run Code Online (Sandbox Code Playgroud)

java wicket

2
推荐指数
1
解决办法
4202
查看次数